private static void insertMissingData(Sheet sheet) {
    Workbook workbook = sheet.getWorkbook(); // 获取工作簿对象
    List<Date> insertDates = new ArrayList<>(); // 保存插入的日期

    // 遍历工作表的每一行数据
    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 currentDateCell = currentRow.getCell(0); // 当前行的日期单元格
            Cell nextDateCell = nextRow.getCell(0); // 下一行的日期单元格

            // 判断当前行和下一行的日期单元格是否为空,并且单元格格式是否为数字
            if (currentDateCell != null && nextDateCell != null && currentDateCell.getCellType() == CellType.NUMERIC && nextDateCell.getCellType() == CellType.NUMERIC) {
                Date currentDate = currentDateCell.getDateCellValue(); // 当前行的日期
                Date nextDate = nextDateCell.getDateCellValue(); // 下一行的日期

                long diff = nextDate.getTime() - currentDate.getTime(); // 计算两个日期之间的时间差
                long diffDays = diff / (24 * 60 * 60 * 1000); // 计算时间差的天数

                // 如果时间差大于10天,则需要插入缺失数据
                if (diffDays > 10) {
                    int insertCount = (int) (diffDays / 10); // 计算需要插入的行数

                    // 遍历插入行数
                    for (int j = 1; j <= insertCount; j++) {
                        // 移动原有数据
                        sheet.shiftRows(i + 1, sheet.getLastRowNum(), 1, true, true);

                        // 插入新行
                        Row newRow = sheet.createRow(i + 1);
                        Cell newDateCell = newRow.createCell(0); // 新行的日期单元格
                        Cell newDataCell = newRow.createCell(1); // 新行的数据单元格

                        long insertTime = currentDate.getTime() + j * 10 * 24 * 60 * 60 * 1000; // 计算插入的时间戳
                        Date insertDate = new Date(insertTime); // 将插入的时间戳转换为日期
                        newDateCell.setCellValue(insertDate); // 设置新行的日期单元格的值
                        insertDates.add(insertDate); // 添加插入的日期到列表中

                        // 将原有数据填充到新行的相应位置上
                        Cell currentDataCell = currentRow.getCell(1); // 当前行的数据单元格
                        CellStyle currentDataCellStyle = currentDataCell.getCellStyle(); // 当前行的数据单元格样式
                        Cell newDataCell2 = newRow.createCell(1); // 新行的数据单元格
                        newDataCell2.setCellStyle(currentDataCellStyle); // 设置新行的数据单元格样式
                        newDataCell2.setCellValue(currentDataCell.getNumericCellValue()); // 设置新行的数据单元格的值

                        // 设置新行的日期单元格格式与原单元格一致
                        CellStyle oldDateCellStyle = currentDateCell.getCellStyle(); // 原单元格的日期样式
                        CellStyle newDateCellStyle = workbook.createCellStyle(); // 新单元格的日期样式
                        newDateCellStyle.cloneStyleFrom(oldDateCellStyle); // 克隆原单元格的日期样式
                        newDateCell.setCellStyle(newDateCellStyle); // 设置新行的日期单元格样式

                        // 设置新行的数据单元格格式与原单元格一致
                        CellStyle oldDataCellStyle = currentRow.getCell(1).getCellStyle(); // 原单元格的数据样式
                        CellStyle newDataCellStyle = workbook.createCellStyle(); // 新单元格的数据样式
                        newDataCellStyle.cloneStyleFrom(oldDataCellStyle); // 克隆原单元格的数据样式
                        newDataCell.setCellStyle(newDataCellStyle); // 设置新行的数据单元格样式

                        newDataCell.setCellValue(calculateEM(sheet, i + j)); // 设置新行的数据单元格的值
                    }
                    i += insertCount; // 更新行索引
                }
            }
        }
    }

    // 对插入的日期进行排序
    Collections.sort(insertDates);

    // 更新对应行的日期单元格的值
    for (int i = 0; i < insertDates.size(); i++) {
        Row row = sheet.getRow(i + 1); // 获取对应行
        Cell dateCell = row.getCell(0); // 获取日期单元格
        dateCell.setCellValue(insertDates.get(i)); // 设置日期单元格的值

        // 设置日期单元格的格式与原单元格一致
        CellStyle oldDateCellStyle = sheet.getRow(1).getCell(0).getCellStyle(); // 原单元格的日期样式
        CellStyle newDateCellStyle = workbook.createCellStyle(); // 新单元格的日期样式
        newDateCellStyle.cloneStyleFrom(oldDateCellStyle); // 克隆原单元格的日期样式
        dateCell.setCellStyle(newDateCellStyle); // 设置日期单元格的样式
    }
}
Java Apache POI: 如何插入缺失的日期数据到Excel表格

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

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