java JExcel中的编码问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5701743/
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
Encoding problem in JExcel
提问by Javi
I am loading an excel file in an GAE/Java application with JExcel like this:
我正在使用 JExcel 在 GAE/Java 应用程序中加载一个 excel 文件,如下所示:
The html form to upload the file islike this:
上传文件的html表单是这样的:
<form id="" action="/save" method="post" enctype="multipart/form-data" accept-charset="ISO-8859-1">
<input name="file" type="file" value="load"/>
<input type="submit"value="load excel"/>
</form>
and in the server I have:
在服务器中我有:
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (!item.isFormField()) {
//if it's not a form field it's a file
Workbook workbook = Workbook.getWorkbook(stream);
...
String name = sheet.getCell(COL_NUMBER, row).getContents();
}
}
The problem is that if I write in the cell something like 'city ó' when it reads in the server the variable name is ' city ?'. The encoding is not OK.
问题是,如果我在单元格中写入类似 ' cityó' 的内容,当它在服务器中读取时,变量名称是 ' city ? '。编码不正常。
I've tried to change accept-charset="ISO-8859-1" (setting it to utf-8 or removing it) but with no success.
我尝试更改 accept-charset="ISO-8859-1" (将其设置为 utf-8 或删除它)但没有成功。
Can anyone tell me how could I solve this problem.
谁能告诉我如何解决这个问题。
Thanks
谢谢
回答by Javi
OK, I got it by doing this:
好的,我通过这样做得到了它:
WorkbookSettings ws = new WorkbookSettings();
ws.setEncoding("Cp1252");
Workbook workbook = Workbook.getWorkbook(stream, ws);
回答by Nick Robson
WorkbookSettings will look for system property jxl.encoding
WorkbookSettings 将查找系统属性 jxl.encoding
If you don't have easy access to WorkbookSettings (i.e. coming from Drools- ExcelParser) you might find this preferable.
如果您无法轻松访问 WorkbookSettings(即来自 Drools-ExcelParser),您可能会发现这更可取。
回答by Gagravarr
First up, make sure you're using a recent version of POI (something like 3.7 or 3.8 beta 2). Very old versions of POI did have encoding problems, but as long as you're on a new one then that shouldn't be your issue.
首先,确保您使用的是最新版本的 POI(例如 3.7 或 3.8 beta 2)。非常旧版本的 POI 确实存在编码问题,但只要您使用的是新版本,那么这应该不是您的问题。
Next, on your local machine, run something like org.apache.poi.hssf.extractor.ExcelExtractor against the file. This will let you confirm that POI is handling the encoding correctly. Run it with
接下来,在您的本地计算机上,针对该文件运行类似 org.apache.poi.hssf.extractor.ExcelExtractor 之类的内容。这将让您确认 POI 正在正确处理编码。运行它
java -classpath poi-3.8-beta2.jar org.apache.poi.hssf.extractor.ExcelExtractor --show-sheet-names Y -i MyExcel.xls
Assuming that works fine, then you know your issue is within Google App Engine.
假设工作正常,那么您就知道您的问题出在 Google App Engine 中。