package Data_Recovery;import javaioFile;import javaioFileInputStream;import javaioFileOutputStream;import javaioIOException;import javatimeLocalDate;import javatimetemporalChronoUnit;import orgapachep
在第29行和第56行修改代码,将getColumn方法改为getColumnIterator方法。修改后的代码如下:
// 获取P2工作表的第一列
Iterator<Cell> column = newSheet.getColumnIterator(0);
// ...
// 遍历第一列的单元格
for (Row row : newSheet) {
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;
row = newSheet.createRow(row.getRowNum() + 1);
currCell = row.createCell(0, CellType.NUMERIC);
currCell.setCellValue(interpolatedDate);
continue;
}
}
prevCell = currCell;
}
}
此外,在第50行和第56行修改代码,将setCellValue方法的参数改为interpolatedDate.toString()。修改后的代码如下:
// 在两个日期之间插入中间日期
Cell interpolatedCell = row.createCell(0, CellType.STRING);
interpolatedCell.setCellValue(interpolatedDate.toString());
// ...
row = newSheet.createRow(row.getRowNum() + 1);
currCell = row.createCell(0, CellType.STRING);
currCell.setCellValue(interpolatedDate.toString());
这样可以将LocalDate类型的日期转换为String类型的字符串进行设置。
原文地址: https://www.cveoy.top/t/topic/ijWK 著作权归作者所有。请勿转载和采集!