Changing the cell color to specific defined color

Ezz Eddin Othman :

I want to change the color of the cell to a color, that I define with `java.awt.Color[r=255,g=255,b=255] I used the code:

Color sColor = new Color(200, 0, 0);
XSSFColor userColor = new XSSFColor(sColor);
try
{
    CellStyle style = wb.createCellStyle();
    Font font = wb.createFont();
    font.setColor(userColor.getIndexed());
    style.setFont(font);
    cell.setCellStyle(style);
}
catch (NumberFormatException | NullPointerException ex) 
{
    //Handle NumberFormat and NullPointer exceptions here    
}

the color always remains black.

And the output of System.out.print(userColor); is: >(org.apache.poi.xssf.usermodel.XSSFColor@2e1b928). Actually, it changes depending on the value that I type in new Color (200,0,0);

but the output of System.out.print(userColor.getIndexed()); is always: (0).

any tips?

RaceYouAnytime :

If you instantiate Font as XSSFFont, you can use the method

XSSFFont.setColor(XSSFColor) // Takes a color object, not a short!

See the documentation here: https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFFont.html#setColor-org.apache.poi.xssf.usermodel.XSSFColor-

So try this (it worked for me in a quick test):

Color sColor = new Color(200, 0, 0);
XSSFColor userColor = new XSSFColor(sColor);

CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(userColor);
style.setFont(font);
cell.setCellStyle(style);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=110394&siteId=1