Reputation: 73
Im editing an xlsx file and Im trying to find some mistakes in numerical cells and make the bold. the numbers are big for instance: 13882004729568
After finding them and changing the font to bold (or changing foreground color) the cell type is changing to its default state and I get : b1.3882E+13 even after trying to change the type to numerical.
XSSFFont italic = wb.createFont();
italic.setFontName("Arial");
italic.setItalic(true);
CellStyle italicStyle = wb.createCellStyle();
italicStyle.setFont(italic);
cell.setCellStyle(italicStyle);
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
Am I doing something wrong ? Is it other way to do this ?
Upvotes: 2
Views: 2491
Reputation: 73
@maraca thx for the help what I did is :
XSSFDataFormat df = wb.createDataFormat();
italicStyle.setDataFormat(df.getFormat("##0"));
so the format change made the difference
Upvotes: 2
Reputation: 8743
Official documentation of setCellType
(link):
Set the cells type (numeric, formula or string). If the cell currently contains a value, the value will be converted to match the new type, if possible. Formatting is generally lost in the process however.
So, just switch the statements and it should work:
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
cell.setCellStyle(italicStyle);
Another possibility is that you run out of styles, because you create a new one each time. Create the style once and reuse it.
You can either get the style before changing the cell type or you have to set the format too:
italicStyle.setDataFormat(wb.createDataFormat().getFormat("0"));
(Just to show the priciple, don't create a new data format each time, reuse.)
Upvotes: 1