1

I have a web application In which i have excel file(.xls) download option. Now I Have to provide that feature in .xlsx

I am trying to Use POI Jar. When I try to do that as a stand alone Application it works fine, but when I try to integrate that into a web application, I am getting an error as

Excel Found Unreadable Content in FILENAME.xlsx. do you want to recover the content of this workbook?
If you trust the source of this workbook click yes!

XSSFWorkbook w = FileName.createExcelWorkbookPosition(
        request.getParameter("BSNS_DT"));
response.setContentType(
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition","attachment;filename=filename.xlsx");
w.write(response.getOutputStream());

Here's the Java code where I create the spreadsheet:

public static XSSFWorkbook createExcelWorkbookPosition(String BsnsDate)
    throws Exception
{
    FileOutputStream out = new FileOutputStream("workbook.xlsx");

    // create a new workbook
    XSSFWorkbook wb = new XSSFWorkbook();
    // create a new sheet
    XSSFSheet s = wb.createSheet();
    // declare a row object reference
    XSSFRow r = null;
    // declare a cell object reference
    XSSFCell c = null;

    // header row and columns
    r = s.createRow(0);
    c = r.createCell(0);
    c.setCellValue("Business Date");    
    //c.setCellStyle(cs);
    c = r.createCell(1);
    c.setCellValue("Account No");

    try {
        wb.write(out);
        out.close();
        System.out.println("File writed");
    } catch (Exception e) {
        System.out.println("Error");
        System.out.println(e);
    }
    return wb;
}

Can anyone please help? Sorry for any bad English! Thanks.

Rup
  • 33,765
  • 9
  • 83
  • 112
crazyStart
  • 127
  • 1
  • 10
  • Can you share the code you use to create the excel workbook? – Priyesh Apr 28 '14 at 11:09
  • Can you post some snippet of code to ensure that excel is correctly generated? Do you only changed file extension o change generation too? – zeppaman Apr 28 '14 at 11:10
  • Check this link. http://answers.microsoft.com/en-us/office/forum/office_2007-excel/excel-found-unreadable-content/d5c54cc7-6290-4f55-86ac-21a65f9bb807. Might help. – Priyesh Apr 28 '14 at 11:28
  • @zeppaman I have changed the code as well .Please find the Code In edit Post – crazyStart Apr 28 '14 at 11:42
  • I'm sorry, I think we wrote at the same time, please take a look at the link belove. – zeppaman Apr 28 '14 at 11:50
  • 2
    I had a quite similar issue, please have a look at http://stackoverflow.com/q/10431317/1225328. The solution was to add the content-length of the response. – sp00m Apr 28 '14 at 11:55
  • @sp00m I am not able to get The Size of file . Can You Please help me with that – crazyStart Apr 28 '14 at 11:59

1 Answers1

4

I had a quite similar issue, please have a look at Forcing the browser to download a docx file in JAVA generates a corrupted document. The point was to add the Content-Length header of the response.

Try to make createExcelWorkbookPosition returning the file instead of the XSSFWorkbook:

public static File createExcelWorkbookPosition(String BsnsDate) throws Exception {  
    File file = new File("workbook.xlsx");
    FileOutputStream out = new FileOutputStream(file);
    // ...  
    return file;
}

Then:

File file = FileName.createExcelWorkbookPosition(request.getParameter("BSNS_DT"));
// ...
response.setContentLength((int) file.length());    

InputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
    out.write(buffer, 0, len);
}
// if using Apache IO, the above code can be simplified to IOUtils.copy(in, out);
// if using Guava, Files.copy(file, out);

// don't forget to close your streams and flush the response buffer
Community
  • 1
  • 1
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • Thanks It worked like a charm.Can you Please tell me is it necessary to Initialize the byte array with 1024 .What if I have too much data .Will it cause me a problem.Thanks a lot For your help . – crazyStart Apr 28 '14 at 12:52
  • 2
    @crazyStart This is just the size of the buffer. Have a look at http://stackoverflow.com/q/8748960/1225328 for more details. And if this answer suited your needs, don't forget to [accept](http://meta.stackoverflow.com/a/5235/186921) it ;) – sp00m Apr 28 '14 at 12:58