0

I am unable to generate an Excel file for download on the client side. This is the code I have:

 <%
 try{
  //Getting HSSFWorbook object from controller
    HSSFWorkbook wb= (HSSFWorkbook)pageContext.findAttribute("wb");
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-   Disposition","attachment;
    filename=ESTHotelPerfByMonthExcelReport.xls"
    );
  //Writing to output Stream 

    ServletOutputStream outputStream = response.getOutputStream();
    wb.write(outputStream);
    outputStream.flush();
 }//closing try
 catch (IOException ioe) { 
 }
}//closing if


//The above code is not generating any 
// excel sheet however if i write the output to 
// a    excel file it shows the all the data

 %>
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • I actually already answered this question previously. See http://stackoverflow.com/questions/11226603/create-an-excel-file-for-users-to-download-using-apache-poi-jsp/11249051#11249051 – Howard Schutzman Jul 10 '12 at 05:44

1 Answers1

2

I assume that you are trying to write the generated xls file from a JSP file. The problem might be in the whitespaces, if there are some, then write to ServletOutputStream will produce exception.

So please check that before "<%" there are no whitespace between directives. Also set <%@ page trimDirectiveWhitespaces="true" %> just in case.

Alexey A.
  • 1,389
  • 1
  • 11
  • 20