首页 > 解决方案 > Apache POI 单元格值未出现

问题描述

我有以下代码,目标是让 Parking、Free 和 Units 在一列中下降,并且它们旁边是纯色。但是,当我运行它时,我得到了正确单元格中的颜色,但左侧没有文本。我尝试重新排序 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);

标签: javaexcelapache-poi

解决方案


您正在创建每一行两次,因此以前的内容可能会被覆盖。尝试重用同一行:

// 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);

推荐阅读