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> rowIterator = newSheet.iterator();
            rowIterator.next(); // 跳过第一行标题行

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

            // 遍历第一列的单元格
            while (rowIterator.hasNext()) {
                Row row = rowIterator.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);

                            // 继续判断插入的日期与上下日期的间隔
                            prevCell = interpolatedCell;
                            currCell = row.createCell(0, CellType.NUMERIC);
                            currCell.setCellValue(interpolatedDate);
                            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;
                }
            }
        }
    }
}
Java实现Excel日期插值:解决日期间隔过大的问题

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

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