java - 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);
解决方案
您正在创建每一行两次,因此以前的内容可能会被覆盖。尝试重用同一行:
// 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);
推荐阅读
- sql - 在sql中用不同的标签标记值
- linux - 将外部变量注入 Linux shell 脚本并通过 SSH 执行代码的最安全方法是什么?
- typescript - 如何在模块中使构造函数私有但仍允许外部构造
- vhdl - Xilinx ISE 14.5 不要求提供 .ucf 文件,并且可能不会读取它
- json - NestJS - 在请求正文中接受多种 MIME 类型
- excel - 清除特定选定列表视图行的内容?
- google-apps-script - 什么会导致 Google Apps 脚本挂在 GetSheetByName 上?
- tsql - TSQL - PIVOT 但连接字段
- kubernetes - Kubernetes 部署文件中的 SAML 配置
- c# - 条纹 webhook 签名不存在