简体   繁体   中英

Merged Cells BackGround or ForeGround In Excel

I am using Apache Poi for writing of data Into the Excel Sheet. 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

and then pass the object to cell.setCellStyle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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