Excel 日期数据插入:在相邻日期间隔大于10天时插入中间值

本代码使用Java和Apache POI库,实现将Excel文件'input-2.xlsx'中P1工作表第一列的时间日期数据,在相邻日期间隔大于10天时插入中间值,并将结果保存到新建的P2工作表中,同时保持与P1工作表相同的单元格格式。

代码示例

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class DateInsertion {
    public static void main(String[] args) {
        try {
            // 读取input-2.xlsx文件
            FileInputStream file = new FileInputStream("input-2.xlsx");
            Workbook workbook = new XSSFWorkbook(file);
            Sheet sheet = workbook.getSheet("P1");
            
            // 创建新的P2工作表
            Sheet newSheet = workbook.createSheet("P2");
            
            // 复制P1工作表的单元格格式到P2工作表
            copyCellStyle(workbook, sheet, newSheet);
            
            // 获取P1工作表第一列的时间日期数据
            List<Date> dates = getColumnValues(sheet, 0);
            
            // 进行日期插入操作
            List<Date> newDates = insertDates(dates);
            
            // 将插入后的日期写入P2工作表
            writeColumnValues(newSheet, 0, newDates);
            
            // 保存结果到input-2.xlsx文件
            FileOutputStream outFile = new FileOutputStream("input-2.xlsx");
            workbook.write(outFile);
            outFile.close();
            
            System.out.println("操作完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    // 复制单元格格式
    private static void copyCellStyle(Workbook workbook, Sheet sourceSheet, Sheet targetSheet) {
        for (int rowIndex = 0; rowIndex < sourceSheet.getLastRowNum(); rowIndex++) {
            Row sourceRow = sourceSheet.getRow(rowIndex);
            Row targetRow = targetSheet.createRow(rowIndex);
            
            for (int columnIndex = 0; columnIndex < sourceRow.getLastCellNum(); columnIndex++) {
                Cell sourceCell = sourceRow.getCell(columnIndex);
                Cell targetCell = targetRow.createCell(columnIndex);
                
                CellStyle style = sourceCell.getCellStyle();
                targetCell.setCellStyle(style);
            }
        }
    }
    
    // 获取指定列的日期数据
    private static List<Date> getColumnValues(Sheet sheet, int columnIndex) {
        List<Date> columnValues = new ArrayList<>();
        
        for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
            Row row = sheet.getRow(rowIndex);
            
            if (row != null) {
                Cell cell = row.getCell(columnIndex);
                
                if (cell != null && cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    columnValues.add(cell.getDateCellValue());
                }
            }
        }
        
        return columnValues;
    }
    
    // 在日期数据中插入中间值,直至相邻日期间隔小于10天
    private static List<Date> insertDates(List<Date> dates) {
        List<Date> newDates = new ArrayList<>(dates);
        
        for (int i = 0; i < newDates.size() - 1; i++) {
            Date currentDate = newDates.get(i);
            Date nextDate = newDates.get(i + 1);
            
            long interval = (nextDate.getTime() - currentDate.getTime()) / (24 * 60 * 60 * 1000);
            
            while (interval > 10) {
                Date middleDate = new Date((currentDate.getTime() + nextDate.getTime()) / 2);
                newDates.add(i + 1, middleDate);
                interval = (nextDate.getTime() - currentDate.getTime()) / (24 * 60 * 60 * 1000);
            }
        }
        
        return newDates;
    }
    
    // 将日期数据写入指定列
    private static void writeColumnValues(Sheet sheet, int columnIndex, List<Date> dates) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        
        for (int rowIndex = 0; rowIndex < dates.size(); rowIndex++) {
            Row row = sheet.getRow(rowIndex);
            
            if (row == null) {
                row = sheet.createRow(rowIndex);
            }
            
            Cell cell = row.createCell(columnIndex);
            cell.setCellValue(dateFormat.format(dates.get(rowIndex)));
        }
    }
}

使用说明

  1. 添加 Apache POI 库: 您可以从官方网站(https://poi.apache.org/)下载并添加所需的 JAR 文件到您的项目中。
  2. 修改代码: 将代码中的 'input-2.xlsx' 替换为您的 Excel 文件名。
  3. 运行代码: 运行代码,程序将自动在 'input-2.xlsx' 中创建 P2 工作表并保存结果。

注意

  • 代码中将日期间隔大于10天的定义为大于10天。您可以根据需要修改此阈值。
  • 代码假设您的 Excel 文件是 .xlsx 格式。如果您使用的是其他格式,则需要更改代码中的 Workbook 类。
  • 代码只处理了第一列的时间日期数据,您可以根据需要修改代码以处理其他列的数据。

希望这篇文章对您有所帮助!

Excel 日期数据插入:在相邻日期间隔大于10天时插入中间值

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

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