I want to read data in Excel with Java, and data of cell in Excel got 2 types is NUMERIC
and STRING
. So when I want to read the data as a NUMERIC
, it only displays numbers 101125340004
, not like this 1.01E+11
because it is a telephone attribute. My code is working, but for some values (1%) still display floating-point number but they are actually integers, and I don't know why.
DataFormatter fmt = new DataFormatter();
if (cellType.toString().equals("NUMERIC"))
companyTel = fmt.formatCellValue(currentRow.getCell(8));
else
companyTel = currentRow.getCell(8).toString();
Output still got floating-point number in database
Version of Java Apache POI I'm using is 3.17.
Please tell me what wrong in my code above or how do I solve the problem? Thank you all.
Microsoft Excel
converts values having more than 11 digits in scientific notation when cell number formatGeneral
is used. So842223622111
leads to8,42224E+11
inExcel
cells having number formatGeneral
. If inExcel
the cell shall show842223622111
, then a special number format (0
) is needed instead ofGeneral
.You can read about available number formats in Excel for Office 365 and the special behavior of the
General
format for large numbers (12 or more digits).Libreoffice/OpenOffice Calc
will not do so. There842223622111
stays842223622111
in cells having number formatGeneral
.Now, if
apache poi
gets a cell containing842223622111
and having number formatGeneral
, thenDataFormatter
will format this likeExcel
would also do.If you wants
DataFormatter
should format this more likeLibreoffice/OpenOffice Calc
, then you could do:Using this approach, no changes in the
Excel
sheet are necessary. But of course the default behavior ofapache poi
'sDataFormatter
is changed. To avoid this, you could format the affected cells inExcel
using number format0
, which isNumber
without thousands separator and number of decimal decimal places = 0.But since you mentioned that this column contains telephone numbers, the most common solution would be formatting the whole column using number format
Text
(@
) . This "treats the content of a cell as text and displays the content exactly as you type it, even when you type numbers." So after that formatting was applied nothing will change to scientific notation any more.