【问题标题】:Excel .xlsx file not opening after downloading the file from the server in java [duplicate]在java中从服务器下载文件后Excel .xlsx文件未打开[重复]
【发布时间】:2014-11-05 03:18:29
【问题描述】:

这是我的示例代码。我正在使用eclipse,tomcat服务器.Browser作为IE9。

protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");


        ServletContext context = request.getServletContext();
        @SuppressWarnings("unchecked")
        List<Student> students = (List<Student>) context.getAttribute("students");
        PrintWriter out = response.getWriter();
        for(Student student:students){
            out.println(student.getId()+"\t"+student.getName());
        }
        out.close();

    }

我正在获取学生名单。但是当我打开下载的文件文件时,出现错误,指出文件格式或扩展名无效。我下载的文件是 .xlsx 。

【问题讨论】:

  • 您发送的不是 xslx 文件。您发送带有制表符作为分隔符的 csv。
  • 试试 application/vnd.ms-excel
  • @Jens 你从哪里知道他发送了一个 csv 文件??
  • @navin xlsx 文件通常是一个 zip 存档。你测试过它没有损坏吗?

标签: java excel jakarta-ee servlets


【解决方案1】:

我强烈建议您使用HSSFWorkbook 类来创建您的excel 文件。创建后(创建过程参见:this example),您可以将其内容写入响应,如下所示:

Workbook workbook = new XSSFWorkbook();

// Add sheet(s), colums, cells and its contents to your workbook here ...

// First set response headers
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=YourFilename.xlsx");

// Get response outputStream
ServletOutputStream outputStream = response.getOutputStream();

// Write workbook data to outputstream
workbook.write(outputStream);

【讨论】:

  • 这样我将创建一个 .xls 文件而不是 .xlsx 文件。在这里我想创建一个 .xlsx 文件
  • 抱歉,我误读了该要求。然后你只需使用 XSSFWorkbook 类,并更改 YourFilename extension to xlsx. Both HSSFWorkbook` 和 XSSFWorkbook 正在实现 Wokbook 接口,所以它的 api 将是相同的。见poi.apache.org/spreadsheet/quick-guide.html更新答案
【解决方案2】:

与其说是 .xlsx 文件,不如说是 CSV 或制表符分隔值文本文件。它伪装成 Excel 文件;是的,然后 Excel 会正确打开它,

尝试使用记事本阅读。您还可以使用记事本制作一个 .xlsx 文件,以检查该技巧是否有效。

以下尝试:

  • .xls
  • Windows \r\n (CR+LF) 行结束。也许服务器是 Linux 并提供 \n (LF)。
  • 定义的编码。

然后

    response.setEncoding("UTF-8");
    response.setContentType("application/vnd.ms-excel");

    ServletContext context = request.getServletContext();
    @SuppressWarnings("unchecked")
    List<Student> students = (List<Student>) context.getAttribute("students");
    PrintWriter out = response.getWriter();
    out.print("\uFEFF"); // UTF-8 BOM, redundant and ugly
    for(Student student:students){
        out.printf("%s\t%s\r\n", student.getId(), student.getName());
    }
    //out.close();

【讨论】:

  • 我保留了 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");不变。
  • 当我在 notepad++ 中打开同一个下载的 (.xlsx) 文件时,可以查看内容。
  • response.setHeader("Content-Disposition", "filename=\"test.xsl\""; 左右?通过双击将(下载、编辑的)文本文件尝试为 .xsl 和 .xslx。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 2016-10-22
  • 2012-04-25
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
相关资源
最近更新 更多