0

我无法在客户端生成 Excel 文件以供下载。这是我的代码:

 <%
 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

 %>
4

1 回答 1

2

我假设您正在尝试从 JSP 文件写入生成的 xls 文件。问题可能出在空格中,如果有的话,写入 ServletOutputStream 会产生异常。

所以请检查在“<%”之前指令之间没有空格。也设置<%@ page trimDirectiveWhitespaces="true" %>以防万一。

于 2012-07-09T08:41:57.537 回答