java - Apache POI 单元值未出现

标签 java excel apache-poi

我有以下代码,目标是将“ 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/

相关文章:

Python/ Pandas : writing multiple Dataframes to Excel sheets using a "for-loop"

xml - 如何更改 Excel 的默认 XML 输出编码?

java - 使用Java apache POI填充Excel行中的背景颜色

java - 使用 Apache POI 获取 Excel 文件中的列名称

java - Eclipse for Java 显示多个错误

java - 为什么 ExecutorService 没有被频繁执行

arrays - 使用 excel vba 通过分隔符分割字符串同时忽略所述分隔符的某些实例的最有效方法

java - 在java中读取文件上传的内容

java - java 中的 nextChar()

java - Spring Integration 是否有 JSR-303 验证过滤器实现?