I'm using Apache POI to read an existing template excel file and would like to copy the existing styles in some header rows and apply them to new cells.
It seems like the existing formatting is not being applied (IE, Date, Currency, Percentage etc).
The code is pretty basic:
//read existing style
Row existingRow = sheet.getRow(headerRowIndex);
Cell existingCell = existingRow.getCell(0);
CellStyle currentStyle = existingCell.getCellStyle();
//apply date style here
Date date = StringUtil.toDate(map.get(column.getHeaderName()));
cell.setCellValue(date);
//apply previous style
cell.setCellStyle(currentStyle);
It does copy the font and background color etc but it seems like the formatting is lost (all cells have "general" formatting applied).
Also when I do this:
currentStyle.getDataFormat(); // always 0
So it makes me think that I'm not reading the formatting correctly. Any ideas on how to accomplish this?
OK I figured it out, it was my mistake. I was reading the style from the first cell in the row and applying it to all, instead of reading from each cell.
This fixes it
Cell existingCell = existingRow.getCell(i);
Related
We have an excel file which may be large.
We want to set cell format to 'text' to avoid issue like '=cmd', which may run 'cmd'.
Does there have any high efficient way to keep excel data format as text but keep other format like backgroud color or font-size?
To set the cell type, you can use:
Cell cell = row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellType(CellType.STRING);
This should avoid using formulas in the cells, unless you specifically set the type to CellType.FORMULA.
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 ?
#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
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.)
I have a problem with HSSFWorkbook object. I have a XLS template, where first row is dark gray, and next rows are light gray. During generation of the report, I want to change the background color of few cells (works perfectly) for red/blue. But after calling:
HSSFCellStyle style = workbook.createCellStyle();
style.setFillBackgroundColor(...)
or:
HSSFCellStyle style = workbook.getCellStyleAt(0) [default style of the workbook]
style.setFillBackgroundColor(...)
the styles of whole sheet is lost. The header becomes violet, and the rest cells becomes dark-dark-gray. I love violets, but I need my colours from the XLS not being changed.
In short - styles from my XLS template are not being preserved after I change style of one cell in the way described above.
I'm out of ideas. Please help.
Mateusz
I think the problem will be in your .xls spreadsheet itself. Styles that you chose on your template are incompatible with the current file format (and spreadsheet could be converted to .xlsx, but then you'd probably have to change your implementation to XSSFWorkbook).
You can check if styles are compatible by opening your template, making a minor change and save it - in your case warning should pop up that styles will be converted to the closest equivalents.
Solution: Edit your spreadsheet and pick compatible styles with your .xls format or upgrade to .xlsx format.
I have a generic problem with a not-so-generic requirement.
I have to convert an Excel file into PDF with a catch that all the formatting of each cell in the excel file has to be retained as it is. No change allowed. Like if a cell is formatted as Currency/Accounting then by default negative values are displayed in round braces e.g.(8.5) but when read from Java the value is -8.5. For numeric cell, value would be 0 but is displayed as hyphen (-).
Similarly for rest of the formatting types, the display changes.
As the cell has different cell types and the actual value in the cell is displayed according to the formatting applied on the cell, how to copy it in output PDF file?
Latest I learnt that DataFormatter is much useful in my case. So I wrote below code
DataFormatter df = new DataFormatter();
Cell c = row.getCell(i);
CellStyle style = c.getCellStyle();
cellvalue = df.formatRawCellContents(row.getCell(i).getNumericCellValue(), style.getDataFormat(), style.getDataFormatString());
One of the cell has formatting ($* #,##0.00);($* (#,##0.00);($* "-"??);(#_) (0x2c). If the value in the cell is 0 then ideally it should display "-" but it displays as 0.0 only.
Any help would be appreciated.
First install the pdf software in your local system then run this code in excel vb application. Give the name as you wish... that's it..
Sub expf()
Application.ActivePrinter = "PDF Complete on PDFC"
ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,""PDF Complete on PDFC"",,TRUE,,FALSE)"
End Sub
Using POI jar we can read excel . After reading for each row you can create PDF files using PDFBox. Also based on your requirements apply the styles for the pdf.
Using Apache Poi I'm exporting my table to an excel sheet. I extended ExcelExport class, override the getCellStyle method and customized my cell styles.
Now what i want is when a user edits any cell in the excel document i want that cell to change its color, so anyone later reviewing the document can easily see what has been changed.
Is there way to achieve this?
One solution could be to add conditional formatting to the cells use Apache POI's HSSFSheetConditionalFormatting.
For example, if the cell value written by your program is 5 then your conditional formatting could set the cell background to yellow if the value wasn't equal to 5