接JasperReport

java 代码

以上的例子应当很清楚的写明了如何生成数据源。
对于数据源的填充,笔者使用了二个类,分别用来对应使用Connction及JavaBean Collection进行填充。

java 代码
 
  1. /**  
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.  
  3.  * @author Jimmy.Shine 2007-5-12  
  4.  */  
  5. package cn.com.reachway.framework.report.jasperPrint;   
  6.   
  7. import java.io.File;   
  8. import java.sql.Connection;   
  9. import java.util.Map;   
  10.   
  11. import net.sf.jasperreports.engine.JRException;   
  12. import net.sf.jasperreports.engine.JasperFillManager;   
  13. import net.sf.jasperreports.engine.JasperPrint;   
  14. import net.sf.jasperreports.engine.JasperReport;   
  15. import net.sf.jasperreports.engine.util.JRLoader;   
  16. import cn.com.reachway.framework.exception.JasperReportException;   
  17.   
  18. /**  
  19.  * 使用报表模板及数据等来生成JapserPrint  
  20.  */  
  21. public class JasperPrintWithConnection {   
  22.  /** 传入的参数 */  
  23.  private Map params;   
  24.  /** 模板文件的地址 */  
  25.  private String reportFilePath;   
  26.  /** JDBC connection */  
  27.  private Connection con;   
  28.   
  29.  public Connection getCon() {   
  30.   return con;   
  31.  }   
  32.   
  33.  public void setCon(Connection con) {   
  34.   this.con = con;   
  35.  }   
  36.   
  37.  public Map getParams() {   
  38.   return params;   
  39.  }   
  40.   
  41.  public void setParams(Map params) {   
  42.   this.params = params;   
  43.  }   
  44.   
  45.  public String getReportFilePath() {   
  46.   return reportFilePath;   
  47.  }   
  48.   
  49.  public void setReportFilePath(String reportFilePath) throws JasperReportException {   
  50.   if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))   
  51.    throw new JasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");   
  52.   this.reportFilePath = reportFilePath;   
  53.  }   
  54.   
  55.  public JasperPrintWithConnection() {   
  56.   super();   
  57.  }   
  58.   
  59.  public JasperPrintWithConnection(String reportFilePath, Map params, Connection con) throws JasperReportException {   
  60.   if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))   
  61.    throw new JasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");   
  62.   if (con == null)   
  63.    throw new JasperReportException("Conncetion不应当为null!");   
  64.   this.setReportFilePath(reportFilePath);   
  65.   this.setParams(params);   
  66.   this.setCon(con);   
  67.  }   
  68.     
  69.  /**  
  70.   * 取得JasperPrint  
  71.   * @return  
  72.   * @throws JasperReportException  
  73.   */  
  74.  public JasperPrint getJasperPrint() throws JasperReportException {   
  75.   File reportFile = new File(this.reportFilePath);   
  76.   if (!reportFile.exists())   
  77.    throw new JasperReportException("传入的模板文件不存在!");   
  78.   
  79.   try {   
  80.    // Load编译好的模板   
  81.    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());   
  82.    // 进行数据填充   
  83.    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.con);   
  84.    return jasperPrint;   
  85.   
  86.   } catch (JRException jre) {   
  87.    jre.printStackTrace();   
  88.    throw new JasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");   
  89.   }   
  90.   
  91.  }   
  92. }   
  93.   

 
  1. /**  
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.  
  3.  * @author Jimmy.Shine 2007-5-14  
  4.  */  
  5. package cn.com.reachway.framework.report.jasperPrint;   
  6.   
  7. import java.io.File;   
  8. import java.util.Map;   
  9.   
  10. import net.sf.jasperreports.engine.JRDataSource;   
  11. import net.sf.jasperreports.engine.JRException;   
  12. import net.sf.jasperreports.engine.JasperFillManager;   
  13. import net.sf.jasperreports.engine.JasperPrint;   
  14. import net.sf.jasperreports.engine.JasperReport;   
  15. import net.sf.jasperreports.engine.util.JRLoader;   
  16. import cn.com.reachway.framework.exception.JasperReportException;   
  17.   
  18. /**  
  19.  *   
  20.  */  
  21. public class JasperPrintWithDataSource {   
  22.  /** 传入的参数 */  
  23.  private Map params;   
  24.  /** 模板文件的地址 */  
  25.  private String reportFilePath;   
  26.  /** dataSrouce */  
  27.  private JRDataSource dataSource;   
  28.   
  29.  public JRDataSource getDataSource() {   
  30.   return dataSource;   
  31.  }   
  32.   
  33.  public void setDataSource(JRDataSource dataSource) {   
  34.   this.dataSource = dataSource;   
  35.  }   
  36.   
  37.  public Map getParams() {   
  38.   return params;   
  39.  }   
  40.   
  41.  public void setParams(Map params) {   
  42.   this.params = params;   
  43.  }   
  44.   
  45.  public String getReportFilePath() {   
  46.   return reportFilePath;   
  47.  }   
  48.   
  49.  public void setReportFilePath(String reportFilePath) throws JasperReportException {   
  50.   if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))   
  51.    throw new JasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");   
  52.   this.reportFilePath = reportFilePath;   
  53.  }   
  54.   
  55.  public JasperPrintWithDataSource() {   
  56.   super();   
  57.  }   
  58.   
  59.  public JasperPrintWithDataSource(String reportFilePath, Map params, JRDataSource dataSource)   
  60.    throws JasperReportException {   
  61.   if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))   
  62.    throw new JasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");   
  63.   if (dataSource == null)   
  64.    throw new JasperReportException("DataSource不应当为null!");   
  65.   this.setReportFilePath(reportFilePath);   
  66.   this.setParams(params);   
  67.   this.setDataSource(dataSource);   
  68.  }   
  69.  /**  
  70.   * 取得JasperPrint  
  71.   * @return  
  72.   * @throws JasperReportException  
  73.   */  
  74.  public JasperPrint getJasperPrint() throws JasperReportException {   
  75.   File reportFile = new File(this.reportFilePath);   
  76.   if (!reportFile.exists())   
  77.    throw new JasperReportException("传入的模板文件不存在!");   
  78.   
  79.   try {   
  80.    // Load编译好的模板   
  81.    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());   
  82.    // 进行数据填充   
  83.    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.dataSource);   
  84.    return jasperPrint;   
  85.   
  86.   } catch (JRException jre) {   
  87.    jre.printStackTrace();   
  88.    throw new JasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");   
  89.   }   
  90.   
  91.  }   
  92. }   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值