Apache-POI:单元格背景颜色不起作用
Apache-POI: Cell Background Color doesn't work
我正在尝试配置通用 CellStyles
以使用 Apache-POI 3.11
格式化 HSSFCells
。
这是一个可运行的代码示例。粗体和边框格式被正确应用。问题出在背景和前景颜色上。
关于我做错了什么的任何线索?
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 文件输出:
提前致谢。
我认为您需要更改一些内容。首先,您不能只为自定义颜色分配新的索引值。调色板已经满了,因此您需要用您自己的自定义颜色覆盖现有颜色。因此,请尝试将您的两种布鲁斯的定义更改为:
private final static short MY_LIGHT_BLUE = HSSFColor.CORNFLOWER_BLUE.index,
MY_DARK_BLUE = HSSFColor.BLUE.index;
其次,我从来没有同时设置前景色和背景色,因为这看起来很冲突。要更改单元格中的颜色,请尝试只设置前景色,例如:
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFillForegroundColor(MY_DARK_BLUE);
titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
titleStyle.setFont(fontBold);
setTopBotBorder(titleStyle);
如果您随后想要更改单元格中实际文本的颜色,您可以更改字体的颜色,例如:
fontNormal.setColor(HSSFColor.RED.index);
我已经对此进行了测试,它似乎有效。
我正在尝试配置通用 CellStyles
以使用 Apache-POI 3.11
格式化 HSSFCells
。
这是一个可运行的代码示例。粗体和边框格式被正确应用。问题出在背景和前景颜色上。
关于我做错了什么的任何线索?
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 文件输出:
提前致谢。
我认为您需要更改一些内容。首先,您不能只为自定义颜色分配新的索引值。调色板已经满了,因此您需要用您自己的自定义颜色覆盖现有颜色。因此,请尝试将您的两种布鲁斯的定义更改为:
private final static short MY_LIGHT_BLUE = HSSFColor.CORNFLOWER_BLUE.index,
MY_DARK_BLUE = HSSFColor.BLUE.index;
其次,我从来没有同时设置前景色和背景色,因为这看起来很冲突。要更改单元格中的颜色,请尝试只设置前景色,例如:
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFillForegroundColor(MY_DARK_BLUE);
titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
titleStyle.setFont(fontBold);
setTopBotBorder(titleStyle);
如果您随后想要更改单元格中实际文本的颜色,您可以更改字体的颜色,例如:
fontNormal.setColor(HSSFColor.RED.index);
我已经对此进行了测试,它似乎有效。