How to set existing excel file data format 'text' using java - java

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.

Related

Custom number format is gone after updating cells

I'm using Java API of Google Spreadsheets to modify the value of a cell in a spreadsheet. Doing it this way:
CellEntry cell = ...
cell.changeInputValueLocal(newInputValue);
cell.update();
My problem is that the cell has a custom number format (# ###0 "colli"), and it's gone after I write the cell programmatically. I want to write a number into the cell (200). The cell's number format is set to "Automatic" after this operation.
What am I doing wrong?

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.

Apache POI - Writing to Excel template without overwriting existing cell formatting

I am using Apache POI to write data to an Excel template. The template only contains headers on the first row, but I also applied specific styles to the entire column (e.g. Accounting and Percentage).
It would be nice to use these formats when I write data to the new cells. But if I use the createRow and createCell methods, the cell format is overwritten and I get General for all the cells. If I try to use getRow and getCell instead, I run into NullPointerExceptions retrieving the blank cell.
Is there a way to use the pre-existing cell formatting saved in the template? Or am I stuck setting the data format myself through the API?
Thanks for your help.
If you have applied specific styles to an entire column, then you can retrieve that CellStyle with Sheet's getColumnStyle method, passing it the 0-based column index. It retrieves a normal CellStyle object that can be used anywhere else CellStyles are accepted, such as in Cell's setCellStyle method.
For avoiding the NullPointerException, both getRow and getCell may return null if the row or cell doesn't exist, respectively. You will need to call createRow and/or createCell to create the Cell, on which you can always call setCellStyle.
cell.setCellStyle(sheet.getColumnStyle(index) works well.

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