langzibo 2010-03-01 10:25
浏览 185
已采纳

serlet中不能调用POI的包吗?

报错:Source not found for ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 258

源码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        try{


            response.setContentType("application/vnd.ms-excel;charset=utf-8");
               response.setHeader("Content-Disposition", "attachment;filename="
                 + new String("测试.xls".getBytes(), "iso-8859-1"));

               ServletOutputStream out = response.getOutputStream();

               BufferedInputStream bis = null;
               BufferedOutputStream bos = null;

               try {

                bis = new BufferedInputStream(this.getInputStream());
                bos = new BufferedOutputStream(out);

                byte[] buff = new byte[2048];
                int bytesRead;

                while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                 bos.write(buff, 0, bytesRead);
                }

               } catch (final IOException e) {
                System.out.println("IOException.");
                throw e;
               } finally {
                if (bis != null)
                 bis.close();
                if (bos != null)
                 bos.close();
               }





        }catch(Exception e){

            e.printStackTrace();
        }

    }
    private InputStream getInputStream() {

              [color=red]//运行下面那一行就报错[/color]
        [color=red]  HSSFWorkbook wb = new HSSFWorkbook();[/color]
           HSSFSheet sheet = wb.createSheet("sheet1");

           HSSFRow row = sheet.createRow(0);

           HSSFCell cell = row.createCell((short) 0);



           cell.setCellValue("序号");

           cell = row.createCell((short) 1);

           cell.setCellValue("姓");

           cell = row.createCell((short) 2);

           cell.setCellValue("名");

           cell = row.createCell((short) 3);

           cell.setCellValue("年龄");

           //创建一行记录
           row = sheet.createRow(1);

           cell = row.createCell((short) 0);

           cell.setCellValue("1");

           cell = row.createCell((short) 1);
           cell.setCellValue("刘");

           cell = row.createCell((short) 2);

           cell.setCellValue("继忠");

           cell = row.createCell((short) 3);

           cell.setCellValue("25");


           ByteArrayOutputStream os = new ByteArrayOutputStream();

           try {
            wb.write(os);
           } catch (IOException e) {
            e.printStackTrace();
           }

           byte[] content = os.toByteArray();
           InputStream is = new ByteArrayInputStream(content);
           return is;
        }
  • 写回答

2条回答 默认 最新

  • weixin_42522104 2010-03-01 11:13
    关注

    经过测试,在Servlet中调用HSSFWorkbook wb = new HSSFWorkbook();是不会出错的,请确认一下你的POI的版本和所需要的LIB有没有导入到WEB工程中。

    你可以用以下代码测试正确性。该代码在我机器上正常运行。
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
    response.setHeader("Content-Disposition", "attachment;filename="
    + new String("测试.xls".getBytes(), "iso-8859-1"));
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("test");
    OutputStream out = response.getOutputStream();
    wb.write(out);
    out.flush();
    out.close();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划