Skip to content

Commit 6ad2a5d

Browse files
committed
DP-5394 - Write Excel With Rich Formatting And Styles
1 parent ec0f8d8 commit 6ad2a5d

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[
2+
{
3+
"transactionId": 1001,
4+
"customerName": "Alice Johnson",
5+
"category": "Refund",
6+
"amount": -120.5,
7+
"transactionDate": "2025-01-05"
8+
},
9+
{
10+
"transactionId": 1002,
11+
"customerName": "Bob Smith",
12+
"category": "Purchase",
13+
"amount": 250.0,
14+
"transactionDate": "2025-01-06"
15+
},
16+
{
17+
"transactionId": 1003,
18+
"customerName": "Charlie Brown",
19+
"category": "Refund",
20+
"amount": -75.0,
21+
"transactionDate": "2025-01-07"
22+
},
23+
{
24+
"transactionId": 1004,
25+
"customerName": "Diana Prince",
26+
"category": "Purchase",
27+
"amount": 430.25,
28+
"transactionDate": "2025-01-08"
29+
},
30+
{
31+
"transactionId": 1005,
32+
"customerName": "Ethan Hunt",
33+
"category": "Refund",
34+
"amount": -60.0,
35+
"transactionDate": "2025-01-09"
36+
},
37+
{
38+
"transactionId": 1006,
39+
"customerName": "Fiona Lee",
40+
"category": "Purchase",
41+
"amount": 180.75,
42+
"transactionDate": "2025-01-10"
43+
},
44+
{
45+
"transactionId": 1007,
46+
"customerName": "George Miller",
47+
"category": "Refund",
48+
"amount": -200.0,
49+
"transactionDate": "2025-01-11"
50+
},
51+
{
52+
"transactionId": 1008,
53+
"customerName": "Hannah Kim",
54+
"category": "Purchase",
55+
"amount": 95.0,
56+
"transactionDate": "2025-01-12"
57+
},
58+
{
59+
"transactionId": 1009,
60+
"customerName": "Ian Wright",
61+
"category": "Refund",
62+
"amount": -45.99,
63+
"transactionDate": "2025-01-13"
64+
},
65+
{
66+
"transactionId": 1010,
67+
"customerName": "Julia Roberts",
68+
"category": "Purchase",
69+
"amount": 320.4,
70+
"transactionDate": "2025-01-14"
71+
}
72+
]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.northconcepts.datapipeline.examples.cookbook;
2+
3+
import java.io.File;
4+
5+
import com.northconcepts.datapipeline.core.DataEndpoint;
6+
import com.northconcepts.datapipeline.core.DataReader;
7+
import com.northconcepts.datapipeline.core.FieldLocationPredicate;
8+
import com.northconcepts.datapipeline.excel.ExcelCellStyleDecorator;
9+
import com.northconcepts.datapipeline.excel.ExcelColorPalette;
10+
import com.northconcepts.datapipeline.excel.ExcelDocument;
11+
import com.northconcepts.datapipeline.excel.ExcelWriter;
12+
import com.northconcepts.datapipeline.job.Job;
13+
import com.northconcepts.datapipeline.json.JsonRecordReader;
14+
15+
import org.apache.log4j.Logger;
16+
17+
public class WriteExcelWithRichFormattingAndStyles {
18+
19+
public static final Logger log = DataEndpoint.log;
20+
21+
public static void main(String[] args) {
22+
DataReader reader = new JsonRecordReader(new File("example/data/input/transactions_data.json"))
23+
.addRecordBreak("/array/object");
24+
25+
ExcelDocument document = new ExcelDocument();
26+
ExcelWriter writer = new ExcelWriter(document)
27+
.setSheetName("transactions")
28+
.setAutofitColumns(true);
29+
30+
// Add Formatting & Cell Style for Header
31+
writer.addHeaderCellStyle(FieldLocationPredicate.all(), ExcelCellStyleDecorator.bold()
32+
.and(ExcelCellStyleDecorator.backgroundColor(ExcelColorPalette.DARK_YELLOW))
33+
.and(ExcelCellStyleDecorator.fontColor(ExcelColorPalette.LIGHT_GREEN))
34+
);
35+
36+
// Add Formatting & Cell Style for Data
37+
writer.addDataCellStyle(FieldLocationPredicate.negativeNumber(), ExcelCellStyleDecorator.fontColor(ExcelColorPalette.RED)
38+
.and(ExcelCellStyleDecorator.italic()));
39+
writer.addDataCellStyle(FieldLocationPredicate.evenRowIndex(), ExcelCellStyleDecorator.backgroundColor(ExcelColorPalette.GREY_25_PERCENT));
40+
41+
Job.run(reader, writer);
42+
43+
document.save(new File("example/data/output/transactions-with-rich-formatting-and-styles.xlsx"));
44+
}
45+
46+
}

0 commit comments

Comments
 (0)