This question already has answers here:
Copying Excel Worksheets in POI
(6个答案)
去年关闭。
我有一张单张(抽象模型)的excel文件。现在,我要将工作表复制到另一个现有的工作簿。我怎样才能做到这一点?
(6个答案)
去年关闭。
我有一张单张(抽象模型)的excel文件。现在,我要将工作表复制到另一个现有的工作簿。我怎样才能做到这一点?
最佳答案
为了处理常规样式和数据,我们可以遍历每个单元格。
但是,如果我们有启用公式的单元格,不可编辑的单元格,合并的单元格,那么这是一个更好的解决方案:
我已经尝试过复印纸张等操作,但是没有用。
除此之外,我需要复制一张纸,其中还有一些不可编辑的单元格和启用了公式的单元格。
此解决方案为我工作:
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();