java poi读取excel的时候报错 cell里的内容是这个怎么办 用Java的POI包读取Excel的空单元格时出错怎么处理

Java\u4f7f\u7528poi\u8bfb\u53d6excel\u7684\u65f6\u5019\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u662f\u600e\u4e48\u56de\u4e8b

Excel\u91cc\u9762\u6709\u4e9b\u8868\u683c\u662f\u516c\u5f0f(fromula),\u4f60\u4e0d\u80fd\u76f4\u63a5\u7528getNumericCellValue()\u8bfb\u53d6\u503c
\u4f60\u8981\u7528\u8bfb\u53d6\u516c\u5f0f\u7684\u65b9\u6cd5\u53d6\u503c,\u53d6\u503c\u4e4b\u524d\u5224\u65ad\u4e00\u4e0b\u5355\u5143\u683c\u7684\u7c7b\u578b,\u4ee3\u7801\u5982\u4e0b:
if(number_Cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA)
{
XSSFFormulaEvaluator formulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workBook);
Double data=formulaEvaluator.evaluate(number_Cell).getNumberValue();
}

public static String getStringCellValue(Cell cell) {
if(cell==null){
return null;
}
String value = "";
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
value = "";
}
} else {
value =
new DecimalFormat("0").format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_FORMULA: //\u5bfc\u5165\u65f6\u5982\u679c\u4e3a\u516c\u5f0f\u751f\u6210\u7684\u6570\u636e\u5219\u65e0\u503c
// System.out.println("Formula:" + cell.getStringCellValue());
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
value = "";
break;
case Cell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y" : "N");
break;
default:
value = "";
}
return value;
}

//你现在取出来的是Excel单元格对象!正确做法如下:
    /**
     * 
     * 获取单元格数据内容为字符串类型的数据
     * TODO
     * @param cell Excel单元格
     * @returnString 单元格数据内容
     */
    private String getStringCellValue(HSSFCell cell) {
        String cellValue = "";  
        DecimalFormat df = new DecimalFormat("#");  
        switch (cell.getCellType()) {  
            case HSSFCell.CELL_TYPE_STRING:  
                cellValue = cell.getRichStringCellValue().getString().trim();  
                break;  
            case HSSFCell.CELL_TYPE_NUMERIC:  
                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;  
    }


扩展阅读:java poi word ... java excel ... jaaaw ... java equals ... java workbook ... java editor ... javaw exe ... java python ... sxssfworkbook读取excel ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网