Java spring中生成的电子表格具有错误的扩展名

Java spring中生成的电子表格具有错误的扩展名,java,spring,servlets,Java,Spring,Servlets,我正在尝试从spring应用程序生成Excel电子表格。它是使用.do扩展名而不是.xls生成的。但是如果我将下载的文件重命名为.xls,我可以在Excel中看到所有可用的内容。我的控制器代码如下 @RequestMapping(value="getReportsList.do") public ModelAndView getReports(HttpServletRequest request, HttpServletResponse response,

我正在尝试从spring应用程序生成Excel电子表格。它是使用.do扩展名而不是.xls生成的。但是如果我将下载的文件重命名为.xls,我可以在Excel中看到所有可用的内容。我的控制器代码如下

    @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;

}
@RequestMapping(value=“getReportsList.do”)
公共模型和查看getReports(HttpServletRequest请求,
HttpServletResponse,
@ModelAttribute(“ordCommand”)ordCommand ordCommand,
BindingResult错误)引发异常{
ByteArrayOutputStream byteArrayStream=新建ByteArrayOutputStream();
列表记录array=null;
试一试{
List recordsArray=ordService.getDCTrackReports();
if(null!=recordsArray){
//excel格式代码
}
setHeader(“缓存控制”、“公共”);
响应。setHeader(“Pragma”、“public”);
response.setHeader(“Expires”、“0”);
setHeader(“内容处置”、“我的报告.xls”);
response.setContentType(“application/vnd.ms excel”);
//ServletOutputStream out=response.getOutputStream()
if(bytearreastream!=null){
response.getOutputStream().write(byteArrayStream.toByteArray());
}
response.getOutputStream().flush();
bytearreastream.flush();
bytearreastream.close();
}捕获(例外e){
e、 getMessage();
}
返回null;
}

请尝试设置内容处置标题:

 response.setHeader("Content-Disposition", "inline:filename=\"my_report.xls\"");
“内联”和“附件”之间的区别:


内容处置标题

在内容处置中添加文件名,并将其添加为附件,并将内容类型添加到带有附件的响应中

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