|
| 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