I am trying to generate an Excel spreadsheet from spring application. It is getting generated with a .do extension instead of .xls. But if I rename the downloaded file to .xls, I can see all my content available in Excel. My controller code is below.
@RequestMapping(value="getReportsList.do")
public ModelAndView getReports(HttpServletRequest request,
HttpServletResponse response,
@ModelAttribute("ordCommand") OrdCommand ordCommand,
BindingResult errors) throws Exception {
ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
List<Object[]> recordsArray = null;
try {
List<ReportVO> recordsArray = ordService.getDCTrackReports();
if (null != recordsArray ) {
//excel formatting code
}
response.setHeader("Cache-Control", "public");
response.setHeader("Pragma", "public");
response.setHeader("Expires", "0");
response.setHeader("Content-Disposition", "my_report.xls");
response.setContentType("application/vnd.ms-excel");
// ServletOutputStream out = response.getOutputStream()
if (byteArrayStream != null) {
response.getOutputStream().write(byteArrayStream.toByteArray());
}
response.getOutputStream().flush();
byteArrayStream.flush();
byteArrayStream.close();
} catch (Exception e) {
e.getMessage();
}
return null;
}