0

我必须为用户提供 Excel 下载功能。

这是我的控制器代码片段

@RequestMapping(value = "downloadFIReport.do", method = RequestMethod.GET)
public void downloadFIReport(@RequestParam("recieptID") String recieptId,HttpServletResponse response) {
    HSSFWorkbook wb = BillExcelCreator.createFIBillExcel(recieptId);
    if(wb != null){
        //Writing file to outputstream
        try
        {
              ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
              wb.write(bos); 
              response.setContentLength(bos.size()); 
              wb.write(response.getOutputStream());
             // response.flushBuffer();
        }
        catch(IOException ex) {
            ex.printStackTrace();
        }   
    }
}

运行上述代码后,我在浏览器上打印垃圾值,而不是在 excel 文件保存/打开弹出窗口上?

是什么原因 ?有什么办法可以解决?

4

1 回答 1

3

您没有将文件设置为可下载的东西。这就是原因,它在响应中打印了一些垃圾值。

请在将以下几行添加到响应后检查。

 response.setContentType("application/vnd.ms-excel")
 response.setHeader("Content-disposition", "attachment;filename=yourFileName.xls")
于 2014-11-18T09:33:06.063 回答