日期:2014-05-16 浏览次数:20899 次
public class EntityDto { private String name; private String pwd; public EntityDto(){ } public EntityDto(String name, String pwd){ this.name = name; this.pwd = pwd; } //setter和getter方法,一定要有 }
public class EntityDtoFactory { //方法参数可以自行设定 public static Collection<EntityDto> getBeanCollection() throws SSException { List<EntityDto> entityDtos = new ArrayList<EntityDto>(); //自行定义获取结果方法,这里仅仅是一个示例 entityDtos.add(new EntityDto("notesth","name for short")); entityDtos.add(new EntityDto("notesomething","full name")); return entityDtos; } }
<!-- 为了解决此包冲突,提前引入 --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> <!-- 解决打印时条形码异常类找不到 --> <dependency> <groupId>net.sourceforge.barbecue</groupId> <artifactId>barbecue</artifactId> <version>1.5-beta1</version> </dependency> <!-- 关于打印部分 --> <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>4.5.1</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency>
public class PrintService(){ //需要参数自行设定 public JasperPrint print() { //把制作好的模版文件放在工程的Class文件夹的/iReportTemplate中,比如名字叫report.jrxml,这个文件会在之后制作 InputStream in = readFile("/iReportTemplate/report.jrxml"); Map<String, Object> parameters = new HashMap<String, Object>(); try{ // 设定数据源 JRDataSource dataSource = new JRBeanCollectionDataSource( EntityDtoFactory.getBeanCollection()); // 编译jrxml文件 JasperReport jasperReport = JasperCompileManager .compileReport(in); // 填充数据 JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, parameters, dataSource); }catch (JRException e){ e.printStackTrace(); } catch (Exception