通过java读取Excel文件获取数据
获取单元格中的内容
public String getValue(Cell cell) {
String cellvalue = null;
if (cell != null) {
// 判断当前Cell的Type
switch (cell.getCellType()) {
// 如果当前Cell的Type为NUMERIC
case Cell.CELL_TYPE_NUMERIC:
case Cell.CELL_TYPE_FORMULA: {
// 判断当前的cell是否为Date
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是Date类型则,转化为Data格式
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellvalue = sdf.format(date);
}
// 如果是纯数字
else {
cell.setCellType(Cell.CELL_TYPE_STRING);
cellvalue = cell.getRichStringCellValue().getString();
}
break;
}
// 如果当前Cell的Type为STRING
case Cell.CELL_TYPE_STRING:
// 取得当前的Cell字符串
cellvalue = cell.getRichStringCellValue().getString();
break;
// 如果当前Cell的Type为BOOLEAN
case Cell.CELL_TYPE_BOOLEAN:
cellvalue = String.valueOf(cell.getBooleanCellValue());
break;
// 默认的Cell值
default:
cellvalue = null;
}
} else {
cellvalue = null;
}
if (cellvalue != null) {
cellvalue = cellvalue.equals("") ? null : cellvalue;
}
return cellvalue;
}