简体   繁体   English

在Excel中合并单元格BackGround或ForeGround

[英]Merged Cells BackGround or ForeGround In Excel

I am using Apache Poi for writing of data Into the Excel Sheet. 我正在使用Apache Poi将数据写入Excel工作表。 I have a requirement to merge cells and set background for the merged Cells. 我需要合并单元格并为合并的单元格设置背景。 I have a code for merged cell but I don't know how to set background or foreground for the Cell. 我有一个合并单元格的代码,但我不知道如何为单元格设置背景或前景。

// This code is the I used for merging cells and setting border for the cells //此代码是我用于合并单元格并为单元格设置边框的代码

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);

I want to have background for the merged cells . 我想为合并的单元格提供背景。 Please help me with a code for setting background or foreground for the merged cells. 请帮助我提供用于设置合并单元格的背景或前景的代码。

Try below steps. 请尝试以下步骤。 Let me know if you have any question. 如果您有任何问题,请告诉我。

Create CellStyle object from workbook ie workbook.createCellStyle set CellStyle properties like setAlignment,font,setFillForegroundColor,setFillPattern 从工作簿创建CellStyle对象,即workbook.createCellStyle设置CellStyle属性,例如setAlignment,font,setFillForegroundColor,setFillPattern

and then pass the object to cell.setCellStyle 然后将对象传递给cell.setCellStyle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM