使用Apache POI获取excel单元格显示值 [英] fetch excel cell display value using Apache POI
问题描述
下面显示了突出显示的单元格,该单元格显示为-
(连字符),但实际上内部具有0
值.
Below shows the highlighted cell which display as -
(hyphen) but actually has 0
value inside it.
这是具有以下自定义格式的公式单元格.
It's a formula cell with following custom formatting.
这是一个公式单元格,但是当结果为0
时,它将显示为-
.
It's a formula cell but when the result is 0
, it displays as -
.
我的要求是获取-
而不是0
.
My requirement is to fetch -
instead of 0
.
cell2Update = sheet.getRow(4).getCell(3);
cell2Update.setCellType(Cell.CELL_TYPE_NUMERIC);
System.out.println(cell2Update.getStringCellValue());//Exception
System.out.println(cell2Update.getNumericCellValue());//Display as 0
请帮助我解决这个疑问.我想显示在电子表格(-
)中显示的值.
Please help me with this doubt. I want to display the value which is displayed in Spreadsheet (-
).
推荐答案
使用DataFormatter
进行操作,如 DataFormatter 和 FormulaEvaluator :
Do using a DataFormatter
as shown in Getting the cell contents. And because the value might be result of a formula. Do using using a DataFormatter together with a FormulaEvaluator:
...
DataFormatter formatter = new DataFormatter();
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
...
cell2Update = sheet.getRow(4).getCell(3);
...
String value = formatter.formatCellValue(cell2Update, evaluator);
System.out.println(value);
...
这篇关于使用Apache POI获取excel单元格显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!