ireport 如何将网址作为数据源制作出二维码,代码怎么写。 30
1个回答
展开全部
一、 在iReport中可视化定制模板
定制报表格式有二种方式,一种就是写jrxml文件,其实就是xml文件,只不过是后缀名不一样罢了。另一种方式更直接,就是生成一个JasperDesign类的实例,在japsperDesign中自己定义模板。jrxml文件也是通过一个JRXmlLoad加载过来,转成JasperDesign类的实例。也就是说写jrxml文件还需要进行解析,加载。现实中我们使用的报表一般格式比较固定,因而可以通过先使用iReport工具生成模板,再加载解析的方式。这种方式简单,而且可见性强。
iReport做为一个优秀的报表设计器,有着功能非常强大的特性。作为开源的Java程序,不但有适合于Windows安装的应用程序,同时,还提供完全开放的源代码,可供参考及原理分析。在本文中,主要通过图形界面中的模板设计,以及与数据库的连接等一系列的操作,来介绍如何定制一定要求的报表模板。
通过iReport可初见化的图形界面,可以设计出各种各样的简单或复杂的报表。通过iReport的这种可视化界面设计,可以为JasperReport提供优秀的报表模板,而无须去理解或是掌握那些复杂的XML语法。如此则可以Web报表开发节省大量的开发时间。
在进行iReport模板设计之前,需要编写JavaBean类:MonthlySalesBean.java,代码如下:
import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class MonthlySalesBean { private int employeeID;; private String last = null; private String first = null; private BigDecimal total = null; private List sales = null; private LatestSale latestSale = null; public static List createBeanCollection () { List list = new ArrayList (); MonthlySalesBean msb = new MonthlySalesBean (); msb.setEmployeeID(1); msb.setFirst("John"); msb.setLast("Doe"); msb.setTotal(new BigDecimal ("1600.50")); LatestSale ls = new LatestSale (); ls.setAmount(new BigDecimal ("32.21")); msb.setLatestSale(ls); list.add(msb); return list; } public int getEmployeeID() { return employeeID; } public void setEmployeeID(int employeeID) { this.employeeID = employeeID; } public String getFirst() { return first; } public void setFirst(String first) { this.first = first; } public String getLast() { return last; } public void setLast(String last) { this.last = last; } public BigDecimal getTotal() { return total; } public void setTotal(BigDecimal total) { this.total = total; } public List getSales() { return sales; } public void setSales(List sales) { this.sales = sales; } public LatestSale getLatestSale() { return latestSale; } public void setLatestSale(LatestSale latestSale) { this.latestSale = latestSale; } }
将上面的类打成一个jar包,并置于classpath目录下,则iReport可以进行访问。使用JavaBean做为数据源,为了在设计报表时能够看到数据,在程序中要为iReport提供一个静态方法,该方法返回上面定义JavaBean的一个结果集,这个静态方法可能在程序运行中并不是必须的,但是在iReport中它确实必须的,换句话说,这个静态方法是专门为iReport量身定做的,为了iReport在设计报表时能够调用这个静态方法返回相应的JavaBean结果集,以便设计的报表放在Java项目中之前就能像使用SQL数据库数据源一样可以浏览。在iReport中先进行数据源的连接配置,此处采用是JavaBeans set data source连接方式:
图2.在iReport进行数据源的连接
二、 处理iBati返回数据
如果iBATIS没有采用JavaBean作为返回对象,则可以采用java.util.map作为数据的返回对象。采用java.util.Map对象,需要额外的一些步骤。下面的代码则说明了iBATIS的select语句返回的java.util.Map对象。Src/ iBATIS.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-////DTD SQL Map 1.0//EN" "-map-2.dtd"> <sqlMap> <select id="salesByListOfMapsSQL" resultClass="java.util.HashMap"> SELECT E.EMPLOYEE_ID "ID", E.FIRST_NAME "FIRST", E.LAST_NAME "LAST", MS.TOTAL_SALES "TOTAL", MS.LATEST_SALE FROM EMPLOYEE E, MONTHLY_SALES MS WHERE E.EMPLOYEE_ID = MS.EMPLOYEE_ID AND MS.MONTH = #value# </select> <resultMap id="searchResultList" class="MonthlySalesBean"> <result property="employeeID" column="ID"/> <result property="first" column="FIRST"/> <result property="last" column="LAST"/> <result property="total" column="TOTAL"/> <result property="latestSale.amount" column="LATEST_SALE"/> </resultMap> <select id="salesByJavaBeansSQL" resultMap="searchResultList"> SELECT E.EMPLOYEE_ID "ID", E.FIRST_NAME "FIRST", E.LAST_NAME "LAST", MS.TOTAL_SALES "TOTAL", MS.LATEST_SALE FROM EMPLOYEE E, MONTHLY_SALES MS WHERE E.EMPLOYEE_ID = MS.EMPLOYEE_ID AND MS.MONTH = #value# </select> </sqlMap>
上面的代码返回的对象即为map对象。请注意,map对象中的Key值直接来自于select语句,因此,像TO_CHAR(MS.TOTAL_SALES)这样的表达式在报表中不提倡使用。因此,比较人性化的为字段命名,是一件很值得的事情。因为map的key值是作为java.lang.Object类型来进行存储的,因此有必要对字段返回类型进行一下整理。
真正的数据填充类应该是ServiceLocatorBean.java类,其代码如下所示:
import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletContext; import orgntext.ApplicationContext; import org.sprntext.support.WebApplicationContextUtils; public class ServiceLocatorBean implements ServiceLocatorIF { private static final long serialVersionUID = -7166271873610635886L; //the Spring application context private ApplicationContext appContext; DAO dao = null; public ServiceLocatorBean() { try { // get the spring context ServletContext context = FacesUtils.getServletContext(); this.appContext = WebApplicationContextUtils.
getRequiredWebApplicationContext(context); // create instance of the business object this.dao = (DAO) this.lookupService("dao"); Connection conn = this.dao.getSqlMapClient().getDataSource().getConnection(); conn.setAutoCommit(false); /* Creating a statement lets us issue commands against the connection. */ Statement s = conn.createStatement(); // just in case old tables from prior run (after first run which // will create the USER1 schema) try { s.execute("drop table employee"); s.execute("drop table monthly_sales"); } catch (Exception ex) { // not to be concerned (at least in this example } /* We create a table, add a few rows, and update one. */ s.execute("create table employee (employee_id int,
first_name varchar(40), last_name varchar(40))"); s.execute("insert into employee values (1,'sterning', 'chen')"); s.execute("insert into employee values (2,'yuxuan', 'Wand')"); s.execute("insert into employee values (3,'Mickey', 'Li')"); s.execute("create table monthly_sales (employee_id int, total_sales numeric(16,
2), latest_sale numeric(8, 2), month int)"); s.execute("insert into monthly_sales values (1, 1600.50, 32.50, 1)"); s.execute("insert into monthly_sales values (2, 1544.20, 12.50, 1)"); s.execute("insert into monthly_sales values (3, 18814.80, 78.65, 1)"); s.execute("insert into monthly_sales values (1, 1450.50, 10.65, 2)"); s.execute("insert into monthly_sales values (2, 2004.25, 52.10, 2)"); s.execute("insert into monthly_sales values (3, 9819.00, 40.65, 2)"); s.close(); mit(); } catch (SQLException sqle) { // just means the tables already exist sqle.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } public DAO getDao() { return this.dao; } public Object lookupService(String serviceBeanName) { return appContext.getBean(serviceBeanName); } }
三、 将iBATIS数据填入JasperReport中
就通常而言,采用Java Bean作为iBATIS的返回对象,相比起java.util.Map对象来说,更加的方便与可行。很多的开发人员采用iBATIS的这种方式来进行数据的映射,同时,此方法还可以无缝的将iBATIS与JapserReport集成起来。
在JasperReport中,提供了一个JRDataSource的实现,从而开发人员可以通过此类来传递iBATIS的list对象给JasperReport模板。而JRBeanCollectionDataSource类使用JavaBean来构造,从而可以通过循环查找collection并获得相应的bean属性。如下的代码示例说明了如何在调用JasperReport引擎时实例化JRBeanCollectionDataSource对象。
import java.io.File; import java.util.HashMap; import java.util.List; import net.sf.jasperreports.engine.JRRuntimeException; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.util.JRLoader; public class SearchBean { private final static String JAVA_BEAN_REPORT = "monthly_sales_java_beans.jasper"; private final static String LIST_OF_MAP_REPORT = "monthly_sales_list_of_maps.jasper"; public String generateFromJavaBeans () { try { ServiceLocatorIF sl = (ServiceLocatorIF) FacesUtils .getManagedBean("serviceLocatorBean"); List list = sl.getDao().getSqlMapClient().queryForList("salesByJavaBeansSQL", month); FacesUtils.setSessionAttribute("JASPER_PRINT", generateReport (list, JAVA_BEAN_REPORT)); viewReport = "true"; } catch (Exception ex) { ex.printStackTrace(); } return null; } public String generateFromListOfMaps () { try { ServiceLocatorIF sl = (ServiceLocatorIF) FacesUtils .getManagedBean("serviceLocatorBean"); List list = sl.getDao().getSqlMapClient().queryForList("salesByListOfMapsSQL", month); FacesUtils.setSessionAttribute("JASPER_PRINT", generateReport (list, LIST_OF_MAP_REPORT)); viewReport = "true"; } catch (Exception ex) { ex.printStackTrace(); } return null; } private JasperPrint generateReport (List dataList, String reportName) { JasperPrint jasperPrint = null; try { String localPath = FacesUtils.getServletContext().getRealPath("/"); File reportFile = new File(localPath + "WEB-INF" + File.separator + reportName); if (!reportFile.exists()) throw new JRRuntimeException(".jasper file not found. The report design must be compiled first."); JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath()); if (reportName.equals(JAVA_BEAN_REPORT)) { jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new JRBeanCollectionDataSource (dataList)); } else { jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new CustomJRDS (dataList)); } } catch (Exception ex) { ex.printStackTrace(); } return jasperPrint; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public String getViewReport() { return viewReport; } public void setViewReport(String viewReport) { this.viewReport = viewReport; } private String month = null; private String viewReport = null; }
在上面的代码中,定义的参数map,是在运行时传递相关的参数值给JasperReport。例如,可以在报表模板中定义一个名为REPORT_TITLE的参数,然后在运行时传递这一参数的值给它,传递的方式一般是健/值对的形式。例如Key=REPORT_TITLE,Value=Sale Report。当然,参数是传递给fillReport方法。然后,JasperReport会加载已经编译好的Jasper模板文件(.jasper)。最后调用静态的fillReport方法。
定制报表格式有二种方式,一种就是写jrxml文件,其实就是xml文件,只不过是后缀名不一样罢了。另一种方式更直接,就是生成一个JasperDesign类的实例,在japsperDesign中自己定义模板。jrxml文件也是通过一个JRXmlLoad加载过来,转成JasperDesign类的实例。也就是说写jrxml文件还需要进行解析,加载。现实中我们使用的报表一般格式比较固定,因而可以通过先使用iReport工具生成模板,再加载解析的方式。这种方式简单,而且可见性强。
iReport做为一个优秀的报表设计器,有着功能非常强大的特性。作为开源的Java程序,不但有适合于Windows安装的应用程序,同时,还提供完全开放的源代码,可供参考及原理分析。在本文中,主要通过图形界面中的模板设计,以及与数据库的连接等一系列的操作,来介绍如何定制一定要求的报表模板。
通过iReport可初见化的图形界面,可以设计出各种各样的简单或复杂的报表。通过iReport的这种可视化界面设计,可以为JasperReport提供优秀的报表模板,而无须去理解或是掌握那些复杂的XML语法。如此则可以Web报表开发节省大量的开发时间。
在进行iReport模板设计之前,需要编写JavaBean类:MonthlySalesBean.java,代码如下:
import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class MonthlySalesBean { private int employeeID;; private String last = null; private String first = null; private BigDecimal total = null; private List sales = null; private LatestSale latestSale = null; public static List createBeanCollection () { List list = new ArrayList (); MonthlySalesBean msb = new MonthlySalesBean (); msb.setEmployeeID(1); msb.setFirst("John"); msb.setLast("Doe"); msb.setTotal(new BigDecimal ("1600.50")); LatestSale ls = new LatestSale (); ls.setAmount(new BigDecimal ("32.21")); msb.setLatestSale(ls); list.add(msb); return list; } public int getEmployeeID() { return employeeID; } public void setEmployeeID(int employeeID) { this.employeeID = employeeID; } public String getFirst() { return first; } public void setFirst(String first) { this.first = first; } public String getLast() { return last; } public void setLast(String last) { this.last = last; } public BigDecimal getTotal() { return total; } public void setTotal(BigDecimal total) { this.total = total; } public List getSales() { return sales; } public void setSales(List sales) { this.sales = sales; } public LatestSale getLatestSale() { return latestSale; } public void setLatestSale(LatestSale latestSale) { this.latestSale = latestSale; } }
将上面的类打成一个jar包,并置于classpath目录下,则iReport可以进行访问。使用JavaBean做为数据源,为了在设计报表时能够看到数据,在程序中要为iReport提供一个静态方法,该方法返回上面定义JavaBean的一个结果集,这个静态方法可能在程序运行中并不是必须的,但是在iReport中它确实必须的,换句话说,这个静态方法是专门为iReport量身定做的,为了iReport在设计报表时能够调用这个静态方法返回相应的JavaBean结果集,以便设计的报表放在Java项目中之前就能像使用SQL数据库数据源一样可以浏览。在iReport中先进行数据源的连接配置,此处采用是JavaBeans set data source连接方式:
图2.在iReport进行数据源的连接
二、 处理iBati返回数据
如果iBATIS没有采用JavaBean作为返回对象,则可以采用java.util.map作为数据的返回对象。采用java.util.Map对象,需要额外的一些步骤。下面的代码则说明了iBATIS的select语句返回的java.util.Map对象。Src/ iBATIS.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-////DTD SQL Map 1.0//EN" "-map-2.dtd"> <sqlMap> <select id="salesByListOfMapsSQL" resultClass="java.util.HashMap"> SELECT E.EMPLOYEE_ID "ID", E.FIRST_NAME "FIRST", E.LAST_NAME "LAST", MS.TOTAL_SALES "TOTAL", MS.LATEST_SALE FROM EMPLOYEE E, MONTHLY_SALES MS WHERE E.EMPLOYEE_ID = MS.EMPLOYEE_ID AND MS.MONTH = #value# </select> <resultMap id="searchResultList" class="MonthlySalesBean"> <result property="employeeID" column="ID"/> <result property="first" column="FIRST"/> <result property="last" column="LAST"/> <result property="total" column="TOTAL"/> <result property="latestSale.amount" column="LATEST_SALE"/> </resultMap> <select id="salesByJavaBeansSQL" resultMap="searchResultList"> SELECT E.EMPLOYEE_ID "ID", E.FIRST_NAME "FIRST", E.LAST_NAME "LAST", MS.TOTAL_SALES "TOTAL", MS.LATEST_SALE FROM EMPLOYEE E, MONTHLY_SALES MS WHERE E.EMPLOYEE_ID = MS.EMPLOYEE_ID AND MS.MONTH = #value# </select> </sqlMap>
上面的代码返回的对象即为map对象。请注意,map对象中的Key值直接来自于select语句,因此,像TO_CHAR(MS.TOTAL_SALES)这样的表达式在报表中不提倡使用。因此,比较人性化的为字段命名,是一件很值得的事情。因为map的key值是作为java.lang.Object类型来进行存储的,因此有必要对字段返回类型进行一下整理。
真正的数据填充类应该是ServiceLocatorBean.java类,其代码如下所示:
import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletContext; import orgntext.ApplicationContext; import org.sprntext.support.WebApplicationContextUtils; public class ServiceLocatorBean implements ServiceLocatorIF { private static final long serialVersionUID = -7166271873610635886L; //the Spring application context private ApplicationContext appContext; DAO dao = null; public ServiceLocatorBean() { try { // get the spring context ServletContext context = FacesUtils.getServletContext(); this.appContext = WebApplicationContextUtils.
getRequiredWebApplicationContext(context); // create instance of the business object this.dao = (DAO) this.lookupService("dao"); Connection conn = this.dao.getSqlMapClient().getDataSource().getConnection(); conn.setAutoCommit(false); /* Creating a statement lets us issue commands against the connection. */ Statement s = conn.createStatement(); // just in case old tables from prior run (after first run which // will create the USER1 schema) try { s.execute("drop table employee"); s.execute("drop table monthly_sales"); } catch (Exception ex) { // not to be concerned (at least in this example } /* We create a table, add a few rows, and update one. */ s.execute("create table employee (employee_id int,
first_name varchar(40), last_name varchar(40))"); s.execute("insert into employee values (1,'sterning', 'chen')"); s.execute("insert into employee values (2,'yuxuan', 'Wand')"); s.execute("insert into employee values (3,'Mickey', 'Li')"); s.execute("create table monthly_sales (employee_id int, total_sales numeric(16,
2), latest_sale numeric(8, 2), month int)"); s.execute("insert into monthly_sales values (1, 1600.50, 32.50, 1)"); s.execute("insert into monthly_sales values (2, 1544.20, 12.50, 1)"); s.execute("insert into monthly_sales values (3, 18814.80, 78.65, 1)"); s.execute("insert into monthly_sales values (1, 1450.50, 10.65, 2)"); s.execute("insert into monthly_sales values (2, 2004.25, 52.10, 2)"); s.execute("insert into monthly_sales values (3, 9819.00, 40.65, 2)"); s.close(); mit(); } catch (SQLException sqle) { // just means the tables already exist sqle.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } public DAO getDao() { return this.dao; } public Object lookupService(String serviceBeanName) { return appContext.getBean(serviceBeanName); } }
三、 将iBATIS数据填入JasperReport中
就通常而言,采用Java Bean作为iBATIS的返回对象,相比起java.util.Map对象来说,更加的方便与可行。很多的开发人员采用iBATIS的这种方式来进行数据的映射,同时,此方法还可以无缝的将iBATIS与JapserReport集成起来。
在JasperReport中,提供了一个JRDataSource的实现,从而开发人员可以通过此类来传递iBATIS的list对象给JasperReport模板。而JRBeanCollectionDataSource类使用JavaBean来构造,从而可以通过循环查找collection并获得相应的bean属性。如下的代码示例说明了如何在调用JasperReport引擎时实例化JRBeanCollectionDataSource对象。
import java.io.File; import java.util.HashMap; import java.util.List; import net.sf.jasperreports.engine.JRRuntimeException; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.util.JRLoader; public class SearchBean { private final static String JAVA_BEAN_REPORT = "monthly_sales_java_beans.jasper"; private final static String LIST_OF_MAP_REPORT = "monthly_sales_list_of_maps.jasper"; public String generateFromJavaBeans () { try { ServiceLocatorIF sl = (ServiceLocatorIF) FacesUtils .getManagedBean("serviceLocatorBean"); List list = sl.getDao().getSqlMapClient().queryForList("salesByJavaBeansSQL", month); FacesUtils.setSessionAttribute("JASPER_PRINT", generateReport (list, JAVA_BEAN_REPORT)); viewReport = "true"; } catch (Exception ex) { ex.printStackTrace(); } return null; } public String generateFromListOfMaps () { try { ServiceLocatorIF sl = (ServiceLocatorIF) FacesUtils .getManagedBean("serviceLocatorBean"); List list = sl.getDao().getSqlMapClient().queryForList("salesByListOfMapsSQL", month); FacesUtils.setSessionAttribute("JASPER_PRINT", generateReport (list, LIST_OF_MAP_REPORT)); viewReport = "true"; } catch (Exception ex) { ex.printStackTrace(); } return null; } private JasperPrint generateReport (List dataList, String reportName) { JasperPrint jasperPrint = null; try { String localPath = FacesUtils.getServletContext().getRealPath("/"); File reportFile = new File(localPath + "WEB-INF" + File.separator + reportName); if (!reportFile.exists()) throw new JRRuntimeException(".jasper file not found. The report design must be compiled first."); JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath()); if (reportName.equals(JAVA_BEAN_REPORT)) { jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new JRBeanCollectionDataSource (dataList)); } else { jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new CustomJRDS (dataList)); } } catch (Exception ex) { ex.printStackTrace(); } return jasperPrint; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public String getViewReport() { return viewReport; } public void setViewReport(String viewReport) { this.viewReport = viewReport; } private String month = null; private String viewReport = null; }
在上面的代码中,定义的参数map,是在运行时传递相关的参数值给JasperReport。例如,可以在报表模板中定义一个名为REPORT_TITLE的参数,然后在运行时传递这一参数的值给它,传递的方式一般是健/值对的形式。例如Key=REPORT_TITLE,Value=Sale Report。当然,参数是传递给fillReport方法。然后,JasperReport会加载已经编译好的Jasper模板文件(.jasper)。最后调用静态的fillReport方法。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询