How can I read numeric strings in Excel cells as string (not numbers)?
To read numeric values in Excel cells as strings in Java, leverage the DataFormatter
class of Apache POI. Here is a basic example:
This code totally ignores potential numerical characteristics of the original Excel cells and outputs them as pure text, precisely preserving their visual representation.
Beating .toString()'s limits
To avoid blurring distinctions between values like "2" and "2.0", refrain from using the basic .toString()
method. Instead, employ the DataFormatter
utility:
This will ensure correct handling of values that need to remain distinctly formatted, such as account numbers, identification codes, etc.
Strategy for formulas
Excel formulas pose an extra layer of complexity. A combination of a FormulaEvaluator
and DataFormatter
is needed:
This technique accurately converts formulas to their presented values, not their underlying formula strings.
Workbook type: Know your tools!
The correct Workbook
implementation (whether HSSF for .xls files or XSSF for .xlsx files) impacts the FormulaEvaluator
selection—either HSSFFormulaEvaluator
or XSSFFormulaEvaluator
.
For mighty large Excel files, you'll want the SXSSFWorkbook
streaming reader from Apache POI. It helps to keep memory use in check.
Dialing it up with NumberToTextConverter
Revisionist historians, focus! For historically accurate value representations, Apache POI's NumberToTextConverter
utility is your best bud:
This method preserves the exact original numeric state, whether an integer, a decimal, a floating-point weirdo, or something in scientific notation. Zero distortion!
Cell type iteration
Being aware of the cell type prior to conversion is vital. So, you'd always check getCellType()
:
Was this article helpful?