如何使用apache POI和java将一个工作簿表复制到另一个工作簿表 [英] how to copy one workbook sheet to another workbook sheet using apache POI and java

查看:55
本文介绍了如何使用apache POI和java将一个工作簿表复制到另一个工作簿表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have one excel file with single sheet (abstract model). Now I want to copy the sheet to another existing workbook. How can I do this?

推荐答案

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

For processing regular styles and data we could iterate through each cells.
But if we have formula enabled cell,non editable cells, merged cell then this is the better solution to go for:
I have tried something like copying sheets but it didn't worked.
In addition with that i need to copy a sheet in which there are some non editable cells and formula enabled cells too.

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

This solution worked for me:
1)copy the entire workbook
2)delete unnecessary sheets 3)add your new sheets to above book

//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();

这篇关于如何使用apache POI和java将一个工作簿表复制到另一个工作簿表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆