Jasperreport5.6.0生成PDF(二)


Jasperreport5.6.0生成PDF

上一篇讲的是 jasperreport5.6.0 生成 pdf ,运行后可以生成 pdf ,可是和我们的需求有点差距,我们是要求生成后,可以直接打开或保存,这就需要 response 的设置了,如下代码:

   //ireport生成项目立项PDF格式

   public String getIreportPDFAction()

   {

      Connection conn =null;//连接

      try {

         String projectapproid =request.getParameter("projectapproid");

          ////连接JDBC得到数据URL

          String url ="jdbc:oracle:thin:@192.168.1.104:1521:orcl";

          Class.forName("oracle.jdbc.driver.OracleDriver");

          conn = DriverManager.getConnection(url,"zwjx","zwjx");

          //设置参数

          Map parmerters = new HashMap();

          parmerters.put("projectapproid", projectapproid);

         //读取报表模板文件

         FilejasperFile=new File("D:/Workspaces/zwjxSystem/WebRoot/jasper/report1.jasper");

         //设置报表中参数的值

         Map map =new HashMap();

         map.put("projectapproid", projectapproid);

          //输出流

          OutputStream ous =response.getOutputStream();

          //输出文件名

          String filenames="项目立项";

          //打印

          JasperPrint print =JasperFillManager.fillReport(jasperFile.getPath(), map,conn);

//        //生成pdf

          JRAbstractExporter exporter =newJRPdfExporter();

          response.reset();

          response.setContentType("application/pdf");

          response.setHeader("Content-Disposition","attachment;filename="+new String(filenames.getBytes("gbk"),"iso8859-1")+".pdf");

          //指向

          JasperReportsUtils.render(exporter, print,ous);

          ous.flush();

          ous.close();

      }catch (Exceptione) {

          e.printStackTrace();

          System.out.println("ireport生成报表出错!");

      }finally

      {

          try {

             conn.close();

          }catch(SQLException e) {

             //TODO Auto-generated catch block

             e.printStackTrace();

          }

      }

          returnnull;

   }

运行后,弹出如下对话框,我们可以根据需要选择。可以直接打开,也可以保存。



你可能感兴趣的:(Jasperreport5.6.0生成PDF(二))