Java- Apache POI XSSF - Custom RGB Cellbackground not working - java

I want to use a custom RGB color (38,38,38) for my cell backgroundcolor.
For that I use this code:
IndexedColorMap colorMap = wb.getStylesSource().getIndexedColors();
XSSFColor customtablegrey = new XSSFColor(new java.awt.Color(38,38,38), colorMap);
cellFormat1.setFillForegroundColor(customtablegrey.getIndex());
cellFormat1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
But all I get is a black background. Why and how can I change it?
Best Regards,
Christian

That's true. IndexedColorMap is useless for XSSFColors. XSSFColors are not indexed colors. In Office Open XML custom colors are stored as RGB directly in the XML. They are not stored in any kind of color map.
Setting XSSFColors as fill foreground color to cell styles is possible using XSSFCellStyle.setFillForegroundColor(XSSFColor color) only. It cannot be set using CellStyle.setFillForegroundColor(short fg).
So cellFormat1.setFillForegroundColor(customtablegrey) should work when cellFormat1 is a XSSFCellStyle.
How to use colors not in IndexedColors for Apache POI XSSF Excel? shows a complete example for how to use XSSFColor as cell fill color. Just tested using apache poi 5.1.0 too.
APACHE POI 4.1 : Set cell background color from hex code shows another complete example. Also tested and works using apache poi 5.1.0 too.

Related

Reading/filtering excel color font cell

I need to filter out excel colored text lines, I found that tFilterRow is not able to do it, is someone were able to resolve that using Java code or some other way?
BR,
Val

How to create page borders using Apache POI in excel files with Java?

I have been using Apache POI to create excel invoices/reports in my application.
The report uses cell styles which have different foreground and background colors, different font sizes, bordered cells and non-bordered cells. A new requirement needs me to put page borders. I have gone through most of the samples. I could only find sample code for cell level borders and not the page as a whole.
In OpenOffice/Libre Office excel applications this can be achieved by going to Format>Page>Border and selecting options as in the image below
page border
I thank you all in advance.

ConditionalFormatting Custom Colors in Apache POI XSSF

I am trying to set a custom color to fill a row using ConditionalFormatting in an XSSF sheet.
I've tried
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("MOD(ROW(),2)=0");
PatternFormatting fill1 = rule1.createPatternFormatting();
XSSFColor customColor = new XSSFColor(new byte[] {alpha, red, green, blue});
fill1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128))); //1
fill1.setFillForegroundColor(customColor); //2
And neither of those two work. It doesn't accept an XSSFColor.
I saw this Bug Report and tried both "hacks" listed there as working, and again, neither work. Also the doc was no help.
I understand that doing:
setFillForegroundColor(new XSSFColor(new java.awt.Color(red,green,blue)));
May work for a single cell.
Most solutions I have come across are a few years old and seem not to be working. I just need a away to be able to enter a custom RGB or Hex color as a
ConditionalFormatting in an XSSF sheet.
Any help?
Also I am running POI 3.9 to avoid parsing errors like:
this and this.
I've just created a for loop to create a new rule for each color (short). It phased out around 62 with a few different shades from there to 80.
Not exactly custom but at least I can attach the colors to a number.

Apache POI -- Font color is reverting back to default font color(black) on double clicking the cell in excel

I am working on generating excel in xlsx format programmatically using Apache POI 3.9.
For a particular column, all the cell values should be in blue font. My sheet has more than 1500 rows and minimum 50 columns.
I have a method which returns the blueFont color style. Here's the code:
public CellStyle createBlueFont(){
CellStyle fontStyle = workbook.createCellStyle(); // Creates new style
Font blueFont = workbook.createFont(); // Creates new font
blueFont.setColor(HSSFColor.BLUE.index);
fontStyle.setFont(blueFont);
fontStyle.setWrapText(true);
return fontStyle;
}
The above method is called each time the cell is created.
Excel is generated without any exception. But, when I double click on a cell which has a value filled with blue font, it reverts back automatically to black (default color).
I solved this issue by creating style and font globally.
I don't understand why my older approach is wrong.
Note : Am using SXSSF library for workbook and spreadsheet creation.
Could somebody please explain me where I was wrong in my previous code?
Thank you
Aswini J

Text direction in apache poi

How to make the text direction in Apache poi as Vertical.
I tried Vertical alignment. I am not able to get the desired results.
I don't want to rotate the text.
Something like :
T
E
X
T
You have to create a cellStyle, where You have a method setRotation(short rotation). Subsequently apply the cellstyle to Your cell.
docs, for setRotation
https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCellStyle.html#setRotation(short)

Categories

Resources