APACHE POI 4.1 : Set cell background color from hex code

Tommolo :

I've tried different solutions posted on stack overflow to apply a background color to an Apache POI generated cell, but nothing worked.

I'm doing something like:

Workbook workbook = new XSSFWorkbook(); 
Sheet sheet = workbook.createSheet(sheetName);

XSSFCellStyle cellStyle = ((XSSFCellStyle) workbook.createCellStyle());

if (styleObject.getBgColor() != null) {
    java.awt.Color javaBdgColor = java.awt.Color.decode(voceStyle.getBgColor()); // this is #FFF000
    XSSFColor bgColor = new XSSFColor(javaBdgColor, new DefaultIndexedColorMap());
    cellStyle.setFillForegroundColor(bgColor.getIndex());
    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}

Row newRow = Rowsheet.createRow(0);
Cell newCell = newRow.createCell(0);
newCell.setCellStyle(cellStyle);

// write file
String pathFileExport = buildPathExportFile("test-export");
FileOutputStream fileOut = new FileOutputStream(pathFileExport);
workbook.write(fileOut);
fileOut.close();

//close workbook
workbook.close();

return Paths.get(pathFileExport);

I think everything is ok in my code but every cell styled like that will result in a black background. black-cells

I have some doubts about "DefaultIndexedColorMap" instance that's during debugging results without fields:

code debugger

At this point, I'm not sure about what to do to solve. Everyone in other posts seems to get things working but I'm still getting dark backgrounds instead of yellow.

Any suggestions? Thanks in advance!

Axel Richter :

As the other answer tells, usage of setFillForegroundColor(XSSFColor color) instead of using indexed colors is necessary in XSSFCellStyle when it comes to customized colors. But usage of indexed colors from org.apache.poi.ss.usermodel.IndexedColors is possible in XSSF too. And this will be the most compatible way if using customized colors is not necessary.

But also creating the XSSFColor from java.awt.Color should be avoided. The constructor XSSFColor(java.awt.Color clr, IndexedColorMap map) is marked "TEST ONLY". And java.awt.Color will not be available in some circumstances.

So if the need is "set cell background color from hex code" and the hex code is in a String, then org.apache.commons.codec.binary.Hex can be used to get an byte[] array from that String. Apache commons codec is one of apache poi's dependencies already. Then constructor XSSFColor(byte[] rgb, IndexedColorMap colorMap) can be used. IndexedColorMap has no usage until now. So it can be set null. If IndexedColorMap gets any usage later, then the code has to be adjusted anyway.

Example:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import org.apache.commons.codec.binary.Hex;

class CreateXSSFColor {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   String rgbS = "FFF000";
   byte[] rgbB = Hex.decodeHex(rgbS); // get byte array from hex string
   XSSFColor color = new XSSFColor(rgbB, null); //IndexedColorMap has no usage until now. So it can be set null.

   XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle();
   cellStyle.setFillForegroundColor(color);
   cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

   Sheet sheet = workbook.createSheet(); 
   Row row = sheet.createRow(0);
   Cell cell = row.createCell(0);
   cell.setCellValue("yellow");
   cell.setCellStyle(cellStyle);

   workbook.write(fileout);
  }

 }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set background color of a cell using apache poi 4.1.0

Java get the excel cell background color from Apache poi

XSSFColor with Apache POI 4: cell background color gets overwritten by another cell background color

Apache POI XSSFColor from hex code

Apache-POI : How to set background color of a cell when creating spreadsheet?

how to set hex color code for background

Wrong coping of cell's background color from one XLSX file to another using Apache POI

Apache-POI: Cell Background Color doesn't work

Set an automatic color background depending on the HEX value in the cell?

POI delete background color of cell

Java Apache Poi, how to set background color and borders at same time

Can't set background color because of HTML Hex code

Set Background Color using HEX

how to set background color of a cell using apache pio 4.1.0

xamarin set view background color from a hex value

Dispaly hex color as background color of textbox after conversion from rbg value to hex code using javascript

Apache poi cell style with color and right alignment

Java apache poi: excel cell color

Apache POI background color style seems not working

Set background cell color on CListCtrl

Set background color in table cell

Apache POI set cell border is not working

Set Windows Forms Background Color To Hex Value

Jquery object set background color in hex

How to get the background color code of an element in hex?

Not able to set custom color in XSSFCell Apache POI

Reading a cell from Excel using Apache POI

Kivy Label background color from hex

Getting Excel cell background themed color as hex with openpyxl