JExcel中的编码问题

发布于 2024-11-02 11:02:38 字数 1031 浏览 4 评论 0原文

我正在使用 JExcel 在 GAE/Java 应用程序中加载一个 excel 文件,如下所示:

上传文件的 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>

在服务器中我有:

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();
    }
}

问题是,如果我在单元格中写入类似 ' city ó',当它在服务器中读取变量名称为“city ?”时。编码不正确。

我尝试更改accept-charset =“ISO-8859-1”(将其设置为utf-8或将其删除)但没有成功。

谁能告诉我如何解决这个问题。

谢谢

I am loading an excel file in an GAE/Java application with JExcel like this:

The html form to upload the file islike this:

<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.

I've tried to change accept-charset="ISO-8859-1" (setting it to utf-8 or removing it) but with no success.

Can anyone tell me how could I solve this problem.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑着哭最痛 2024-11-09 11:02:38

好的,我通过这样做得到了它:

WorkbookSettings ws = new WorkbookSettings();
ws.setEncoding("Cp1252");
Workbook workbook = Workbook.getWorkbook(stream, ws);

OK, I got it by doing this:

WorkbookSettings ws = new WorkbookSettings();
ws.setEncoding("Cp1252");
Workbook workbook = Workbook.getWorkbook(stream, ws);
深海夜未眠 2024-11-09 11:02:38

WorkbookSettings 将查找系统属性 jxl.encoding

如果您无法轻松访问 WorkbookSettings(即来自 Drools- ExcelParser),您可能会发现这更好。

WorkbookSettings will look for system property jxl.encoding

If you don't have easy access to WorkbookSettings (i.e. coming from Drools- ExcelParser) you might find this preferable.

长伴 2024-11-09 11:02:38

首先,确保您使用的是最新版本的 POI(例如 3.7 或 3.8 beta 2)。非常旧的 POI 版本确实存在编码问题,但只要您使用的是新版本,那么这就不应该是您的问题。

接下来,在本地计算机上,针对该文件运行 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

假设工作正常,那么您就知道您的问题出在 Google App Engine 中。

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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文