写一个POI工作簿输出流产生怪异值 [英] writing a poi workbook to output stream is generating weird values

查看:125
本文介绍了写一个POI工作簿输出流产生怪异值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标创建一个Excel文件,供用户通过Apache POI下载。

i am aiming to create an Excel file for the user to download via apache poi.

我有这样的code在我的servlet:

I have this code in my servlet:

protected void doGet(HttpServletRequest request, 
              HttpServletResponse response) throws ServletException, IOException 
      {

            // create a workbook , worksheet
            Workbook wb = new HSSFWorkbook();
            Sheet sheet = wb.createSheet("MySheet");
            CreationHelper createHelper = wb.getCreationHelper();

            // Create a row and put some cells in it. Rows are 0 based.
            Row row = sheet.createRow((short)0);
            Cell cell = row.createCell(0);
            cell.setCellValue(1);
            row.createCell(1).setCellValue(1.2);
            row.createCell(2).setCellValue( createHelper.createRichTextString("This is a string") );
            row.createCell(3).setCellValue(true);

            //write workbook to outputstream
            ServletOutputStream out = response.getOutputStream();
            wb.write(out);
            out.flush();
            out.close();

            //offer the user the option of opening or downloading the resulting Excel file
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls");

问题是,我得到这些奇怪的值:

The problem is that i am getting these weird values:

`...的MySheetŒ®üÿ»IAW¯¯
  Dü©ñÒMbP?
的* +,€%ÿÁƒ¡D ,,à?à?

`…MySheetŒ®üÿ » ÌÁ w dü©ñÒMbP?*+‚€%ÿÁƒ„¡"d,,à?à?

有什么建议?

推荐答案

发现什么是错。 HttpServletResponse的必须首先别的之前设置

found whats wrong. the HttpServletResponse must first be set before anything else

//offer the user the option of opening or downloading the resulting Excel file
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls");

这篇关于写一个POI工作簿输出流产生怪异值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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