how to copy one workbook sheet to another workbook

2019-02-16 07:17发布

问题:

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?

回答1:

A little bit of googling helps:

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

.. and, consider removing your email here, a forum is meant to share discussions in effort to help everyone who has stumbled upon the similar problems, certainly not for offline/private discussions.



回答2:

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