【发布时间】:2019-06-16 16:09:56
【问题描述】:
我正在使用 Apache Poi 将数据写入 Excel 表。 我需要合并单元格并为合并的单元格设置背景。我有合并单元格的代码,但我不知道如何为单元格设置背景或前景。
// 这段代码是我用来合并单元格和设置单元格边框的代码
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowNum,rowNum,colNum,colNum);
sheet.addMergedRegion(cellRangeAddress);
RegionUtil.setBorderTop(CellStyle.BORDER_MEDIUM, mergedCell, sheet, workBook);
RegionUtil.setBorderBottom(CellStyle.BORDER_MEDIUM, mergedCell,sheet, workBook);
RegionUtil.setBorderLeft(CellStyle.BORDER_MEDIUM, mergedCell, sheet, workBook);
RegionUtil.setBorderRight(CellStyle.BORDER_MEDIUM, mergedCell,sheet, workBook);
我想要合并单元格的背景。请帮助我为合并的单元格设置背景或前景的代码。
【问题讨论】:
-
请参阅stackoverflow.com/questions/50712340/…,了解创建具有合并单元格和设置不同背景颜色的复杂 Excel 表格的示例。
-
感谢 Axel Richter 的回复。我检查了您提供的链接,但我没有找到
CellUtil.setCellStyleProperty(cell,properties)方法。我想这可能是 jar 文件问题我使用的是 3.9 poi jar,我需要更新。 -
我没有用过
CellUtil.setCellStyleProperty,而是CellUtil.setCellStyleProperties。但你是对的,apache poi 3.9没有那个方法,它只有setCellStyleProperty(Cell cell, Workbook workbook, String propertyName, Object propertyValue)。但是apache poi 3.9现在已经 6 岁多了。对于apache poi这样的项目来说,那是永恒的。我建议始终使用最新的稳定版本,即apache poi 4.0.1。 -
link 完成阿克塞尔
标签: java excel apache-poi