package Data_Recovery;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class EM03 {
    public static void main(String[] args) {
        try {
            // 读取 input.xlsx 文件
            FileInputStream file = new FileInputStream(new File('input-2.xlsx'));
            Workbook workbook = new XSSFWorkbook(file);
            Sheet sheet = workbook.getSheet('P1');

            // 创建新的 P2 工作表
            Sheet newSheet = workbook.createSheet('P2');

            // 复制 P1 工作表的数据到 P2 工作表
            copySheet(sheet, newSheet);

            // 获取 P2 工作表的第一列
            Iterator<Row> rows = newSheet.iterator();

            // 获取第一列的单元格
            Cell prevCell = null;
            Cell currCell = null;

            // 遍历第一列的单元格
            while (rows.hasNext()) {
                Row row = rows.next();
                currCell = row.getCell(0);
                if (currCell != null && currCell.getCellType() == CellType.NUMERIC) {
                    if (prevCell != null) {
                        LocalDate prevDate = prevCell.getLocalDateTimeCellValue().toLocalDate();
                        LocalDate currDate = currCell.getLocalDateTimeCellValue().toLocalDate();

                        // 判断相邻日期间隔是否大于 10 天
                        long daysBetween = ChronoUnit.DAYS.between(prevDate, currDate);
                        if (daysBetween > 10) {
                            LocalDate interpolatedDate = prevDate.plusDays(daysBetween / 2);

                            // 在两个日期之间插入中间日期
                            Cell interpolatedCell = row.createCell(0, CellType.NUMERIC);
                            interpolatedCell.setCellValue(interpolatedDate.toEpochDay());

                            // 继续判断插入的日期与上下日期的间隔
                            prevCell = interpolatedCell;
                            row = newSheet.createRow(row.getRowNum() + 1);
                            currCell = row.createCell(0, CellType.NUMERIC);
                            currCell.setCellValue(interpolatedDate.toEpochDay());
                            continue;
                        }
                    }
                    prevCell = currCell;
                }
            }

            // 保存修改后的文件
            FileOutputStream outFile = new FileOutputStream(new File('input-2.xlsx'));
            workbook.write(outFile);
            outFile.close();

            System.out.println('P2 工作表已保存成功。');
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 复制源工作表的数据到目标工作表
    private static void copySheet(Sheet sourceSheet, Sheet targetSheet) {
        for (Row sourceRow : sourceSheet) {
            Row newRow = targetSheet.createRow(sourceRow.getRowNum());
            for (Cell sourceCell : sourceRow) {
                Cell newCell = newRow.createCell(sourceCell.getColumnIndex(), sourceCell.getCellType());
                switch (sourceCell.getCellType()) {
                    case STRING:
                        newCell.setCellValue(sourceCell.getStringCellValue());
                        break;
                    case NUMERIC:
                        newCell.setCellValue(sourceCell.getNumericCellValue());
                        break;
                    case BOOLEAN:
                        newCell.setCellValue(sourceCell.getBooleanCellValue());
                        break;
                    case FORMULA:
                        newCell.setCellFormula(sourceCell.getCellFormula());
                        break;
                    default:
                        break;
                }
            }
        }
    }
}

代码说明:

  1. 更新 POI 库: 由于 getColumnIterator()getLocalDateTimeCellValue() 方法在旧版本的 POI 库中不存在,需要更新到最新版本。在 Maven 项目中,修改 pom.xml 文件,添加以下依赖项:
<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.0.0</version>
    </dependency>
</dependencies>
  1. 使用 Iterator<Row> 遍历工作表: 使用 Iterator<Row> 遍历工作表的行,获取第一列的单元格。

  2. LocalDate 转换为 double 类型: setCellValue() 方法无法直接接受 LocalDate 类型的数据,需要将其转换为 double 类型。可以使用 toEpochDay() 方法将 LocalDate 转换为从 1970 年 1 月 1 日开始的连续天数。

注意:

  • 以上代码假设 Excel 文件中第一列包含日期数据,并且日期格式为 yyyy-MM-dd
  • 可以根据实际情况调整代码逻辑,例如更改日期间隔、插入日期的格式等等。
  • 建议在操作 Excel 文件时,使用 try-catch 语句处理异常情况。

示例:

假设 input-2.xlsx 文件的第一列包含以下数据:

| 日期 | 其他数据 | |----------|-----------| | 2023-01-01 | ... | | 2023-01-12 | ... | | 2023-01-24 | ... |

运行代码后,input-2.xlsx 文件的第一列将被修改为:

| 日期 | 其他数据 | |----------|-----------| | 2023-01-01 | ... | | 2023-01-06 | ... | | 2023-01-12 | ... | | 2023-01-17 | ... | | 2023-01-24 | ... |

代码会在相邻日期间隔大于 10 天的位置插入中间日期,并将中间日期的值设置为两个相邻日期的中间日期。


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

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