我正在阅读Excel表格。最初(直到rowid 30)我得到公式单元格的正确(CELL_TYPE_STRING)结果类型,但是从行ID 31开始,我收到了错误的(CELL_TYPE_NUMERIC)结果类型。
请参阅下面的日志:我使用====
作为分隔符来分隔每行的日志。列号1是要聚焦的点,其中对于rowID = 30,我得到type = String而对于rowId = 31,我得到type = Numeric,但是每行的第1列是string类型,因此我不知道如何获取字符串值
请参阅Excel工作表内容以获取与日志对应的行。
switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA:
cellWrapper.setCellValueFarmula(true);
switch (cell.getCachedFormulaResultType()) {
case Cell.CELL_TYPE_NUMERIC:
cellValue = String.valueOf(cell.getNumericCellValue());
logged.append("CELL_TYPE_FORMULA->CELL_TYPE_NUMERIC");
break;
case Cell.CELL_TYPE_STRING:
cellValue = "" + cell.getRichStringCellValue();
logged.append("CELL_TYPE_FORMULA->CELL_TYPE_STRING");
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = "" + cell.getBooleanCellValue();
logged.append("CELL_TYPE_FORMULA->CELL_TYPE_BOOLEAN");
break;
case Cell.CELL_TYPE_ERROR:
cellValue = "error! ";
logged.append("CELL_TYPE_FORMULA->CELL_TYPE_ERROR");
break;
default:
cellValue = null;
logged.append("case not captured" + " type fetched = " + cell.getCellType());
break;
}
break;
case Cell.CELL_TYPE_BLANK:
logged.append("CELL_TYPE_BLANK");
cellValue = null;
break;
case Cell.CELL_TYPE_ERROR:
cellValue = "error!";
logged.append("CELL_TYPE_ERROR");
break;
... some for cases below
Excel内容
Col1 Col2 Col3 Col4
[ACTIVE] - 21 - 1A - CALDRA - 305 . {Chitra Subramaniam - 9538924280}, {deepakag@yahoo.com} 12 Rs. 1,000
[ACTIVE] - 22 - 1A - CALDRA - 306 . {Parasad Parigi - 9538924280}, {deepakag@yahoo.com} 25 Rs. 1,000
[SEALED] - 23 - 1A - CALDRA - 401 . {Vivek - 9538924280}, {deepakag@yahoo.com} <empty cell> Rs. 1,075
日志
==================================================================================================
Cell @ row=30col=1 type = CELL_TYPE_FORMULA->CELL_TYPE_STRING Value=[ACTIVE] - 21 - 1A - CALDRA - 305 .
Cell @ row=30col=2 type = CELL_TYPE_FORMULA->CELL_TYPE_STRING Value= {Chitra Subramaniam - 9538924280}, {deepakag@yahoo.com}
Cell @ row=30col=3 type = CELL_TYPE_NUMERIC Value=12.0
Cell @ row=30col=4 type = CELL_TYPE_STRING Value=Rs. 1,000
==================================================================================================
Cell @ row=31col=1 type = CELL_TYPE_FORMULA->CELL_TYPE_NUMERIC Value=0.0
Cell @ row=31col=2 type = CELL_TYPE_FORMULA->CELL_TYPE_STRING Value= {Parasad Parigi - 9538924280}, {deepakag@yahoo.com}
Cell @ row=31col=3 type = CELL_TYPE_NUMERIC Value=25.0
Cell @ row=31col=4 type = CELL_TYPE_STRING Value=Rs. 1,000
==================================================================================================
Cell @ row=32col=1 type = CELL_TYPE_FORMULA->CELL_TYPE_NUMERIC Value=0.0
Cell @ row=32col=2 type = CELL_TYPE_FORMULA->CELL_TYPE_STRING Value= {Vivek - 9538924280}, {deepakag@yahoo.com}
Cell @ row=32col=3 type = CELL_TYPE_BLANK Value=null
Cell @ row=32col=4 type = CELL_TYPE_STRING Value=Rs. 1,075
==================================================================================================
那么即使值是数字,我怎样才能检索单元格结果值? 是否存在一种通用方法,无论字符串的类型是什么,都会以字符串形式给出结果值?
答案 0 :(得分:2)
Excel中的单元格可以有许多不同的类型。 POI会以Excel存储它为单位返回单元格值,这可能不是Excel显示它的方式。 (Excel可能有点神秘!)。
如果您希望POI尝试将单元格格式设置为与Excel中的格式相同,则无论类型如何,您要查找的类都是DataFormatter。您的代码将类似于:
DataFormatter fmt = new DataFormatter();
for (Row r : sheet) {
for (Cell c : r) {
CellReference cr = new CellRefence(c);
System.out.println("Cell " + cr.formatAsString() + " is " +
fmt.formatCellValue(c) );
}
}