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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制