一、错误原因:jar包版本更新,官方改动;
二、解决方案:导入CellType包import org.apache.poi.ss.usermodel.CellType;使用CellType.STRING代替HSSFCell.CELL_TYPE_STRING
三、旧版代码:
/**
* excel值处理
* @param hssfCell
* @return
*/
public static Object getXSSFValue(XSSFCell hssfCell) {
if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
return hssfCell.getNumericCellValue(); //数字
}else if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
return hssfCell.getBooleanCellValue(); //boolean
}else if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_ERROR){
return hssfCell.getErrorCellValue(); //故障
}else if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_FORMULA){
return hssfCell.getCellFormula(); //公式
}else if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
return ""; //空值
}else {
return hssfCell.getStringCellValue(); //字符串
}
}
/**
* excel值处理
* @param hssfCell
* @return
*/
public static Object getValue(Cell hssfCell) {
if(hssfCell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC) {
return hssfCell.getNumericCellValue(); //数字
}else if(hssfCell.getCellType()==XSSFCell.CELL_TYPE_BOOLEAN) {
return hssfCell.getBooleanCellValue(); //boolean
}else if(hssfCell.getCellType()==XSSFCell.CELL_TYPE_ERROR){
return hssfCell.getErrorCellValue(); //故障
}else if(hssfCell.getCellType()==XSSFCell.CELL_TYPE_FORMULA){
return hssfCell.getCellFormula(); //公式
}else if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
return ""; //空值
}else {
return hssfCell.getStringCellValue(); //字符串
}
}
四、新版代码:
/**
* excel值处理
*
* @param hssfCell
* @return
*/
public static Object getXSSFValue(XSSFCell hssfCell) {
Object result = null;
CellType cellType = hssfCell.getCellType();
switch (hssfCell.getCellType()) {
case NUMERIC: //数字
result = hssfCell.getNumericCellValue();
break;
case BOOLEAN: //Boolean
result = hssfCell.getBooleanCellValue();
break;
case ERROR: //故障
result = hssfCell.getErrorCellValue();
break;
case FORMULA: //公式
result = hssfCell.getCellFormula();
break;
case BLANK: //空值
result = "";
break;
default: //字符串
result = hssfCell.getStringCellValue();
}
return result;
}
/**
* excel值处理
*
* @param hssfCell
* @return
*/
public static Object getValue(Cell hssfCell) {
Object result = null;
switch (hssfCell.getCellType()) {
case NUMERIC: //数字
result = hssfCell.getNumericCellValue();
break;
case BOOLEAN: //Boolean
result = hssfCell.getBooleanCellValue();
break;
case ERROR: //故障
result = hssfCell.getErrorCellValue();
break;
case FORMULA: //公式
result = hssfCell.getCellFormula();
break;
case BLANK: //空值
result = "";
break;
default: //字符串
result = hssfCell.getStringCellValue();
}
return result;
}
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!
文章知识点与官方知识档案匹配,可进一步学习相关知识
————————————————
版权声明:本文为CSDN博主「java·D·wj」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wang_jing_jing/article/details/118933708
针对Apache POI jar包版本更新导致的代码修改问题,本文介绍如何通过引入新的CellType包并调整代码来实现对不同类型的Excel单元格数据进行正确处理。包括数字、布尔值、错误值、公式和字符串等。

1万+

被折叠的 条评论
为什么被折叠?



