Apache poi HSSFWorkbook overwrites styles from the XLS template - java

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.

Related

How to change color of a cell of an existing excel (xlsx or xls) in java

I have requirement in that i have to read and validate the excel file and insert to data base, initially i am reading the file and inserting it to database, if the data already there in database, i am returning row and column numbers of the duplicates from database, i need to change the color of those cells based on the row and column numbers.
can any one please help me to solve this problem. or can suggest any idea how to read/ write and validate excel file, (validation in the sense i need to compare the data in the excel file with the data in the database).
currently i am using apache poi
This answer is for .xls; the API for xlsx is very similar, though.
Reading the Excel file is easy, open a FileInputstream for the file, then create a HSSFWorkbook
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
To change to color of some cells, you first have to create a Style with this color (changing the style of a cell will also change some other properties, like the font etc., so things might get a bit messy here, but that's how excel works)
colorStyle = workbook.createCellStyle();
colorStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
colorStyle.setFillForegroundColor(HSSFColor.RED.index);
Now you have to loop through the rows in all workbooks, identify the duplicates, and change the style of those cells:
cell.setCellStyle(colorStyle);
Finally, you write the workbook to a new file, through a FileOutputStream
workbook.write(outputStream);

XLS to PDF conversion without losing formatting

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.

How to apply border to a comple hssfsheet in java

i am using apache poi library to create a workbook, in the sheet i want to add border to my whole content table , there is option available to create border for a single cell, is there any way so that i can apply the border to whole sheet content
Use this default method, just create a HSSFCellStyle and save that object, then in the loop whenever you are writing the excel, just use this style object directly passed to the method
setDefaultColumnStyle(),, method doc tells you..
public void setDefaultColumnStyle(int column,CellStyle style)
Sets the default column style for a given column. POI will only apply this style to new cells added to the sheet.
Specified by:
setDefaultColumnStyle in interface Sheet
Parameters:
column - the column index
style - the style to set
Just let me know if its working for you?

Apache POI preserve existing Excel formatting style

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);

Apache POI excel cell style update

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

Categories