Total Pageviews

2019/10/12

[Apache POI] 常用 CellStyle 設定

Sample code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
     // 為 cell 上色
     CellStyle headerCellStyle = workbook.createCellStyle();
     headerCellStyle.setFillForegroundColor(IndexedColors.LIME.getIndex());
     headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
     headerCellStyle.setDataFormat(workbook.createDataFormat().getFormat("#,##0"));
     headerCellStyle.setFont(font);
    
     CellStyle highlightCellStyle = workbook.createCellStyle();
     highlightCellStyle.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
     highlightCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
     highlightCellStyle.setDataFormat(workbook.createDataFormat().getFormat("#,##0"));
    
     CellStyle footerCellStyle = workbook.createCellStyle();
     footerCellStyle.setFillForegroundColor(IndexedColors.LIME.getIndex());
     footerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
     footerCellStyle.setDataFormat(workbook.createDataFormat().getFormat("#,##0"));
     footerCellStyle.setFont(font);
    
     // 為 cell value 加上 1000 seperator
     CellStyle nonHighlightCellStyle = workbook.createCellStyle();
     nonHighlightCellStyle.setDataFormat(workbook.createDataFormat().getFormat("#,##0"));
    
     // 為 cell value 加上百分比
     CellStyle percentageCellStyle = workbook.createCellStyle();
     percentageCellStyle.setDataFormat(workbook.createDataFormat().getFormat("0.000%"));

No comments: