Free Support Forum - aspose.com

Combining multiple Workbooks and saving the "Master" Workbook

It seems that doing this causes a NullPointerException when saving the Master. I have a List of Workbooks that i want to combine into a master workbook. I then want to save the master workbook. Note: The List of workbooks were created in memory and will not be saved on to disk. Only the master workbook will be saved.

However, if I save each individual workbook prior to combining it to the master, saving the master will then work.

So if I have workbooks Child 1 WB, Child 2 WB, Child 3 WB in memory. I create a new Workbook "MasterWorkBook".

If i do a MasterWorkBook.combine(Child 1 WB);MasterWorkBook.combine(Child 2 WB);MasterWorkBook.combine(Child 1 WB);MasterWorkBook.save. It will fail.

However, If i do a Child1WB.save. Child2WB.save. Child3WB.save. MasterWorkbook.combine(Child1WB);MasterWorkBook.combine(Child2WB);MasterWorkBook.combine(Child3WB);MasterWorkBook.save. It works.

It seems that when you do a workbook combine, it loses some state information - unless you save the workbook first.

The exception is below

java.lang.NullPointerException
at com.aspose.cells.yB.a(Unknown Source)
at com.aspose.cells.ah.a(Unknown Source)
at com.aspose.cells.Bb.a(Unknown Source)
at com.aspose.cells.Bb.b(Unknown Source)
at com.aspose.cells.Bb.a(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.save(Unknown Source)
at com.aspose.cells.Workbook.save(Unknown Source)

Hi,


Thanks for providing us some details.

Could you please attach your template Excel files and also paste your runnable JAVA program to reproduce the issue on our end, we will check your issue soon.

Thank you.

I found the issue.

It happens when you create the instance of the workbook using a stream, instead of providing the absolute path.

InputStream inputStream = getClass().getResourceAsStream(fileName);

Workbook workbook = new Workbook(inputStream);

However, if you just do

Workbook workbook = new Workbook("C:\\BrokerDetailReportTemplate.xlsx") then it works.

It seems like you aren't closing the stream once the Workbook has been created. But, you are when the Workbook is being saved.

Using the absolute path isn't always an option for us. We currently are using a temporary license and will be buying a real one soon.

When can this be fixed? Or is there a work around?

Hi,


I have tested your scenario using our latest version/fix Aspose.Cells for Java v7.4.2: http://www.aspose.com/community/files/72/java-components/aspose.cells-for-java/entry453651.aspx
Please try the latest version if you are not using it already.

It works fine. Here is my simple sample code and please find attached the input and output files here.

Sample code:

final Workbook newWorkbookTest = new Workbook();

InputStream inputStream = new FileInputStream(“Bk1.xlsx”);
Workbook workbook1 = new Workbook(inputStream);

inputStream = new FileInputStream(“Bk2.xlsx”);
Workbook workbook2 = new Workbook(inputStream);

newWorkbookTest.combine(workbook1);
newWorkbookTest.combine(workbook2);

newWorkbookTest.save(“f:/files/resultantfile2.xlsx”);

If you still find the issue, kindly attach your template files and paste your sample runnable code or program here, we will check your issue soon.

Thank you.


FYI, the above works, but only if you're input format and your output format are the same. If you reading an xlsx template, and then saving to a xls file, then the exception will still occur.

This is for the input stream constructor only. If you specify an asbolute path, then everything is fine.

Hi,


Well, I think for reading and saving file, please use LoadFormat and SaveFormat constants accordingly, it may resolve your issue.
e.g

//for reading file from streams.

LoadOptions loadOptions = new LoadOptions(LoadFormat.EXCEL_97_TO_2003);
Workbook workbook = new Workbook(inputStream, loadOptions);


//for saving file to stream
workbook.save(stream, SaveFormat.EXCEL_97_TO_2003);