Spring generated table has wrong extension

I am trying to create an Excel spreadsheet from a spring application. It is generated with .do extension instead of .xls. But if I rename the uploaded file to .xls, I can see all my stuff 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;

}

      

+3


source to share


2 answers


Try setting the Content-Disposition header:

 response.setHeader("Content-Disposition", "inline:filename=\"my_report.xls\"");

      



Differences between "inline" and "attachment": Content-Disposition: What are the differences between "inline" and "attachment "? and "attachment",

Content-Disposition header https://tools.ietf.org/html/rfc6266

+1


source


Add file name to Content-Disposition and add it as attachment and add content type in response with attachment,

 response.setHeader("Content-Disposition","attachment; filename=my_report.xls");
 response.setHeader("Content-Type", "application/vnd.ms-excel");

      



That's for sure.

+1


source







All Articles