要想根据不同类型excel表格获取其数据,就要先判断其表格类型
poi-api种方法:
getCellType
public int getCellType() Return the cell type.Specified by:
getCellType in interface CellReturns: the cell typeSee Also:Cell.CELL_TYPE_BLANK, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING, Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_ERROR --------------------------------------------------------------------------------
CELL_TYPE_NUMERIC double static final int CELL_TYPE_NUMERICNumeric Cell type (0) --------------------------------------------------------------------------------
CELL_TYPE_STRING String static final int CELL_TYPE_STRINGString Cell type (1) --------------------------------------------------------------------------------
CELL_TYPE_FORMULA 带excel函数 static final int CELL_TYPE_FORMULAFormula Cell type (2) --------------------------------------------------------------------------------
CELL_TYPE_BLANK static final int CELL_TYPE_BLANKBlank Cell type (3) --------------------------------------------------------------------------------
CELL_TYPE_BOOLEAN Boolean static final int CELL_TYPE_BOOLEANBoolean Cell type (4) --------------------------------------------------------------------------------
CELL_TYPE_ERROR static final int CELL_TYPE_ERRORError Cell type (5)
--------------------------------------------------------------------------------
例子:
XSSFCell xssfcell = (XSSFCell) row.getCell(0); switch(xssfcell.getCellType()){ case NUMERIC://double double val1 = xssfcell.getNumericCellValue(); break; case STRING://String String val2 = xssfcell.getStringCellValue(); break; case FORMULA://表格内容是通过excel函数得到 XSSFRichTextString val3 = xssfcell.getRichStringCellValue(); String val4 = String.valueOf(val3); break; case BLANK://空白表格 break; case BOOLEAN://boolean boolean val6 = xssfcell.getBooleanCellValue(); break; case ERROR://错误单元格 String val7 = xssfcell.getErrorCellString(); break; default: break; }