The error 'Cannot get a STRING value from a NUMERIC cell' occurs in the insertMissingDates method at line 181. It indicates that the cell being accessed is of type NUMERIC, but the code is trying to retrieve its value as a STRING.

To fix this error, you need to handle the NUMERIC cell type appropriately. You can use the DataFormatter class from Apache POI to format the cell value based on its type. Here's the updated code for the insertMissingDates method:

// Insert missing dates
private static void insertMissingDates(Sheet sheet) {
    DataFormatter dataFormatter = new DataFormatter();
    for (int i = 1; i < sheet.getLastRowNum(); i++) {
        Row currentRow = sheet.getRow(i);
        Row nextRow = sheet.getRow(i + 1);
        if (currentRow != null && nextRow != null) {
            Cell currentCell = currentRow.getCell(0);
            Cell nextCell = nextRow.getCell(0);
            if (currentCell != null && nextCell != null) {
                String currentDate = dataFormatter.formatCellValue(currentCell);
                String nextDate = dataFormatter.formatCellValue(nextCell);
                Date parsedCurrentDate = parseDate(currentDate);
                Date parsedNextDate = parseDate(nextDate);
                if (parsedCurrentDate != null && parsedNextDate != null) {
                    long diff = parsedNextDate.getTime() - parsedCurrentDate.getTime();
                    if (diff > 10 * 24 * 60 * 60 * 1000) {
                        int insertCount = (int) (diff / (10 * 24 * 60 * 60 * 1000));
                        for (int j = 1; j <= insertCount; j++) {
                            Row newRow = sheet.createRow(i + j);
                            Cell newCell = newRow.createCell(0);
                            newCell.setCellValue(formatDate(new Date(parsedCurrentDate.getTime() + j * 10 * 24 * 60 * 60 * 1000)));
                        }
                    }
                }
            }
        }
    }
}

In this updated code, the DataFormatter is used to format the cell values as strings, regardless of their cell types. This ensures that the date values can be properly parsed.

Fix

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

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