如何使用apache POI和java将一个工作簿工作表复制到另一个工作簿表

问题描述 投票:15回答:2

我有一个带单页的excel文件(抽象模型)。现在我想将工作表复制到另一个现有工作簿。我怎样才能做到这一点?

java apache-poi
2个回答
5
投票

一点谷歌搜索有助于:

http://www.coderanch.com/t/420958/open-source/Copying-sheet-excel-file-another

..并且,考虑在这里删除你的电子邮件,一个论坛旨在分享讨论,以帮助每个偶然发现类似问题的人,当然不是为了离线/私人讨论。


1
投票

为了处理常规样式和数据,我们可以迭代每个单元格。 但是如果我们有公式启用的单元格,不可编辑的单元格,合并单元格,那么这是更好的解决方案: 我尝试过复制工作表之类的东西,但它没有用。 此外,我需要复制一张表格,其中有一些不可编辑的单元格和公式启用的单元格。

这个解决方案对我有用: 1)复制整个工作簿 2)删除不必要的工作表3)将新工作表添加到上面的书中

//input source excel file which contains sheets to be copied
file = new FileInputStream("C:\\SamepleTemplate.xlsx");
workbookinput = new XSSFWorkbook(file);

//output new excel file to which we need to copy the above sheets
//this would copy entire workbook from source
XSSFWorkbook workbookoutput=workbookinput;

//delete one or two unnecessary sheets, you can delete them by specifying the sheetnames
workbook.removeSheetAt(workbook.getSheetIndex(workbook.getSheet(" your sheet name ")));

//if you want to delete more sheets you can use a for to delete the sheets
for (int index=0;index<=5;index++)
{
   workbook.removeSheetAt(index);
}

//To write your changes to new workbook
FileOutputStream out = new FileOutputStream("C:\\FinalOutput.xlsx");
workbookoutput.write(out);
out.close();
© www.soinside.com 2019 - 2024. All rights reserved.