java获取该路径下所有 Excel 文件(含子文件夹下),并把 Excel 文件最后修改时间为:一 周之内的 Excel 内容,合并到 mzy-merge.xlsx 文件中。 ② mzy-merge.xlsx 文件中,每个被合并文件内容最前部插入:该文件绝对路
时间: 2024-01-22 22:17:51 浏览: 110
遍历目录下所有的文件_遍历文件夹文件_java遍历目录下所有文件_
径和最后修改时间。以下是 Java 代码实现:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.poi.ss.usermodel.Cell;
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 MergeExcelFiles {
private static final String MERGE_FILE_NAME = "mzy-merge.xlsx";
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
try {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("merged data");
// Create header row
Row headerRow = sheet.createRow(0);
Cell cell1 = headerRow.createCell(0);
cell1.setCellValue("File Path");
Cell cell2 = headerRow.createCell(1);
cell2.setCellValue("Last Modified");
Cell cell3 = headerRow.createCell(2);
cell3.setCellValue("Data");
// Get all Excel files in the given directory and its subdirectories
File directory = new File("C:\\path\\to\\directory");
File[] allFiles = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(".xlsx"));
int rowCount = 1;
for (File file : allFiles) {
// Check if file was modified within the last week
BasicFileAttributes attributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
Calendar lastWeek = Calendar.getInstance();
lastWeek.add(Calendar.DATE, -7);
if (attributes.lastModifiedTime().toMillis() < lastWeek.getTimeInMillis()) {
continue;
}
// Open Excel file and read data
FileInputStream inputStream = new FileInputStream(file);
Workbook inputWorkbook = new XSSFWorkbook(inputStream);
Sheet inputSheet = inputWorkbook.getSheetAt(0);
int rows = inputSheet.getPhysicalNumberOfRows();
for (int i = 0; i < rows; i++) {
Row inputRow = inputSheet.getRow(i);
if (inputRow != null) {
Row outputRow = sheet.createRow(rowCount);
// Insert file path and last modified time
Cell filePathCell = outputRow.createCell(0);
filePathCell.setCellValue(file.getAbsolutePath());
Cell lastModifiedCell = outputRow.createCell(1, CellType.STRING);
lastModifiedCell.setCellValue(DATE_FORMAT.format(new Date(attributes.lastModifiedTime().toMillis())));
// Copy cell contents to output row
int cells = inputRow.getPhysicalNumberOfCells();
for (int j = 0; j < cells; j++) {
Cell inputCell = inputRow.getCell(j);
if (inputCell != null) {
Cell outputCell = outputRow.createCell(j + 2);
outputCell.setCellValue(inputCell.getStringCellValue());
}
}
rowCount++;
}
}
inputWorkbook.close();
inputStream.close();
}
// Save merged data to file
FileOutputStream outputStream = new FileOutputStream(MERGE_FILE_NAME);
workbook.write(outputStream);
workbook.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这个代码可以获取指定目录下所有扩展名为 ".xlsx" 的 Excel 文件,然后筛选出最后修改时间为一周内的文件。接着,读取每个文件的内容,并将文件的绝对路径和最后修改时间插入到每个行的前两列中。最后,将所有数据合并到一个新的 Excel 文件中。注意,在运行代码之前需要添加 Apache POI 依赖。
阅读全文