0

我在我的 spring mvc3 项目中使用 Apache POI 生成一个 Excel 表。生成的文件没有 .xls 扩展名。如果我用 .xls 手动重命名同一个文件,则数据生成良好。这是我的代码片段::

@RequestMapping(value = "/download/")
    public void downloadMerchantMiniStatement(final HttpServletResponse response,
            @RequestParam(value = "fromDate", required = true) String fromDate,
            @RequestParam(value = "toDate", required = true) String toDate,
            @RequestParam(value = "status", required = false) String status
            ) throws IOException {

        String fileName = STATEMENT_REPORT_" + getDateString(new Date()) + ".xls";
        List<TransactionDTO> transactionDtos = excelService.getTransactionsForExcel(Status, DateUtil.convertStringToDate(fromDate), DateUtil.convertStringToDate(toDate));

        ByteArrayOutputStream excel = getExcelStatement(transactionDtos, fromDate, toDate, status);
        excel.writeTo(response.getOutputStream());
        response.setContentType("application/excel");
        response.setHeader("Expires:", "0");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }
4

1 回答 1

0

终于找到解决办法了,代码 response.setContentType("application/excel"); 需要换成response.setContentType("application/vnd.ms-excel");

这给出了标准格式的输出。

于 2013-05-15T01:52:34.370 回答