I am generating HSSF style workbook.
After creating new CellStyle my grid lines disappear. I don't want to put BorderStyle.DORDER_THIN because that is not like just grid line. I has bigger edges.
I have tried in sheet currentSheet.setDisplayGridlines(false)
my code:
HssfCellStyle hssfCellStyle = hssfWorkbook.createCellStyle
Font hssfFont = this.getFontFor(rowCellStyle.cellFont)
hssfCellStyle.setFont(hssfFont)
hssfCellStyle.setFillForegroundColor(rowCellStyle.backgroundColor.hssfColor.getIndex)
hssfCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
so this makes disappear gdi lines
SOLUTION:
I was little bit stupid.
Default grid line is not border, so if you put some color then that default grid line is filled with that color.
Default Cell color is AUTOMATIC -> what means TRANSPARENT and because of that default grid can been seen.
default color = new HSSFColor.AUTOMATIC()
Related
I'm trying to copy a cell style (background color, format, etc) and set that to another cell.
Using this methods:
CellStyle style = originalCell.getCellStyle();
newCell.setCellStyle(style);
CellStyle cellstyle = workbook.createCellStyle();
cellstyle.cloneStyleFrom(originalCell.getCellStyle())
But somehow the new cell style has different background color then the original one.
This is how my cell backgrounds looks like
Thats the result
Any suggestion why is that?
I use the following code to set background color for cells:
XSSFCellStyle cellStyle = (XSSFCellStyle) excelStyle.getCellStyle();
cellStyle.setFillForegroundColor(new XSSFColor(java.awt.Color.decode("#FFFF99")));
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
But it is not working for cells which are merged, it always becomes black no matter of what I try to set.
It is working only if I set a predefined color like:
cellStyle.setFillForegroundColor(IndexedColors.CORAL.index);
The color is defined by user so I can't use IndexedColors. Also, I can't create a custom palette color (as explained here) because my workbook is of type SXSSFWorkbook, not HSSFWorkbook.
How to set background color for merged cells?
I have tried below steps and working for me.
Create a sheet
Create a row
Create style with custom colors using RGB values
Loop for creating cell > create cell and add value to cells 0 which will appear for subsequent merged cells, apply style to every cell,
Finally merge cells > lets say you want to merge cell 0 to cell 3 of row 0 use sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
After lot of debugging I found issue which was causing the merged cell to be always black was below line.
CellUtil.setAlignment(sheetSummary.getRow(0).getCell(4), HorizontalAlignment.CENTER_SELECTION);
Im trying to append a cell in a sheet, for example the cell 1A has a border and filled with color grey and I want to insert a string "hello", but using
cell.setCellValue("hello");
destroy the cell format or rather make the cell format in default mode. I know how to use the
CellStyle cs = workbook.createCellStyle();
method but in my project I'm inserting many different data with different cell format. I googled it and no luck finding an answer.
Is there another way to solve my problem?
To elaborate my problem.
In my sheet in 1A I have a cell format
Cell format (fill with color grey and have thin border)
but when I use
cell.setCellValue("hello");
it makes the cell's format become default but I want my cell to become like this without using CellStyle cs = workbook.createCellStyle();
cell I want
Is there a way to this?
I believe you have an excel workbook and you wanna append data to it. If you wanna keep the existing format and insert data to it you can do that using below code
cell = sheet.getRow(0).getCell(0);
cell.setCellValue("Hellooooo");
but if you are create a new cell by using
cell = sh.createRow(0).createCell(0);
then you're destroying the current format, in that case you have to create all required cellStyle all over again.
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
Is there any way to write a cell with the "Vertical" text orientation available in Apache POI? I can only find a method setRotation that rotates the entire text, rather than have it as Excel displays it when the "Vertical" option is applied. Visually this will look like:
this text
becomes
t
h
i
s
t
e
x
t
As per the HSSFCell.setRotation(short)
set the degree of rotation for the text in the cell
rotation - degrees (between -90 and 90 degrees, or 0xff for vertical)
So, you'll first need to create a (single, workbook-wide) cell style with that on:
CellStyle styleVertical = wb.createCellStyle();
styleVertical.setRotation(0xff);
Then apply that to your cell
Cell cell = row.createCell(0);
cell.setCellValue("this text");
cell.setCellStyle(styleVertical);
CellStyle cs = wb.createCellStyle();//where 'wb' is the workbook
cs.setWrapText(true);
cell.setCellStyle(cs);//where 'cell' is the cell you wish to edit
After that, the standard \n can be used to make a new line in the cell.