I am Using poi version : 3.14
I access an Excel (.xlsx) file
this.workbook = WorkbookFactory.create(new FileInputStream(f.getPath()));
this.workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
this.sheet = workbook.getSheetAt(0);
Here is a custom format found in .xlsx file applied to a cell. I want to display the cell value in my code.
As you can see the cell value visible in file is "031642153283700100". Also when i clic on this particular cell, value is changing to "42153283700100" (data without custom format applied on).
EDIT
Original cell type is CELL_TYPE_NUMERIC.
How can i display the formatted value "031642153283700100" in java code ?
I tried :
- cell.toString() => "42153283700100"
- cell.getNumericCellValue() => "42153283700100"
With previous cell type conversion :
cell.setCellType(Cell.CELL_TYPE_STRING);
- cell.getStringCellValue() => "42153283700100"
cell.getRichStringCellValue() => "42153283700100"
Old HSSFDataFormatter with formatCellValue(cell) method => "421532837001000316"
You could try this
For number formats you should use
CellGeneralFormatter
orCellNumberFormatter
.Example:
Edit
OK, let's have a more general example. The above will not work with dates (what I had known already) but also not with all number formats. The latter I thought the
CellNumberFormatter
should do but it does not. Unfortunately it will even produce errors with some proper number formats.In
Excel
a number format can contain up to 4 parts delimited with semicolon. The first part is for numbers greater than 0, the second part is for numbers lower than 0, the third part is for numbers equal 0 and the fourth part is for text.format > 0
;format < 0
;format = 0
;text
Since the
CellNumberFormatter
does not handle this properly, we should do it before using theCellNumberFormatter
.