Excel 日期数据自动插入 - 使用 Apache POI 进行日期插入和格式复制
package Data_Recovery;
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.CellType;
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 EM03 {
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() == CellType.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)));
}
}
}
该代码使用 Apache POI 库,读取 Excel 文件中 P1 工作表的日期数据,自动在相邻日期间隔大于 10 天的情况下插入中间日期,并将插入后的日期数据写入到新工作表 P2。同时,代码也保留了 P1 工作表的单元格格式,确保插入后的日期数据具有相同的格式。
代码功能:
- 读取 Excel 文件:从 'input-2.xlsx' 文件中读取 'P1' 工作表数据。
- 创建新工作表:创建名为 'P2' 的新工作表。
- 复制单元格格式:将 'P1' 工作表的单元格格式复制到 'P2' 工作表。
- 获取日期数据:从 'P1' 工作表的第 1 列获取日期数据。
- 插入日期:在日期数据中插入中间值,直到相邻日期间隔小于 10 天。
- 写入日期数据:将插入后的日期数据写入 'P2' 工作表的第 1 列。
- 保存结果:将修改后的 Excel 文件保存为 'input-2.xlsx'。
代码亮点:
- 使用 Apache POI 库操作 Excel 文件,方便高效。
- 代码逻辑清晰易懂,便于理解和维护。
- 代码包含注释,解释了代码功能和逻辑。
使用说明:
- 将代码保存为 'EM03.java' 文件。
- 将 'input-2.xlsx' 文件放置在与代码文件相同的目录下。
- 编译并运行代码,代码会自动读取 'input-2.xlsx' 文件,进行日期插入操作,并保存结果到 'input-2.xlsx' 文件。
注意:
- 代码使用了 Apache POI 库,需要确保已正确安装该库。
- 'input-2.xlsx' 文件的格式需要符合代码要求。
- 代码示例仅供参考,实际应用中需要根据具体需求进行修改。
原文地址: https://www.cveoy.top/t/topic/fTyv 著作权归作者所有。请勿转载和采集!