【发布时间】:2011-09-07 13:41:44
【问题描述】:
我正在使用 Apache POI 将数据导出到 Excel 工作表中。它工作正常。问题是我需要在生成 excel 表时为 excel 表中的几行应用黄色背景色。请教我如何在生成时为 excel 表的行应用背景颜色。
谢谢, 雷迪
【问题讨论】:
标签: java apache-poi
我正在使用 Apache POI 将数据导出到 Excel 工作表中。它工作正常。问题是我需要在生成 excel 表时为 excel 表中的几行应用黄色背景色。请教我如何在生成时为 excel 表的行应用背景颜色。
谢谢, 雷迪
【问题讨论】:
标签: java apache-poi
直接来自official guide:
// Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
row.setRowStyle(style);
【讨论】:
//Opening excel file
FileInputStream inputStream = new FileInputStream(new File(excelFileLocation));
XSSFWorkbook resultWorkbook = new XSSFWorkbook(inputStream);
XSSFSheet resultSheet = resultWorkbook.getSheet(sheetName);
//Applying style
XSSFRow sheetrow = resultSheet.getRow(1); // Row number
XSSFCellStyle style = resultWorkbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
sheetrow.getCell(0).setCellStyle(style);//Cell number
//Saving file
FileOutputStream outFile =new FileOutputStream(new File(excelFile));
resultWorkbook.write(outFile);
outFile.close();
【讨论】: