仅xls2003
//获取单元格内容(支持日期及小数)
private String getCellValue(HSSFCell cell) {
if(cell == null){
return null;
}
String cellValue = "";
//like12 modified bug,20171124,不能保留小数
//DecimalFormat df = new DecimalFormat("#");//不保留小数
DecimalFormat df = new DecimalFormat("#.##");//最多保留x位小数
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
cellValue = cell.getRichStringCellValue().getString().trim();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
// like12 add,20180622,支持日期格式
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date d = cell.getDateCellValue();
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
cellValue = df2.format(d);
}
// 数字
else {
cellValue = df.format(cell.getNumericCellValue()).toString();
}
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
break;
case HSSFCell.CELL_TYPE_FORMULA:
cellValue = cell.getCellFormula();
break;
default:
cellValue = "";
}
return cellValue;
}
兼容xls2003及xlsx2007
//获取单元格内容(支持日期及小数)(兼容xls2003及xlsx2007)
private String getCellValue(Cell cell) {
if(cell == null){
return null;
}
String cellValue = "";
//like12 modified bug,20171124,不能保留小数
//DecimalFormat df = new DecimalFormat("#");//不保留小数
DecimalFormat df = new DecimalFormat("#.##");//最多保留x位小数
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
cellValue = cell.getRichStringCellValue().getString().trim();
break;
case Cell.CELL_TYPE_NUMERIC:
// like12 add,20180622,支持日期格式
if (DateUtil.isCellDateFormatted(cell)) {
Date d = cell.getDateCellValue();
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
cellValue = df2.format(d);
}
// 数字
else {
cellValue = df.format(cell.getNumericCellValue()).toString();
}
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
break;
case Cell.CELL_TYPE_FORMULA:
cellValue = cell.getCellFormula();
break;
default:
cellValue = "";
}
return cellValue;
}
//获取文件的后缀名,不能是exe/dll/com/bat
String fileName = file.getOriginalFilename(); // 获取文件名
String suffixName = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();// 获取文件的后缀名,并转化为小写
if(suffixName.equals(".exe")||suffixName.equals(".dll")||suffixName.equals(".com")||suffixName.equals(".bat")) {
result.put("code", 999);
result.put("msg", "文件格式错误");
return result.toJSONString();
}
//兼容xls2003及xlsx2007
Workbook wb = null;
if(".xls".equals(suffixName)){
wb = new HSSFWorkbook(file.getInputStream());
}else if(".xlsx".equals(suffixName)){
wb = new XSSFWorkbook(file.getInputStream());
}else{
result.put("code", 999);
result.put("msg", "不支持的文件后缀");
return result.toJSONString();
}
jar包从3.17升级到5.2.2后
解决jar升级到5.2.2后报”枚举 switch case 标签必须为枚举常量的非限定名称“的问题
//like12 modified,20220818,解决jar升级到5.2.2后报”枚举 switch case 标签必须为枚举常量的非限定名称“的问题
//获取单元格内容(支持日期及小数)(兼容xls2003及xlsx2007)
private String getCellValue(Cell cell) {
if(cell == null){
return null;
}
String cellValue = "";
//like12 modified bug,20171124,不能保留小数
//DecimalFormat df = new DecimalFormat("#");//不保留小数
DecimalFormat df = new DecimalFormat("#.##");//最多保留x位小数
switch (cell.getCellType()) {
case STRING://Cell.CELL_TYPE_STRING
cellValue = cell.getRichStringCellValue().getString().trim();
break;
case NUMERIC://CELL_TYPE_NUMERIC
// like12 add,20180622,支持日期格式
if (DateUtil.isCellDateFormatted(cell)) {
Date d = cell.getDateCellValue();
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
cellValue = df2.format(d);
}
// 数字
else {
cellValue = df.format(cell.getNumericCellValue()).toString();
}
break;
case BOOLEAN://Cell.CELL_TYPE_BOOLEAN
cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
break;
case FORMULA://Cell.CELL_TYPE_FORMULA
cellValue = cell.getCellFormula();
break;
default:
cellValue = "";
}
return cellValue;
}
参考:
POI 操作 Excel 中 HSSFCell.CELL_TYPE_STRING 等无定义解决方法_竹林幽深的博客-CSDN博客_cell_type_stringcell.getCellType() is deprecated 与 HSSFCell.CELL_TYPE_NUMERIC is deprecated的替代方法_神蜗牛的博客-CSDN博客(后面这个有点误差5.2.2版本找不到cell.getCellTypeEnum()而依然是cell.getCellType())