根据异常信息,可以看到在解析日期时出现了问题,具体是因为日期单元格中的值为"日期"而不是有效的日期格式。解决办法是在解析日期之前,先判断单元格的类型,如果是字符串类型,则跳过该行数据的解析。

在代码中添加以下判断语句即可:

if (dateCell != null && valueCell != null) {
    Date date = null;
    double value = 0;
    if (dateCell.getCellType() == CellType.NUMERIC) {
        date = dateCell.getDateCellValue();
    } else if (dateCell.getCellType() == CellType.STRING) {
        String dateString = dateCell.getStringCellValue();
        if (!dateString.equals("日期")) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy/M/d h:mm;@");
            date = formatter.parse(dateString);
        }
    }
    if (valueCell.getCellType() == CellType.NUMERIC) {
        value = valueCell.getNumericCellValue();
    } else if (valueCell.getCellType() == CellType.STRING) {
        value = Double.parseDouble(valueCell.getStringCellValue());
    }
    dates.add(date);
    values.add(value);
}

这样就可以避免解析无效日期字符串时出现异常

package org1;import javaio;import javautil;import orgapachepoissusermodel;import orgapachepoixssfusermodel;import orgjfreechart;import orgjfreechartaxis;import orgjfreechartplot;import orgjfreedatatim

原文地址: https://www.cveoy.top/t/topic/hylO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录