您的位置:首页 > Web前端 > HTML

iReport输出pdf,html,word,excel

2013-02-07 16:33 761 查看
转载自:http://zxs19861202.iteye.com/blog/1171118 

开发报表使用ireport开发还是比较快和方便的,以下整理jsp将jasper文件生成html,word,excel,pdf格式的代码:

ireport的使用教程可以参见一下附件:

 

1、方法一、生成html格式:

 //输出html格式

   

    Map<String,String> parameters = new HashMap<String,String>();

    parameters.put("title","我们的产品");

    Connection con = null;

    try {

      con=Conmysql.getCon();

   

    //返回生成文件路径

    String file=JasperRunManager.runReportToHtmlFile(application.getRealPath     ("reports/test.jasper"), parameters, con);

    

    response.sendRedirect("reports/test.html");

    

     }

    catch (Exception e) {

      System.out.println(e);

      

    }finally{

      con.close();

    }

方法二、通过servletOutputStream直接输出

JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(),parameters,conn);
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,out);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.exportReport();
out.flush();
out.close();

 

 

2、生成PDF格式文件

 //生成pdf格式

    Map<String,String> parameters = new HashMap<String,String>();

    parameters.put("title","我们的产品");

    Connection con = null;

    try {

      con=Conmysql.getCon();

      System.out.println("连接数据库成功");

   

    byte[] bytes = JasperRunManager.runReportToPdf(application.getRealPath("reports/test.jasper"), parameters, con);

    response.setContentType("application/pdf");

    response.setContentLength(bytes.length);

    ServletOutputStream ouputStream = response.getOutputStream();

    ouputStream.write(bytes, 0, bytes.length);

    ouputStream.flush();

    ouputStream.close();

     }

    catch (Exception e) {

      System.out.println(e);

    }finally{

      con.close();

    }

 

 

 

3、生成excel格式的文件

//生成excel格式

   

    Connection con=null;

    con=Conmysql.getCon();

    Map<String,String> parameters = new HashMap<String,String>();

    parameters.put("title","我们的产品");

  

    try{

     JasperPrint jasperPrint = JasperFillManager.fillReport(application.getRealPath("reports/test.jasper"),parameters,con);

   

     ByteArrayOutputStream oStream = new ByteArrayOutputStream();

  

      JRXlsExporter exporter = new JRXlsExporter();  

    

      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

      exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);

      exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE); // 删除记录最下面的空行

      exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);// 删除多余的ColumnHeader

      exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);// 显示边框

      exporter.exportReport();

    

     byte[] bytes = oStream.toByteArray();

   

     if(bytes != null && bytes.length > 0) {

      response.reset();

      response.setContentType("application/vnd.ms-excel");

      response.setContentLength(bytes.length); 

      ServletOutputStream ouputStream = response.getOutputStream(); 

      ouputStream.write(bytes,0,bytes.length); 

      ouputStream.flush(); 

      ouputStream.close();

  

     }else{

      out.print("bytes were null!");

     }

   

     }catch(JRException ex){

     out.print("Jasper Output Error:"+ex.getMessage());

     }finally{

      con.close();

     }

 

 

 

4、生成word格式的文件:

//生成word格式

   

    Map<String,String> parameters = new HashMap<String,String>();

    parameters.put("title","我们的产品");

    Connection con = null;

    try {

      con=Conmysql.getCon();

      System.out.println("连接数据库成功");

   

    JasperPrint jasperPrint = JasperFillManager.fillReport(application.getRealPath("reports/test.jasper"),parameters,con);

   

    ByteArrayOutputStream oStrEeam = new ByteArrayOutputStream
a4b4
();

  

    JRExporter exporter = new JRRtfExporter();  

    

    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStrEeam);

    exporter.exportReport();

    

    byte[] bytes = oStrEeam.toByteArray();

   

    if(bytes != null && bytes.length > 0) {

    response.reset();

    response.setContentType("application/ms_word"); 

    response.setHeader("Content-disposition", "attachment; filename=zhiyou_erp.doc"); 

    response.setContentLength(bytes.length);

    ServletOutputStream ouputStream = response.getOutputStream();

    ouputStream.write(bytes, 0, bytes.length);

    ouputStream.flush();

    ouputStream.close();

    }

    

     }

    catch (Exception e) {

      System.out.println(e);

    }finally{

       con.close();

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: