I have Excel file and some contents, coming from external source(database, file, whatever). I've encountered problems with numeric cells in Excel -getNumericValue returns double value for any cell, no matter how value is formatted in it. External source returns string representations of numbers, i.e. it may return "301" and "301.0" and these are different values, and i don't know which type may be in which case. But if i have "301" in Excel file, getNumericValue method will always return "301.0", so if i need to compare values from external source and from excel, "301" in excel will be different from "301" from external source, as they will have different string representations.
Long story short, can you show me the working way to get string representation of cell value as it looks in E开发者_如何学Goxcel? I have only HSSF* classes available for using.
You want the DataFormatter class. The handles formatting the contents of a cell based on the formatting rules applied to it. It should give you basically what Excel displays
basing from another thread you can probably set the cell's type to string first before getting its values
cell.setCellType(Cell.CELL_TYPE_STRING);
I've tried this approach when getting a number with more than 15 decimal places and it works like a charm.
Dont compare String, compare double instead:
String excelDouble=301.0;
String external="301";
Double externalDouble=Double.parseDouble(external);
externalDouble.equals(excelDouble); //return true
精彩评论