我正在使用 Apache POI 创建 Excel 文件。我正在使用简写符号来创建单元格,我想知道是否有一种方法可以使用相同的编码模式填充单元格上的颜色。
编码标准1.
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue(NYPG3Constants.FIRST_NAME);
headerRow.createCell(1).setCellValue(NYPG3Constants.LAST_NAME);
headerRow.createCell(2).setCellValue(NYPG3Constants.POLICY_NUMBER);
headerRow.createCell(3).setCellValue(NYPG3Constants.ZIP_CODE);
headerRow.createCell(4).setCellValue(NYPG3Constants.DATE_OF_BIRTH);
我可以使用以下编码模式设置样式,但对于每个单元格标题,我需要创建一个单独的单元格对象。
编码标准2
CellStyle style = workBook.createCellStyle();
style.setFillForegroundColor(HSSFColor.GOLD.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
Cell cell = headerRow.createCell(0);
cell.setCellValue(NYPG3Constants.FIRST_NAME);
cell.setCellStyle(style);
有没有办法使用我的第一个编码标准在标题单元格上填充颜色?
提前致谢
最佳答案
一个简单的 for
循环怎么样?
CellStyle style = workBook.createCellStyle();
style.setFillForegroundColor(HSSFColor.GOLD.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
...
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue(NYPG3Constants.FIRST_NAME);
headerRow.createCell(1).setCellValue(NYPG3Constants.LAST_NAME);
headerRow.createCell(2).setCellValue(NYPG3Constants.POLICY_NUMBER);
headerRow.createCell(3).setCellValue(NYPG3Constants.ZIP_CODE);
headerRow.createCell(4).setCellValue(NYPG3Constants.DATE_OF_BIRTH);
for (int c = 0; c < 5; c++) {
headerRow.getCell(c).setCellStyle(style);
}
关于java - 如何使用 Apache POI 为 Excel 标题单元格着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016490/