JasperReport 简单使用(数据库源为JavaBean) 博客分类: 工具 templateireportjasperreportjavabean
程序员文章站
2024-03-24 19:50:28
...
一 创建JavaBean数据源
二 创建Report模板
三 生成报表
jasperreport.javabean.User.java
package jasperreport.javabean; import java.util.Date; public class User { private String username; private Date birthday; public User(String username, Date birthday) { this.username = username; this.birthday = birthday; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
jasperreport.javabean.JasperReportWithJavaBean.java
package jasperreport.javabean; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.export.JRTextExporter; import net.sf.jasperreports.engine.export.JRTextExporterParameter; public class JasperReportWithJavaBean { public static void export() throws Exception{ InputStream inputStream = JasperReportWithJavaBean.class.getResourceAsStream("JavaBeanReport.jasper"); Map<Object,Object> parameters = new HashMap<Object,Object>(); List<User> users = new ArrayList<User>(); users.add(new User("user_01",new Date())); users.add(new User("user_02",new Date())); users.add(new User("user_03",new Date())); JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(users); JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, dataSource); JRTextExporter exporter = new JRTextExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "javabean.txt"); exporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, 200); exporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, 100); exporter.exportReport(); } public static void main(String[] args) throws Exception{ export(); } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>JasperReport</groupId> <artifactId>JasperReport</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>3.7.2</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.13</version> </dependency> </dependencies> </project>