We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Getting java.io.IOException while copying and saving

Hi,

I am getting " java.io.IOException at com.aspose.cells.Workbook.save(Unknown Source)" while trying to save the template in an Excel2007 format

please find the code below where i am trying to copy the contents of sheet1 of the genearated report (excel 2003 format) to the Template which is on Excel2007 format.

private void processExcelTemplate(Report report, byte[] b, ServletOutputStream out, HttpServletResponse response, String outputFilename) throws Exception {
AttachmentService attachmentService = (AttachmentService) ServiceManager.getInstance().lookup(AttachmentService.ROLE);

Workbook reportWorkBook = new Workbook();
reportWorkBook.open(new ByteArrayInputStream(b));

List attachmentList = attachmentService.loadAttachment(report.getId(), report.moduleId());
if (attachmentList != null && attachmentList.size() > 0) {
Attachment attachment = (Attachment) attachmentList.get(0);
String fileName = attachment.getName();
String ext = fileName.substring(fileName.lastIndexOf(".") +1, fileName.length());

Workbook templateWorkBook = new Workbook();
// We need to identify the file format of the template excels file. (eg:- 2000, 2003, 2007 format)
if ("xls".equalsIgnoreCase(ext)) {
templateWorkBook.open(new ByteArrayInputStream(Zip.unzip(attachment.getContents())), FileFormatType.DEFAULT);
} else if ("xlsx".equalsIgnoreCase(ext)) {
templateWorkBook.open(new ByteArrayInputStream(Zip.unzip(attachment.getContents())), FileFormatType.EXCEL2007);
} else if ("xlsm".equalsIgnoreCase(ext)) {
templateWorkBook.open(new ByteArrayInputStream(Zip.unzip(attachment.getContents())), FileFormatType.EXCEL2007XLSM);
} else if ("xltx".equalsIgnoreCase(ext)) {
templateWorkBook.open(new ByteArrayInputStream(Zip.unzip(attachment.getContents())), FileFormatType.EXCEL2007XLTX);
} else if ("xltm".equalsIgnoreCase(ext)) {
templateWorkBook.open(new ByteArrayInputStream(Zip.unzip(attachment.getContents())), FileFormatType.EXCEL2007XLTM);
} else {
templateWorkBook.open(new ByteArrayInputStream(Zip.unzip(attachment.getContents())), FileFormatType.DEFAULT);
}

Worksheets reportWorksheets = reportWorkBook.getWorksheets();

Worksheets templateWorkSheets = templateWorkBook.getWorksheets();
Worksheet templateWorkSheet = templateWorkSheets.getSheet(0);
templateWorkSheet.copy(reportWorksheets.getSheet(0));
if ("xls".equalsIgnoreCase(ext)) {
response.setContentType("application/msexcel");
response.setHeader("Content-Disposition", "attachment; filename=" + outputFilename + "." +ext);
templateWorkBook.save(out, FileFormatType.DEFAULT);
} else if ("xlsx".equalsIgnoreCase(ext)) {
response.setContentType("application/xlsx");
response.setHeader("Content-Disposition", "attachment; filename=" + outputFilename + "." +ext);
templateWorkBook.save(out, FileFormatType.EXCEL2007);
} else if ("xlsm".equalsIgnoreCase(ext)) {
response.setContentType("application/xlsm");
response.setHeader("Content-Disposition", "attachment; filename=" + outputFilename + "." +ext);

//the exception is throw while executing the below code
templateWorkBook.save(out, FileFormatType.EXCEL2007XLSM);
} else if ("xltx".equalsIgnoreCase(ext)) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=" + outputFilename + "." +ext);
templateWorkBook.save(out, FileFormatType.EXCEL2007XLTX);
} else if ("xltm".equalsIgnoreCase(ext)) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=" + outputFilename + "." +ext);
templateWorkBook.save(out, FileFormatType.EXCEL2007XLTM);
} else {
response.setContentType("application/msexcel");
response.setHeader("Content-Disposition", "attachment; filename=" + outputFilename + ".xls");
templateWorkBook.save(out, FileFormatType.DEFAULT);
}
} else {
reportWorkBook.save(out);
}
}


any help on this issue is highly appreciated.

Regards,

Mahesh

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for considering Aspose.

We tried to check your scenario a bit, but we are unable to figure out the issue. Please share the complete exception message; this will help us find out the issue soon.

Thank You & Best Regards,