【问题标题】:Merged Cells BackGround or ForeGround In ExcelExcel中合并单元格的背景或前景
【发布时间】: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


【解决方案1】:

尝试以下步骤。如果您有任何问题,请告诉我。

从工作簿创建 CellStyle 对象,即 workbook.createCellStyle 设置 CellStyle 属性,如 setAlignment、font、setFillForegroundColor、setFillPattern

然后将对象传递给 cell.setCellStyle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2017-06-19
    相关资源
    最近更新 更多