我有以下代码,目标是将“ parking ”、“免费”和“单位”放在一列中,并在它们旁边显示纯色。然而,当我运行它时,我在正确的单元格中得到了颜色,但左侧没有文本。我尝试重新排序 set 语句,但没有任何效果。
CellStyle park=workbook.createCellStyle();
park.setFillForegroundColor(IndexedColors.BLACK.getIndex());
park.setFillPattern(FillPatternType.SOLID_FOREGROUND);
CellStyle free=workbook.createCellStyle();
free.setFillForegroundColor(IndexedColors.GREEN.getIndex());
free.setFillPattern(FillPatternType.SOLID_FOREGROUND);
CellStyle unit=workbook.createCellStyle();
unit.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
unit.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Sheet key=workbook.createSheet("Key");
Cell cell11=key.createRow(1).createCell(1);
cell11.setCellValue("Parking");
Cell cell21 = key.createRow(2).createCell(1);
cell21.setCellValue("Free");
Cell cell31 = key.createRow(3).createCell(1);
cell31.setCellValue("Units");
Cell cell12=key.createRow(1).createCell(2);
cell12.setCellStyle(park);
Cell cell22=key.createRow(2).createCell(2);
cell22.setCellStyle(free);
Cell cell32=key.createRow(3).createCell(2);
cell32.setCellStyle(unit);
最佳答案
您将每行创建两次,因此之前的内容可能会被覆盖。尝试重复使用同一行:
// Cell styles
CellStyle park = workbook.createCellStyle();
park.setFillForegroundColor(IndexedColors.BLACK.getIndex());
park.setFillPattern(FillPatternType.SOLID_FOREGROUND);
CellStyle free = workbook.createCellStyle();
free.setFillForegroundColor(IndexedColors.GREEN.getIndex());
free.setFillPattern(FillPatternType.SOLID_FOREGROUND);
CellStyle unit= workbook.createCellStyle();
unit.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
unit.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Sheet key=workbook.createSheet("Key");
Row row = key.createRow(1);
Cell cell11 = row.createCell(1);
cell11.setCellValue("Parking");
Cell cell12 = row.createCell(2);
cell12.setCellStyle(park);
row = key.createRow(2);
Cell cell21 = row.createCell(1);
cell21.setCellValue("Free");
Cell cell22 = row.createCell(2);
cell22.setCellStyle(free);
row = key.createRow(3);
Cell cell31 = row.createCell(1);
cell31.setCellValue("Units");
Cell cell32 = row.createCell(2);
cell32.setCellStyle(unit);
关于java - Apache POI 单元值未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50649713/