java 代码
以上的例子应当很清楚的写明了如何生成数据源。
对于数据源的填充,笔者使用了二个类,分别用来对应使用Connction及JavaBean Collection进行填充。
java 代码
- /**
- * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
- * @author Jimmy.Shine 2007-5-12
- */
- package cn.com.reachway.framework.report.jasperPrint;
- import java.io.File;
- import java.sql.Connection;
- import java.util.Map;
- import net.sf.jasperreports.engine.JRException;
- import net.sf.jasperreports.engine.JasperFillManager;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.JasperReport;
- import net.sf.jasperreports.engine.util.JRLoader;
- import cn.com.reachway.framework.exception.JasperReportException;
- /**
- * 使用报表模板及数据等来生成JapserPrint
- */
- public class JasperPrintWithConnection {
- /** 传入的参数 */
- private Map params;
- /** 模板文件的地址 */
- private String reportFilePath;
- /** JDBC connection */
- private Connection con;
- public Connection getCon() {
- return con;
- }
- public void setCon(Connection con) {
- this.con = con;
- }
- public Map getParams() {
- return params;
- }
- public void setParams(Map params) {
- this.params = params;
- }
- public String getReportFilePath() {
- return reportFilePath;
- }
- public void setReportFilePath(String reportFilePath) throws JasperReportException {
- if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
- throw new JasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
- this.reportFilePath = reportFilePath;
- }
- public JasperPrintWithConnection() {
- super();
- }
- public JasperPrintWithConnection(String reportFilePath, Map params, Connection con) throws JasperReportException {
- if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
- throw new JasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");
- if (con == null)
- throw new JasperReportException("Conncetion不应当为null!");
- this.setReportFilePath(reportFilePath);
- this.setParams(params);
- this.setCon(con);
- }
- /**
- * 取得JasperPrint
- * @return
- * @throws JasperReportException
- */
- public JasperPrint getJasperPrint() throws JasperReportException {
- File reportFile = new File(this.reportFilePath);
- if (!reportFile.exists())
- throw new JasperReportException("传入的模板文件不存在!");
- try {
- // Load编译好的模板
- JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
- // 进行数据填充
- JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.con);
- return jasperPrint;
- } catch (JRException jre) {
- jre.printStackTrace();
- throw new JasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");
- }
- }
- }
- /**
- * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
- * @author Jimmy.Shine 2007-5-14
- */
- package cn.com.reachway.framework.report.jasperPrint;
- import java.io.File;
- import java.util.Map;
- import net.sf.jasperreports.engine.JRDataSource;
- import net.sf.jasperreports.engine.JRException;
- import net.sf.jasperreports.engine.JasperFillManager;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.JasperReport;
- import net.sf.jasperreports.engine.util.JRLoader;
- import cn.com.reachway.framework.exception.JasperReportException;
- /**
- *
- */
- public class JasperPrintWithDataSource {
- /** 传入的参数 */
- private Map params;
- /** 模板文件的地址 */
- private String reportFilePath;
- /** dataSrouce */
- private JRDataSource dataSource;
- public JRDataSource getDataSource() {
- return dataSource;
- }
- public void setDataSource(JRDataSource dataSource) {
- this.dataSource = dataSource;
- }
- public Map getParams() {
- return params;
- }
- public void setParams(Map params) {
- this.params = params;
- }
- public String getReportFilePath() {
- return reportFilePath;
- }
- public void setReportFilePath(String reportFilePath) throws JasperReportException {
- if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
- throw new JasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
- this.reportFilePath = reportFilePath;
- }
- public JasperPrintWithDataSource() {
- super();
- }
- public JasperPrintWithDataSource(String reportFilePath, Map params, JRDataSource dataSource)
- throws JasperReportException {
- if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
- throw new JasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");
- if (dataSource == null)
- throw new JasperReportException("DataSource不应当为null!");
- this.setReportFilePath(reportFilePath);
- this.setParams(params);
- this.setDataSource(dataSource);
- }
- /**
- * 取得JasperPrint
- * @return
- * @throws JasperReportException
- */
- public JasperPrint getJasperPrint() throws JasperReportException {
- File reportFile = new File(this.reportFilePath);
- if (!reportFile.exists())
- throw new JasperReportException("传入的模板文件不存在!");
- try {
- // Load编译好的模板
- JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
- // 进行数据填充
- JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.dataSource);
- return jasperPrint;
- } catch (JRException jre) {
- jre.printStackTrace();
- throw new JasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");
- }
- }
- }