Java SpringMVC,Excel文件下载,损坏文件

Java SpringMVC,Excel文件下载,损坏文件,java,excel,spring,spring-mvc,Java,Excel,Spring,Spring Mvc,我正在我的一个网络应用程序中开发excel导出功能。我设置了一个小测试用例并让下载工作正常,但xlsx文件已损坏,不知道还能尝试什么。如果我将excel写入文件,它将毫无问题地打开,因此下载时一定会发生错误 设置: spring mvc 3.2.7 poi 3.10.1 Tomcat 8.0 控制器方法: @RequestMapping(value = "/download", method = RequestMethod.GET) public ModelAndView downloadExc

我正在我的一个网络应用程序中开发excel导出功能。我设置了一个小测试用例并让下载工作正常,但xlsx文件已损坏,不知道还能尝试什么。如果我将excel写入文件,它将毫无问题地打开,因此下载时一定会发生错误

设置:

spring mvc 3.2.7 poi 3.10.1 Tomcat 8.0

控制器方法:

@RequestMapping(value = "/download", method = RequestMethod.GET)
public ModelAndView downloadExcel() {
    // create some sample data
    List<Book> listBooks = new ArrayList<Book>();
    listBooks.add(new Book("Effective Java", "Joshua Bloch", "0321356683",
            "May 28, 2008", 38.11F));
    listBooks.add(new Book("Head First Java", "Kathy Sierra & Bert Bates",
            "0596009208", "February 9, 2005", 30.80F));
    listBooks.add(new Book("Java Generics and Collections",
            "Philip Wadler", "0596527756", "Oct 24, 2006", 29.52F));
    listBooks.add(new Book("Thinking in Java", "Bruce Eckel", "0596527756",
            "February 20, 2006", 43.97F));
    listBooks.add(new Book("Spring in Action", "Craig Walls", "1935182358",
            "June 29, 2011", 31.98F));

    // return a view which will be resolved by an excel view resolver
    return new ModelAndView(new ExcelBuilder(listBooks));
}

当这是二进制数据时,字符集将被设置,这让我很困惑。这可能是问题所在吗?

不要返回ModelAndView,只需将excel文件写入响应的输出流

@RequestMapping(value = "/download", method = RequestMethod.GET)
@ResponseBody
public Object downloadExcel(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition",
                "attachment; filename=" + theFileNameString + ".xls");
        try {
            generateExcel(response.getOutputStream());
        } catch (IOException e) {
            System.out.println("ERROR: " + e);
        }
    return null;
}

检查所有刷新/关闭的流

我建议使用现有的解决方案,而不是自己尝试处理响应流

我会用你的
AbstractPOIExcelView
来做这项工作。检查灵感


对于Spring 4.2或更新版本,请使用(或),因为原始的
AbstractExcelView
已被弃用。

谢谢您的回答,StanislavL。我尝试了你的解决方案,就像你发布的一样,它产生了相同的结果。无法使用Excel 2010打开该文件。响应头看起来也很相似。
public class ExcelBuilder extends AbstractPOIExcelView {

private List<Book> listBooks;

public ExcelBuilder(List<Book> books) {
    this.listBooks = books;
}

@Override
protected void buildExcelDocument(Map<String, Object> model, XSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Sheet sheet = workbook.createSheet("Java Books");
    sheet.setDefaultColumnWidth(30);

    Row header = sheet.createRow(0);
    header.createCell(0).setCellValue("Book Title");
    header.createCell(1).setCellValue("Author");
    header.createCell(2).setCellValue("ISBN");
    header.createCell(3).setCellValue("Published Date");
    header.createCell(4).setCellValue("Price");

    // create data rows
    int rowCount = 1;

    for (Book aBook : listBooks) {
        Row aRow = sheet.createRow(rowCount++);
        aRow.createCell(0).setCellValue(aBook.getTitle());
        aRow.createCell(1).setCellValue(aBook.getAuthor());
        aRow.createCell(2).setCellValue(aBook.getIsbn());
        aRow.createCell(3).setCellValue(aBook.getPublishedDate());
        aRow.createCell(4).setCellValue(aBook.getPrice());
    }
}
}
 Cache-Control:private, must-revalidate
 Content-Disposition:attachment;filename="filename.xlsx"
 Content-Language:de-DE
 Content-Length:3778
 Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=ISO-8859-1
 Date:Wed, 19 Nov 2014 12:52:05 GMT
 Expires:Thu, 01 Jan 1970 00:00:00 GMT
 Pragma:private
 Set-Cookie:JSESSIONID=07F50FF2B63D4003311DE222782C4E89; Path=/abc/; HttpOnly
 X-Content-Type-Options:nosniff
 X-Frame-Options:DENY
 X-XSS-Protection:1; mode=block
@RequestMapping(value = "/download", method = RequestMethod.GET)
@ResponseBody
public Object downloadExcel(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition",
                "attachment; filename=" + theFileNameString + ".xls");
        try {
            generateExcel(response.getOutputStream());
        } catch (IOException e) {
            System.out.println("ERROR: " + e);
        }
    return null;
}