java 在java中从服务器下载文件后,Excel .xlsx文件未打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25782961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel .xlsx file not opening after downloading the file from the server in java
提问by navin kumar
Here is my sample code. I am using eclipse , tomcat server .Browser as IE9.
这是我的示例代码。我使用 eclipse ,tomcat server .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();
}
I am getting the List of Student. But when i am opening the downloaded file file getting error saying that file format or extention is not valid. My downloaded file is .xlsx .
我正在获取学生名单。但是当我打开下载的文件时出现错误,说文件格式或扩展名无效。我下载的文件是 .xlsx 。
回答by Glogo
I strongly recommend you to use HSSFWorkbookclass to create your excel file. After its created (for creation process see: this example) you can write its contents to response like this:
我强烈建议您使用HSSFWorkbook类来创建您的 excel 文件。在它创建之后(创建过程见:这个例子)你可以像这样写它的内容来响应:
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);
回答by Joop Eggen
It is not so much an .xlsx file, more a CSV or tab separated value text file. It fakes to be an Excel file; and yes, then Excel opens it correctly,
它与其说是 .xlsx 文件,不如说是 CSV 或制表符分隔值文本文件。伪装成Excel文件;是的,然后 Excel 正确打开它,
Try to read it with NotePad. You also can make a .xlsx file with NotePad to check whether the trick works.
尝试用记事本阅读它。你也可以用记事本制作一个 .xlsx 文件来检查这个技巧是否有效。
The following tries:
以下尝试:
- .xls
- A Windows
\r\n
(CR+LF) line ending. Maybe the server is Linux and delivers\n
(LF). - A defined encoding.
- .xls
- Windows
\r\n
(CR+LF) 行结束。也许服务器是 Linux 并提供\n
(LF)。 - 定义的编码。
Then
然后
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();