使用Javabean作为数据源的JasperReport报表 http://sarin.iteye.com/blog/693622
iReport+JasperReport实现数据库记录导出到html、excel、pdf文档 http://my.oschina.net/jiangli0502/blog/122885
配置iReport参考原文:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.pandy.evisa; import java.io.Serializable; // com.pandy.evisa.CustomDataVO public class CustomDataVO implements Serializable { private int id;// 主键 private String name; // 其余的属性省略了 // 默认的构造方法 public CustomDataVO() { super(); } // 带参数的构造方法,省略其余参数 public CustomDataVO(int id) { super(); this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.pandy.evisa; import java.util.List; import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRField; public class CustomDataSource implements JRDataSource { // 数据集合,使用到了一个VO值对象 private List<CustomDataVO> list; private int i = -1; // 构造方法,初始化list public CustomDataSource(List<CustomDataVO> list) { super(); this.list = list; } // 覆盖接口中的方法,给List添加参数 public Object getFieldValue(JRField jrField) throws JRException { Object value = null; String fieldName = jrField.getName(); if ("id".equals(fieldName)) { value = list.get(i).getId(); } else if ("name".equals(fieldName)) { value = list.get(i).getName(); } return value; } // 覆盖接口中的next()方法 public boolean next() throws JRException { i++; if (list == null) { return false; } return (i < list.size()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package com.pandy.evisa; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.JasperRunManager; import net.sf.jasperreports.engine.util.JRLoader; public class JasperGenerater { private void exportPdf(String template, String outputfile, Map<String, Object> param, List<CustomDataVO> dataList) { try { OutputStream out = new FileOutputStream(outputfile); File jasperFile=new File(template); JasperReport jasperReport= (JasperReport)JRLoader.loadObject(jasperFile); //JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport,param); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport ,new HashMap()); /*JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out); exporter.exportReport(); */ //JasperExportManager.exportReportToPdfFile(jasperPrint, outputfile); byte[] bytes = JasperRunManager.runReportToPdf(template, param, new CustomDataSource(dataList)); out.write(bytes,0,bytes.length); out.flush(); System.out.println(bytes.length); out.close(); } catch (Exception e) { e.printStackTrace(); } } // 测试Main public static void main(String[] args) { JasperGenerater jasperGenerater = new JasperGenerater(); //String template = "D://编程工具//ireport//user.jasper"; // jasper模版 String template = "/home/pandy/workspace/EVisa/resources/report1.jrxml"; // jasper模版 String jasper="/home/pandy/workspace/EVisa/resources/report1.jasper"; /*try { JasperCompileManager.compileReportToFile(template,jasper); } catch (JRException e) { e.printStackTrace(); }*/ //String outputfile = "/home/pandy/workspace/EVisa/resources/report1.pdf"; // 输出路径 String outputfile = "/mnt/D//测试报表.pdf"; // 输出路径 List<CustomDataVO> dataList = new ArrayList<CustomDataVO>(); for (int i = 0; i < 10; i++) { CustomDataVO cur = new CustomDataVO(); cur.setId(i+1); cur.setName("name"+i); dataList.add(cur); } Map param = new HashMap(); // 参数 param.put("title", "用户登陆统计"); param.put("date", "2009.01.01——2009.08.08"); jasperGenerater.exportPdf(jasper, outputfile, param, dataList); // 生成PDF文件 } } |