I have two xlsx files at folder C:\DemoFilesExcel\demo1.xlsx and C:\DemoFilesExcel\demo2.xlsx.
I want to create a new xlsx C:\DemoFilesExcel\merged.xlsx that will have these two files as two sheets in merged.xlsx workbook.
This is to be done using apache POI in java. Any idea how to do
This below piece of code can be used to merge two excel files as two sheets in one workbook in java. This will copy both the excel files with the same structure.
It has worked for me.
http://www.aspose.com/docs/display/cellsjava/Combine+Multiple+Workbooks+into+a+Single+Workbook
My original idea was to use the cloneSheet method of XSSFWorkbook to do the trick, I was successful in copying the Relations and Drawings however failed to copy the data itself. Why? Because the XSSFSheet's method write and read are protected, I did come up with my own version of XSSFSheet by extending it and making the two methods write and read public, but that would mean I will have to copy and edit every source file using XSSFSheet replacing it with my version of XSSFSheet that would mean too much coding I didn't have that kind of time for the project.
The original answer to this question was posted on:
How to copy a sheet between Excel workbooks in Java
However, my project requirement were different, so I improvised the answer.
I added a method called combine which accepts target workbook, and source workbook.
The methods loops through all the worksheets in the source workbook and adds them to target workbook. The original answer lacked copying of relations and drawing.
Also the workbooks I was to copy from lacked Footer, neede Gridlines to be turned off, and fit to page. So here is my solution:
This code can be used for merging two excels. It worked for me: