简体   繁体   English

将 Excel 文件合并到单个工作簿中

[英]Merging Excel files into single workbook

I have a requirement to copy all the individual excel files to one single workbook separated by tabs where I'm using ASPOSE API.我需要将所有单独的 excel 文件复制到一个单独的工作簿中,该工作簿由我使用 ASPOSE API 的选项卡分隔。 But its a paid one.但它是一个付费的。

I have seen another API's which is cell-to-cell copying but its consuming time.我见过另一个 API,它是单元到单元的复制,但它消耗时间。 I don't find any API to copy directly from the sheet.我没有找到任何可以直接从工作表复制的 API。

Is there any way to copy directly from sheet to sheet?有没有办法直接从工作表复制到工作表?

Here's an example that assumes a directory containing files having the extension .xlsx and each one has a single sheet.这是一个示例,假设一个目录包含扩展名为.xlsx 的文件,并且每个文件都有一张纸。

You will need the following imports:您将需要以下导入:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.poi.ss.usermodel.Cell;
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;

And in the example, read the code comments, please:在示例中,请阅读代码注释:

public static void main(String[] args) {
    // provide a path to a folder containing xlsx-workbooks
    Path folderWithWorkbooks = Paths.get("Y:\\our\\path\\to\\a\\folder\\with\\workbooks");
    // provide a workbook object to be written to
    Workbook resultWorkbook = new XSSFWorkbook();

    try {
        // get the file system objects in that folder
        Files.newDirectoryStream(folderWithWorkbooks).forEach(p -> {
            // and if one is an xlsx-workbook
            if (p.getFileName().toString().endsWith(".xlsx")) {
                // try to read its contents
                try (FileInputStream fis = new FileInputStream(p
                                                              .toAbsolutePath()
                                                              .toString())) {
                    // create the workbook to be parsed
                    Workbook currentWorkbook = new XSSFWorkbook(fis);
                    // get the FIRST sheet (adjust code here if you want more sheets)
                    Sheet sourceSheet = currentWorkbook.getSheetAt(0);
                    // create a new sheet in the result workbook, name pointing to its origin
                    Sheet resultSheet = resultWorkbook.createSheet("from "
                                                           + p.getFileName().toString());

                    // then classicly loop through the rows and cells and copy the contents
                    for (int r = 0; r < sourceSheet.getPhysicalNumberOfRows(); r++) {
                        Row sourceRow = sourceSheet.getRow(r);
                        Row resultRow = resultSheet.createRow(r);

                        for (int c = 0; c < sourceRow.getPhysicalNumberOfCells(); c++) {
                            Cell sourceCell = sourceRow.getCell(c);
                            Cell resultCell = resultRow.createCell(c);

                            // copy contents with respect to their types
                            switch (sourceCell.getCellType()) {
                            case NUMERIC:
                                resultCell.setCellValue(sourceCell.getNumericCellValue());
                                break;
                            case STRING:
                                resultCell.setCellValue(sourceCell.getStringCellValue());
                                break;
                            case FORMULA:
                                resultCell.setCellValue(sourceCell.getCellFormula());
                                break;
                            case BOOLEAN:
                                resultCell.setCellValue(sourceCell.getBooleanCellValue());
                                break;
                            case ERROR:
                                resultCell.setCellValue(sourceCell.getErrorCellValue());
                                break;
                            case BLANK:
                            case _NONE:
                                resultCell.setCellValue(sourceCell.getStringCellValue());
                                break;
                            }
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        // write the result workbook to the same folder
        FileOutputStream fos = new FileOutputStream(folderWithWorkbooks
                .resolve("result.xlsx")
                .toAbsolutePath()
                .toString());
        resultWorkbook.write(fos);
        fos.flush();
        fos.close();
        resultWorkbook.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The result will be a workbook in the same directory with a name result.xlsx .结果将是同一目录中名为result.xlsx的工作簿。

Please note that this does not copy any cell formatting or styles.请注意,这不会复制任何单元格格式或样式。 You would have to add code for it in the section that copies the cell values.您必须在复制单元格值的部分中为其添加代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在java中将两个excel文件合并为一个工作簿中的两张工作表 - Merging Two excel files as two sheets in one workbook in java 如何将多个Excel文件合并到具有多个工作表的单个Excel工作簿中? - How to combine multiple Excel files into single Excel workbook with multiple sheets? 将不同的csv文件保存为单个excel工作簿中的不同工作表 - Saving different csv files as different sheets in a single excel workbook 将不同工作簿的多个Excel工作表复制到单个工作簿中 - Copying mutiple excel sheets of different workbook into a single workbook stream to strings:将多个文件合并为一个字符串 - streams to strings: merging multiple files into a single string 如何在 Java 中正确断言运行时生成的 excel 工作簿(或二进制文件) - How to properly assert runtime generated excel workbook(or binary files) in Java Java中将多个文件的子部分合并为一个文件 - Merging sub parts of multiple files into a single file in Java 是否有一个工具可以来回替换传递的Excel文件并将它们合并? - Is there a tool that would replace passing Excel files back and forth and merging them? 在单个Excel文件中合并Excel文件的最佳方法 - Best way to merge Excel files in a single Excel file 我正在尝试使用 java 将多个 csv 文件合并到一个 Excel 工作簿中作为不同的工作表 - I am trying to combine multiple csv files into one excel workbook as different sheets using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM