问题编码 java->xls
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2968237/
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
Issue encoding java->xls
提问by Xerg
This is not a pure java question and can also be related to HTML
这不是纯java问题,也可能与HTML有关
I've written a java servlet that queries a database table and shows the result as a html table. The user can also ask to receive the result as an Excel sheet. Im creating the Excel sheet by printing the same html table, but with the content-type of "application/vnd.ms-excel". The Excel file is created fine. The problem is that the tables may contain non-english data so I want to use a UTF-8 encoding.
我编写了一个 java servlet,它查询数据库表并将结果显示为 html 表。用户还可以要求以 Excel 表格的形式接收结果。我通过打印相同的 html 表来创建 Excel 工作表,但内容类型为“application/vnd.ms-excel”。Excel 文件创建得很好。问题是这些表可能包含非英文数据,所以我想使用 UTF-8 编码。
PrintWriter out = response.getWriter();
response.setContentType("application/vnd.ms-excel:ISO-8859-1");
//response.setContentType("application/vnd.ms-excel:UTF-8");
response.setHeader("cache-control", "no-cache");
response.setHeader("Content-Disposition", "attachment; filename=file.xls");
out.print(src);
out.flush();
The non-english characters appear as garbage (áéíóú)
非英文字符显示为垃圾 (áéíóú)
Also I tried converting to bytes from String
我也尝试从字符串转换为字节
byte[] arrByte = src.getBytes("ISO-8859-1");
String result = new String(arrByte, "UTF-8");
But I Still getting garbage, What can I do?. Thanks
但我仍然收到垃圾,我该怎么办?。谢谢
UPDATE: if I open the excel file in notepad + + the type of file encoding is "UTF-8 without BOM", if I change the encoding to "UTF-8" and then open the file in Excel, the characters "áéíóú" look good.
更新:如果我在记事本+ +中打开excel文件,文件编码类型是“UTF-8 without BOM”,如果我将编码更改为“UTF-8”,然后在Excel中打开文件,字符“áéíóú”看起来不错。
回答by mdma
Excel is a binary format, not a text format, so you should not need to set any encoding, since it simply doesn't apply. Whatever system you are using to build the excel file (e.g. Apache Poi) will take care of the encoding of text within the excel file.
Excel 是二进制格式,而不是文本格式,因此您不需要设置任何编码,因为它根本不适用。无论您使用什么系统来构建 excel 文件(例如 Apache Poi),都会处理 excel 文件中文本的编码。
You should not try to convert the recieved bytes to a string, just store them in a byte array or write them out to a file.
您不应该尝试将接收到的字节转换为字符串,只需将它们存储在字节数组中或将它们写出到文件中。
EDIT: from the comment, it doesn't sound as if you are using a "real" binary excel file, but a tab delimited text file (CSV). In that case, make sure you use consistent encoding, e.g UTF-8 throughout.
编辑:从评论来看,听起来好像您使用的是“真正的”二进制 Excel 文件,而不是制表符分隔的文本文件 (CSV)。在这种情况下,请确保使用一致的编码,例如 UTF-8。
Also, before calling response.getWriter(), call setContentTypefirst.
此外,之前调用response.getWriter(),调用setContentType第一。
See HttpServletResponse.getPrintWriter()
参见HttpServletResponse.getPrintWriter()
EDIT: You can try writing the BOM. It's normally not required, but file format handling in Office is far from normal...
编辑:您可以尝试编写 BOM。通常不需要,但 Office 中的文件格式处理远非正常......
Java doesn't really have support for the BOM. You'll have to fake it. It means that you need to use the response outputStream rather than writer, since you need to write raw bytes (the BOM). So you change your code to this:
Java 并不真正支持 BOM。你必须伪造它。这意味着您需要使用响应 outputStream 而不是 writer,因为您需要写入原始字节(BOM)。因此,您将代码更改为:
response.setContentType("application/vnd.ms-excel:UTF-8");
// set other headers also, "cache-control" etc..
OutputStream outputStream = response.getOutputStream();
outputStream.write(0xEF); // 1st byte of BOM
outputStream.write(0xBB);
outputStream.write(0xBF); // last byte of BOM
// now get a PrintWriter to stream the chars.
PrintWriter out = new PrintWriter(new OutputStreamWriter(outputStream,"UTF-8"));
out.print(src);
回答by Michael
Try using the ServletResponse.setCharacterEncoding(java.lang.String charset)method.
尝试使用该ServletResponse.setCharacterEncoding(java.lang.String charset)方法。
response.setCharacterEncoding("UTF-8");
回答by Jefin Stephan
I had the same issue.. i fixed it with using print() instead of write()
我有同样的问题..我用print()而不是write()修复了它
outputStream.print('\ufeff');
回答by invaderkay
Do you get "garbage" when you print result to standard output?
将结果打印到标准输出时是否会出现“垃圾”?
Edit (code in code tags from the comment below):
response.setContentType("application/vnd.ms-excel; charset=UTF-8")
编辑(来自下面评论的代码标签中的代码):
response.setContentType("application/vnd.ms-excel; charset=UTF-8")

