我试图给我的Excel的标题行的背景色,但下面的代码不工作。有人能帮忙吗?
编辑1下面是完整的代码供参考。标题文本覆盖了背景颜色,即使我将颜色设置为LIGHT_BLUE,Excel也会以黑色作为背景。
XSSFWorkbook工作簿=新建XSSFWorkbook();
XSSFCellStyle style = workbook.createCellStyle();
style.setFillBackgroundColor(IndexedColors.LIGHT_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
style.setFont(font);
XSSFSheet spreadsheet = workbook.createSheet("WIP E-MCAD Development");
Row row = spreadsheet.createRow(0);
Cell cell = row.createCell(1);
cell.setCellValue("Javatpoint");
cell.setCellStyle(style);
//style=row.getRowStyle();
Row rowhead = spreadsheet.createRow(1); //header
Font headerFont = workbook.createFont();
((XSSFFont) headerFont).setBold(true);
rowhead.setRowStyle(style);
rowhead.createCell(0).setCellValue("Wire");
rowhead.createCell(1).setCellValue("Device From");
rowhead.createCell(2).setCellValue("Pin From");
rowhead.createCell(3).setCellValue("Device To");
rowhead.createCell(4).setCellValue("Pin To");
rowhead.createCell(5).setCellValue("Wire Level");
rowhead.createCell(6).setCellValue("HRN Level Alias");
rowhead.createCell(7).setCellValue("Option Code");
rowhead.createCell(8).setCellValue("Feature Description");
rowhead.createCell(9).setCellValue("Option Code Status Message");
rowhead.createCell(10).setCellValue("CAN LIN Status");
for (int i = 1; i < list.size(); i++) {
SoniResponse data = list.get(i);
row = spreadsheet.createRow(i + 1);
row.createCell(0).setCellValue(data.getWire());
row.createCell(1).setCellValue(data.getDevice_From());
row.createCell(2).setCellValue(data.getPin_From());
row.createCell(3).setCellValue(data.getDevice_To());
row.createCell(4).setCellValue(data.getPin_To());
row.createCell(5).setCellValue(data.getWire_Level());
row.createCell(6).setCellValue(data.getHrn_Level_Alias());
row.createCell(7).setCellValue(data.getOption_Code());
row.createCell(8).setCellValue(data.getFeature_Description());
row.createCell(9).setCellValue(data.getOption_Code_Status_Message());
row.createCell(10).setCellValue(data.getCAN_LIN_Status());
}
1条答案
按热度按时间xwbd5t1u1#
首先需要按如下方式设置样式的填充样式:
注意原始代码中的
XSSFCellStyle
与CellStyle
:只有XSSFCellStyle
允许使用FillPatternType
枚举设置样式。然后,您还需要实际设置标题行的样式:
https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFRow.html#setRowStyle-org.apache.poi.ss.usermodel.CellStyle-