1、POI读取Excel时判断单元格数据类型:
CELL_TYPE_NUMERIC 数值型 0
CELL_TYPE_STRING 字符串型 1
CELL_TYPE_FORMULA 公式型 2
CELL_TYPE_BLANK 空值 3
CELL_TYPE_BOOLEAN 布尔型 4
CELL_TYPE_ERROR 错误 5
2、在jeecg中获取Excel中的值:
源码是这样的:jeecgframework/poi/excel/imports/CellValueServer.java
private Object getCellValue(String xclass, Cell cell, ExcelImportEntity entity)
{
if (cell == null) {
return "";
}
Object result = null;
if (("class java.util.Date".equals(xclass)) || ("class java.sql.Time".equals(xclass))) {
if (0 == cell.getCellType())
{
result = cell.getDateCellValue();
} else {
cell.setCellType(1);
result = getDateData(entity, cell.getStringCellValue());
}
if ("class java.sql.Time".equals(xclass))
result = new Time(((Date)result).getTime());
}
else if (0 == cell.getCellType()) {
result = Double.valueof(cell.getNumericCellValue());
} else if (4 == cell.getCellType()) {
result = Boolean.valueOf(cell.getBooleanCellValue());
} else {
result = cell.getStringCellValue();
}
return result;
}
3、在使用中发现,Excel中的整数在导入后,悄悄的加了小数点
为了去掉不该出现的小数点,修改源码中的格式转换部分,发现源码中直接使用的0和4数值去判断,不太了解什么意思,所以查了单元格数据类型附在文章的开始部分,0为数值型,在进行格式转化是,转为Double型了,所以悄悄的加上了小数点,这里为满足自己的业务需要,直接强转为int型,因为我的数值本身就是int型,修改后为
private Object getCellValue(String xclass, Cell cell, ExcelImportEntity entity)
{
if (cell == null) {
return "";
}
Object result = null;
if (("class java.util.Date".equals(xclass)) || ("class java.sql.Time".equals(xclass))) {
if (0 == cell.getCellType())
{
result = cell.getDateCellValue();
} else {
cell.setCellType(1);
result = getDateData(entity, cell.getStringCellValue());
}
if ("class java.sql.Time".equals(xclass))
result = new Time(((Date)result).getTime());
}
else if (XSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
result = (int)cell.getNumericCellValue();
} else if (XSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
result = Boolean.valueOf(cell.getBooleanCellValue());
} else {
result = cell.getStringCellValue();
}
return result;
}