Creating a wrokbook with multiple tabs - java

Looking for some help here. I have a situation I am getting data in a byte array tab by tab.

Say an excel has 10 tabs, I get data for one tab at a time. Now I need to generate the xls with 10 tabs in it. the following is my code and would appreciate any suggestions.

for (int i = 0; i < documentInstance.getReports().getCount(); i++) {
xlsReportTab = documentInstance.getReports().getItem(i);
xlsReportTab.setPaginationMode(PaginationMode.Page);
binaryView = (BinaryView) xlsReportTab.getView(OutputFormatType.XLS);
Workbook workbookTab = new Workbook();
workbookTab.getWorksheets().addSheet(0, xlsReportTab.getName());
workbookTab.open(new ByteArrayInputStream(binaryView.getContent()));
// workbookTab.loadData(binaryView.getContentType());
workbook.getWorksheets().addSheet(i, xlsReportTab.getName());
workbook.getWorksheets().getSheet(i)
.copy(workbookTab.getWorksheets().getSheet(0));
}

Hi,

Please see how to copy all the worksheets inside a source workbook into a new workbook. Please see the source workbook and the output workbook generated by the following code. I have attached them.

Java


String dirPath=“f:\downloads\”;


//Open the source workbook

Workbook sourceWorkbook=new Workbook();

sourceWorkbook.open(dirPath + “source.xlsx”);


//Create a new destination workbook and remove its

//only sheet

Workbook destWorkbook=new Workbook();

destWorkbook.getWorksheets().removeSheet(0);


//Get the source sheets

Worksheets wsheets = sourceWorkbook.getWorksheets();


//Copy all source sheets into destination workbook

for(int i=0; i<wsheets.size(); i++)

{

Worksheet sourceSheet= sourceWorkbook.getWorksheets().getSheet(i);


destWorkbook.getWorksheets().addSheet();


int destSheetIdx=destWorkbook.getWorksheets().size() - 1;

Worksheet destSheet = destWorkbook.getWorksheets().getSheet(destSheetIdx);


destSheet.copy(sourceSheet);

}


//Write the destination workbook on disk

destWorkbook.save(dirPath + “destOutput.xlsx”, FileFormatType.XLSX);