Java 合并excel文件
时间: 2023-09-17 20:14:58 浏览: 143
做了一个java小程序 合并多个excel
好的,我知道了,关于您的问题,您可以使用 Apache POI 库来处理 Excel 文件。下面是一个示例代码来将多个 Excel 文件合并成一个 Excel 文件:
```
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
public class MergeExcels {
public static void main(String[] args) throws Exception {
// 输入文件夹路径
String folderPath = "C:\\excel\\";
// 输出文件路径
String outputPath = "C:\\excel\\merged.xls";
// 获取文件夹下所有的Excel文件
List<File> excels = getExcelFiles(folderPath);
// 如果没有Excel文件则直接返回
if (excels.isEmpty()) {
System.out.println("There are no Excel files in the folder!");
return;
}
// 创建一个输出Excel文件对象
HSSFWorkbook mergedWorkbook = new HSSFWorkbook();
// 循环处理每个Excel文件
for (int i = 0; i < excels.size(); i++) {
File excelFile = excels.get(i);
// 读取Excel文件
FileInputStream inputStream = new FileInputStream(excelFile);
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
// 循环处理每个工作表
for (int j = 0; j < workbook.getNumberOfSheets(); j++) {
HSSFSheet sheet = workbook.getSheetAt(j);
HSSFSheet mergedSheet = getMergedSheet(mergedWorkbook, sheet.getSheetName());
// 循环处理每一行
for (int k = sheet.getFirstRowNum(); k <= sheet.getLastRowNum(); k++) {
HSSFRow row = sheet.getRow(k);
if (row == null) {
continue;
}
// 复制行
HSSFRow mergedRow = mergedSheet.createRow(mergedSheet.getLastRowNum() + 1);
mergedRow.setHeight(row.getHeight());
// 循环处理每个单元格
for (int l = row.getFirstCellNum(); l < row.getLastCellNum(); l++) {
Cell cell = row.getCell(l);
if (cell == null) {
continue;
}
// 复制单元格
Cell mergedCell = mergedRow.createCell(l);
mergedCell.setCellType(cell.getCellTypeEnum());
mergedCell.setCellStyle(cell.getCellStyle());
switch (cell.getCellTypeEnum()) {
case BOOLEAN:
mergedCell.setCellValue(cell.getBooleanCellValue());
break;
case NUMERIC:
mergedCell.setCellValue(cell.getNumericCellValue());
break;
case STRING:
mergedCell.setCellValue(cell.getStringCellValue());
break;
case FORMULA:
default:
break;
}
}
}
}
workbook.close();
}
// 写入输出Excel文件
FileOutputStream outputStream = new FileOutputStream(outputPath);
mergedWorkbook.write(outputStream);
mergedWorkbook.close();
outputStream.close();
System.out.println("The Excel files are merged successfully!");
}
private static List<File> getExcelFiles(String folderPath) {
File folder = new File(folderPath);
File[] files = folder.listFiles();
List<File> excelFiles = new ArrayList<File>();
for (File file : files) {
if (file.isFile() && isExcelFile(file)) {
excelFiles.add(file);
}
}
return excelFiles;
}
private static boolean isExcelFile(File file) {
return file.getName().endsWith(".xls") || file.getName().endsWith(".xlsx");
}
private static HSSFSheet getMergedSheet(HSSFWorkbook mergedWorkbook, String sheetName) {
// 查询是否已经有当前工作表
for (int i = 0; i < mergedWorkbook.getNumberOfSheets(); i++) {
HSSFSheet sheet = mergedWorkbook.getSheetAt(i);
if (sheet.getSheetName().equals(sheetName)) {
return sheet;
}
}
// 创建新的工作表
HSSFSheet mergedSheet = mergedWorkbook.createSheet(sheetName);
mergedSheet.setRowIndex(0);
// 复制列宽
HSSFSheet sourceSheet = mergedWorkbook.getSheetAt(0);
for (int i = 0; i < sourceSheet.getRow(0).getLastCellNum(); i++) {
mergedSheet.setColumnWidth(i, sourceSheet.getColumnWidth(i));
}
// 复制样式
HSSFRow sourceRow = sourceSheet.getRow(0);
HSSFCellStyle sourceStyle = sourceRow.getCell(0).getCellStyle();
HSSFCellStyle mergedStyle = mergedWorkbook.createCellStyle();
mergedStyle.cloneStyleFrom(sourceStyle);
return mergedSheet;
}
}
```
您只需根据实际情况,修改输入文件夹路径和输出文件路径即可。希望对您有所帮助。
阅读全文