Apache POI 的编码问题 [英] Encoding issue with Apache POI

查看:27
本文介绍了Apache POI 的编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache POI 创建 MS Excel 文件,当我在本地主机上使用它时一切正常.但是当我在 Google App Engine 上部署项目然后尝试在 MS Excel 中打开创建的文件时,我可以注意到我所有的特殊字符都变成了问号?".有没有人能告诉我为什么它在本地主机上工作但在部署后无法显示特殊字符.

I'm creating MS Excel file using Apache POI and everything works fine while I'm using it at localhost. But when I deploy project on Google App Engine and then try to open created file in MS Excel I can notice that all my special characters changed into question marks "?". Does anyone of you could tell me why it works on localhost but fail to show special character after deploying.

public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        try {
            OutputStream out = null;
            try
            {
                String dataa = req.getParameter("dataa");
                String json = URLDecoder.decode(dataa, "UTF-8");
                Gson gson = new Gson();
                ExcelData excelData = gson.fromJson(json, ExcelData.class);

                HSSFWorkbook workbook = new HSSFWorkbook();
                HSSFSheet sheet1 = (HSSFSheet) workbook.createSheet("myData");

                // workbook creation...

                ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
                workbook.write(outByteStream);
                byte [] outArray = outByteStream.toByteArray();
                res.setContentType("application/ms-excel");
                res.setContentLength(outArray.length);
                res.setHeader("Expires:", "0");
                res.setHeader("Content-Disposition", "attachment; filename=raport.xls");

                out = res.getOutputStream();
                out.write(outArray);
                out.flush();

            } catch (Exception e)
            {
                throw new ServletException("Exception in Excel Sample Servlet", e);
            } finally
            {
                if (out != null)
                    out.close();
            }
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

推荐答案

问题已解决

当您尝试调试问题一段时间然后在 stackoverflow 上发布后立即找到答案时,这很有趣:) 无论如何,问题就在这里:

It's quite funny when you try to debug problem for some time and then find the answer just after posting on stackoverflow :) Anyway problem was here:

String data = URLEncoder.encode(someString);
String data2 = URLDecoder.decode(data, "UTF-8");

因此,在使用 App Engine 本地服务器数据 == data2 处理 localhost 时,但在生产服务器数据 != data2 上工作时,区别在于编码为问号的特殊字符.

So while working on localhost with App Engine Local Server data == data2, but on production server data != data2 and the difference are special characters which are encoded as question marks.

// solution
String data = URLEncoder.encode(someString, "UTF-8");
String data2 = URLDecoder.decode(data, "UTF-8");

这篇关于Apache POI 的编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆