如何将多个excel文件合并成一个

最近遇到需要将多个xls文件合并到一个新的xls文件内,每个xls文件对应新xls文件的一个sheet。考虑到以后可能会经常用到这个合并操作,故写了一个脚本,解放双手。
具体代码如下:

导入库

import xlrd, xlwt
import os

创建xls写入对象

def create_xls():
    """创建xls文档对象"""
    wt = xlwt.Workbook()
    # table = wt.add_sheet("sheet1", cell_overwrite_ok=True)
    return wt

多xls文件合并到新的xls文件

def multi_xls_to_one_xls
### 合并多个Excel文件一个文件的实现方法 在Java中使用Apache POI库可以有效地处理和操作Excel文件。以下是一个示例,展示如何将多个`.xlsx`格式的Excel文件合并一个文件,并确保低内存消耗[^1]。 #### 依赖配置 首先,确保在项目中添加了Apache POI的依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` #### 示例代码 以下是将多个Excel文件合并一个文件的完整代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ExcelMerger { public static void mergeExcelFiles(String[] fileNames, String outputFileName) throws IOException { // 使用SXSSFWorkbook以降低内存消耗 try (SXSSFWorkbook workbook = new SXSSFWorkbook(100); // 保留100行在内存中 FileOutputStream outputStream = new FileOutputStream(outputFileName)) { for (String fileName : fileNames) { try (FileInputStream inputStream = new FileInputStream(new File(fileName)); Workbook inputWorkbook = WorkbookFactory.create(inputStream)) { int numberOfSheets = inputWorkbook.getNumberOfSheets(); for (int sheetIndex = 0; sheetIndex < numberOfSheets; sheetIndex++) { Sheet inputSheet = inputWorkbook.getSheetAt(sheetIndex); Sheet outputSheet = workbook.createSheet(inputSheet.getSheetName()); for (Row row : inputSheet) { Row newRow = outputSheet.createRow(row.getRowNum()); for (Cell cell : row) { Cell newCell = newRow.createCell(cell.getColumnIndex()); copyCell(cell, newCell); } } } } } // 将合并后的工作簿写入输出文件 workbook.write(outputStream); } } private static void copyCell(Cell oldCell, Cell newCell) { switch (oldCell.getCellType()) { case STRING: newCell.setCellValue(oldCell.getStringCellValue()); break; case NUMERIC: if (DateUtil.isCellDateFormatted(oldCell)) { newCell.setCellValue(oldCell.getDateCellValue()); } else { newCell.setCellValue(oldCell.getNumericCellValue()); } break; case BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue()); break; case FORMULA: newCell.setCellFormula(oldCell.getCellFormula()); break; default: break; } } public static void main(String[] args) { String[] fileNames = {"file1.xlsx", "file2.xlsx", "file3.xlsx"}; String outputFileName = "merged_output.xlsx"; try { mergeExcelFiles(fileNames, outputFileName); System.out.println("Excel files have been merged successfully."); } catch (IOException e) { e.printStackTrace(); } } } ``` #### 代码说明 1. **SXSSFWorkbook**:用于处理大文件,减少内存占用[^1]。 2. **WorkbookFactory**:支持Excel文件格式的读取。 3. **copyCell方法**:根据单元格类型复制数据,确保所有数据正确迁移。 ### 注意事项 - 确保所有输入文件均为`.xlsx`格式。 - 如果需要处理更大数据集,可以调整`SXSSFWorkbook`的缓存行数参数。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值