我正在尝试配置通用CellStyles
格式HSSFCells
以使用Apache-POI 3.11
.
这是代码的可运行示例。正确应用了粗体和边框格式。问题在于背景和前景色。
任何线索我做错了什么?
public class TestSO {
private final static short
MY_LIGHT_BLUE=100,
MY_DARK_BLUE=101,
MY_BLACK=102,
MY_WHITE=103;
public static void main(String[]args) throws Exception{
HSSFWorkbook workbook = new HSSFWorkbook();
setPallete( workbook.getCustomPalette() );
HSSFFont fontNormal = workbook.createFont();
fontNormal.setFontHeightInPoints((short)11);
fontNormal.setFontName("Calibri");
HSSFFont fontBold = workbook.createFont();
fontBold.setFontHeightInPoints((short)11);
fontBold.setFontName("Calibri");
fontBold.setBold(true);
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFillBackgroundColor(MY_DARK_BLUE);
titleStyle.setFillForegroundColor(MY_WHITE);
titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
titleStyle.setFont(fontBold);
setTopBotBorder(titleStyle);
HSSFCellStyle fpStyle = workbook.createCellStyle();
fpStyle.setFillBackgroundColor(MY_LIGHT_BLUE);
fpStyle.setFillForegroundColor(MY_BLACK);
fpStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
fpStyle.setFont(fontNormal);
setTopBotBorder(fpStyle);
HSSFSheet sheet = workbook.createSheet("Leyenda");
HSSFCell cell;
cell = sheet.createRow( 1 ).createCell( 1 );
cell.setCellValue("TitleStyle");
cell.setCellStyle(titleStyle);
cell = sheet.createRow( 2 ).createCell( 1 );
cell.setCellValue("FpStyle");
cell.setCellStyle(fpStyle);
sheet.autoSizeColumn(1);
try (FileOutputStream fos = new FileOutputStream( new File("TestWB.xls") )) {
workbook.write( fos );
}
}
private static void setPallete( HSSFPalette pallete ){
pallete.setColorAtIndex(MY_LIGHT_BLUE, (byte)189,(byte)215,(byte)238);
pallete.setColorAtIndex(MY_DARK_BLUE, (byte)32,(byte)55,(byte)100);
pallete.setColorAtIndex(MY_BLACK, (byte)0,(byte)0,(byte)0);
pallete.setColorAtIndex(MY_WHITE, (byte)255,(byte)255,(byte)255);
}
private static void setTopBotBorder( CellStyle style ){
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(MY_BLACK);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(MY_BLACK);
}
}
这是 Excel 文件输出:
提前致谢。