开发者

how to copy one workbook sheet to another workbook sheet using apache POI and java [duplicate]

开发者 https://www.devze.com 2023-01-08 15:57 出处:网络
This question already has answers here: Copying Excel Worksheets in POI 开发者_开发知识库 (6 answers)
This question already has answers here: Copying Excel Worksheets in POI 开发者_开发知识库 (6 answers) Closed 3 years ago.

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.

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

精彩评论

暂无评论...
验证码 换一张
取 消