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?
Related
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'm trying to check if the text in cell (Excel file) is bold or not.
For now I'm using :
final CellStyle cellStyle = row.getCell(cellIndex).getCellStyle().toString();
This doesn't give any information about bold text or not. But I've seen that getCellStyle() possesses several methods like "getFontIndex()". Does it exist one to know if the text is bold or not ?
This is a bit convoluted, but it is the Font that contains the Bold Attribute, not the CellStyle. You can retrieve it from the CellStyle in a roundabout way though.
Workbook book = row.getSheet().getWorkbook();
CellStyle style = row.getCell(cellIndex).getCellStyle();
int fontIndex = style.getFontIndex();
Font font = book.getFontAt(fontIndex);
if (font.getBold()) {
// if you are here, the font is bold
}
This will tell you if the CellStyle contains a bold font. The text in the cell can be a RichTextString though, and it can be a totally different font than what is specified in the CellStyle. The RichTextString support is not complete though, so you will have to do some casting to retrieve the font from that if necessary.
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.
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()