Spring MVC : HSSFWorkbook.write(ServletOutputStream) prints garbage value on browser

938 views Asked by At

I have to give a functionality to user for excel download.

Here is my controller code snippet

@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();
        }   
    }
}

After running the above code i get garbage value printed on browser instead on excel file save/open pop-up ?

What is the reason ? What can be a solution ?

1

There are 1 answers

0
Srinivas B On BEST ANSWER

You are not setting the file to be a downloadable thing. Thats the reason, it is printing some garbage value in the response.

Please check after adding the below lines to the response.

 response.setContentType("application/vnd.ms-excel")
 response.setHeader("Content-disposition", "attachment;filename=yourFileName.xls")