我有以下代码,目标是让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);
答案 0 :(得分:2)
您正在创建两行,因此可能会覆盖之前的内容。尝试重复使用同一行:
// 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);