JasperReport学习笔记5-其它数据生成动态的报表(WEB)

1.空数据(Empty Datasources)
就是说JRXML文件里面的内容都是静态的,直接使用
JREmptyDataSource就行了

Java代码 收藏代码
  1. JasperRunManager.runReportToPdfStream(reportStream,

  2. servletOutputStream, new HashMap(), new JREmptyDataSource());



2.Map数组(Map Datasources)
有的时候数据不一定要从数据库里面来,还有可能从Map数组里面来
成生Map[]的方法
Java代码 收藏代码
  1. private Map[] initializeMapArray()

  2. {

  3. //你可以把数组里面的每个map看成一个对象,就相于数据库里面的每个字段

  4. HashMap[] reportRows = new HashMap[4];

  5. HashMap row1Map = new HashMap();

  6. HashMap row2Map = new HashMap();

  7. HashMap row3Map = new HashMap();

  8. HashMap row4Map = new HashMap();

  9. row1Map.put("tail_num", "N263Y");

  10. row1Map.put("aircraft_serial", "T-11");

  11. row1Map.put("aircraft_model", "39 ROSCOE TRNR RACER");

  12. row1Map.put("engine_model", "R1830 SERIES");

  13. row2Map.put("tail_num", "N4087X");

  14. row2Map.put("aircraft_serial", "BA100-163");

  15. row2Map.put("aircraft_model", "BRADLEY AEROBAT");

  16. row2Map.put("engine_model", "R2800 SERIES");

  17. row3Map.put("tail_num", "N43JE");

  18. row3Map.put("aircraft_serial", "HAYABUSA 1");

  19. row3Map.put("aircraft_model", "NAKAJIMA KI-43 IIIA");

  20. row3Map.put("engine_model", "R1830 SERIES");

  21. row4Map.put("tail_num", "N912S");

  22. row4Map.put("aircraft_serial", "9973CC");

  23. row4Map.put("aircraft_model", "PA18-150");

  24. row4Map.put("engine_model", "R-1820 SER");

  25. reportRows[0] = row1Map;

  26. reportRows[1] = row2Map;

  27. reportRows[2] = row3Map;

  28. reportRows[3] = row4Map;

  29. return reportRows;

  30. }



生成JRMapArrayDataSource对象
Java代码 收藏代码
  1. private JRDataSource createReportDataSource()

  2. {

  3. JRMapArrayDataSource dataSource;

  4. Map[] reportRows = initializeMapArray();

  5. dataSource = new JRMapArrayDataSource(reportRows);

  6. return dataSource;

  7. }



Servlet里面
Java代码 收藏代码
  1. JRDataSource dataSource = createReportDataSource();

  2. JasperRunManager.runReportToPdfStream(reportStream,

  3. servletOutputStream, new HashMap(), dataSource);



3.Map集合
Java代码 收藏代码
  1. private JRDataSource createReportDataSource()

  2. {

  3. JRMapCollectionDataSource dataSource;

  4. Collection reportRows = initializeMapCollection();

  5. dataSource = new JRMapCollectionDataSource(reportRows);

  6. return dataSource;

  7. }

  8. private Collection initializeMapCollection()

  9. {

  10. ArrayList reportRows = new ArrayList();

  11. HashMap row1Map = new HashMap();

  12. HashMap row2Map = new HashMap();

  13. HashMap row3Map = new HashMap();

  14. HashMap row4Map = new HashMap();

  15. row1Map.put("tail_num", "N263Y");

  16. row1Map.put("aircraft_serial", "T-11");

  17. row1Map.put("aircraft_model", "39 ROSCOE TRNR RACER");

  18. row1Map.put("engine_model", "R1830 SERIES");

  19. row2Map.put("tail_num", "N4087X");

  20. row2Map.put("aircraft_serial", "BA100-163");

  21. row2Map.put("aircraft_model", "BRADLEY AEROBAT");

  22. row2Map.put("engine_model", "R2800 SERIES");

  23. row3Map.put("tail_num", "N43JE");

  24. row3Map.put("aircraft_serial", "HAYABUSA 1");

  25. row3Map.put("aircraft_model", "NAKAJIMA KI-43 IIIA");

  26. row3Map.put("engine_model", "R1830 SERIES");

  27. row4Map.put("tail_num", "N912S");

  28. row4Map.put("aircraft_serial", "9973CC");

  29. row4Map.put("aircraft_model", "PA18-150");

  30. row4Map.put("engine_model", "R-1820 SER");

  31. reportRows.add(row1Map);

  32. reportRows.add(row2Map);

  33. reportRows.add(row3Map);

  34. reportRows.add(row4Map);

  35. return reportRows;

  36. }



使用
Java代码 收藏代码
  1. JRDataSource dataSource = createReportDataSource();

  2. JasperRunManager.runReportToPdfStream(reportStream,

  3. servletOutputStream, new HashMap(), dataSource);



4.对象数组(Java Objects as Datasources) 对象JAVABEAN
Java代码 收藏代码
  1. publicclass AircraftData

  2. {

  3. public AircraftData(String tail, String serial, String model,

  4. String engine)

  5. {

  6. setTailNum(tail);

  7. setAircraftSerial(serial);

  8. setAircraftModel(model);

  9. setEngineModel(engine);

  10. }

  11. public AircraftData()

  12. {

  13. }

  14. private String tailNum;

  15. private String aircraftSerial;

  16. private String aircraftModel;

  17. private String engineModel;

  18. //set get method.....

  19. }



JRXML文件
Java代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"

  3. "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

  4. <jasperReport name="AircraftReport">

  5. <field name="tailNum"class="java.lang.String"/>

  6. <field name="aircraftSerial"class="java.lang.String"/>

  7. <field name="aircraftModel"class="java.lang.String"/>

  8. <field name="engineModel"class="java.lang.String"/>

  9. <pageHeader>

  10. <band height="30">

  11. <staticText>

  12. <reportElement x="0" y="0" width="69" height="24"/>

  13. <textElement verticalAlignment="Bottom"/>

  14. <text>

  15. <![CDATA[Tail Number: ]]>

  16. </text>

  17. </staticText>

  18. <staticText>

  19. <reportElement x="140" y="0" width="69" height="24"/>

  20. <text>

  21. <![CDATA[Serial Number: ]]>

  22. </text>

  23. </staticText>

  24. <staticText>

  25. <reportElement x="280" y="0" width="69" height="24"/>

  26. <text>

  27. <![CDATA[Model: ]]>

  28. </text>

  29. </staticText>

  30. <staticText>

  31. <reportElement x="420" y="0" width="69" height="24"/>

  32. <text>

  33. <![CDATA[Engine: ]]>

  34. </text>

  35. </staticText>

  36. </band>

  37. </pageHeader>

  38. <detail>

  39. <band height="30">

  40. <textField>

  41. <reportElement x="0" y="0" width="69" height="24"/>

  42. <textFieldExpression class="java.lang.String">

  43. <![CDATA[$F{tailNum}]]>

  44. </textFieldExpression>

  45. </textField>

  46. <textField>

  47. <reportElement x="140" y="0" width="69" height="24"/>

  48. <textFieldExpression class="java.lang.String">

  49. <![CDATA[$F{aircraftSerial}]]>

  50. </textFieldExpression>

  51. </textField>

  52. <textField>

  53. <reportElement x="280" y="0" width="69" height="24"/>

  54. <textFieldExpression class="java.lang.String">

  55. <![CDATA[$F{aircraftModel}]]>

  56. </textFieldExpression>

  57. </textField>

  58. <textField>

  59. <reportElement x="420" y="0" width="69" height="24"/>

  60. <textFieldExpression class="java.lang.String">

  61. <![CDATA[$F{engineModel}]]>

  62. </textFieldExpression>

  63. </textField>

  64. </band>

  65. </detail>

  66. </jasperReport>



调用
Java代码 收藏代码
  1. private JRDataSource createReportDataSource()

  2. {

  3. JRBeanArrayDataSource dataSource;

  4. AircraftData[] reportRows = initializeBeanArray();

  5. dataSource = new JRBeanArrayDataSource(reportRows);

  6. return dataSource;

  7. }

  8. private AircraftData[] initializeBeanArray()

  9. {

  10. AircraftData[] reportRows = new AircraftData[4];

  11. reportRows[0] = new AircraftData("N263Y", "T-11", "39 ROSCOE TRNR

  12. RACER", "R1830 SERIES");

  13. reportRows[1] = new AircraftData("N4087X", "BA100-163", "BRADLEY

  14. AEROBAT", "R2800 SERIES");

  15. reportRows[2] = new AircraftData("N43JE", "HAYABUSA 1", "NAKAJIMA

  16. KI-43 IIIA", "R1830 SERIES");

  17. reportRows[3] = new AircraftData("N912S", "9973CC", "PA18-150",

  18. "R-1820 SER");

  19. return reportRows;

  20. }

  21. //最后servlet里面调用

  22. JRDataSource dataSource = createReportDataSource();

  23. JasperRunManager.runReportToPdfStream(reportStream,

  24. servletOutputStream, new HashMap(), dataSource);



5.对象集合
和对象数组基本上一样,就是最后生成的是JRBeanCollectionDataSource
Java代码 收藏代码
  1. private JRDataSource createReportDataSource()

  2. {

  3. JRBeanCollectionDataSource dataSource;

  4. Collection reportRows = initializeBeanCollection();

  5. dataSource = new JRBeanCollectionDataSource(reportRows);

  6. return dataSource;

  7. }

  8. JRDataSource dataSource = createReportDataSource();

  9. JasperRunManager.runReportToPdfStream(reportStream,

  10. servletOutputStream, new HashMap(), dataSource);



6.XML数据成生报表(XML as Datasource)
Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <AircraftData>

  3. <aircraft>

  4. <tail_num>N263Y</tail_num>

  5. <aircraft_serial>T-11</aircraft_serial>

  6. <aircraft_model>39 ROSCOE TRNR RACER</aircraft_model>

  7. <engine_model>R1830 SERIES</engine_model>

  8. </aircraft>

  9. <aircraft>

  10. <tail_num>N4087X</tail_num>

  11. <aircraft_serial>BA100-163</aircraft_serial>

  12. <aircraft_model>BRADLEY AEROBAT</aircraft_model>

  13. <engine_model>R2800 SERIES</engine_model>

  14. </aircraft>

  15. <aircraft>

  16. <tail_num>N43JE</tail_num>

  17. <aircraft_serial>HAYABUSA 1</aircraft_serial>

  18. <aircraft_model>NAKAJIMA KI-43 IIIA</aircraft_model>

  19. <engine_model>R1830 SERIES</engine_model>

  20. </aircraft>

  21. <aircraft>

  22. <tail_num>N912S</tail_num>

  23. <aircraft_serial>9973CC</aircraft_serial>

  24. <aircraft_model>PA18-150</aircraft_model>

  25. <engine_model>R-1820 SER</engine_model>

  26. </aircraft>

  27. </AircraftData>


这样获得
Java代码 收藏代码
  1. JRXmlDataSource xmlDataSource = new JRXmlDataSource(

  2. new BufferedInputStream(getServletConfig().getServletContext()

  3. .getResourceAsStream("/reports/AircraftData.xml")),

  4. "/AircraftData/aircraft");



JREmptyDataSource,JRMapArrayDataSource,JRMapCollectionDataSource,JRBeanArrayDataSource,JRBeanCollectionDataSource,JRXmlDataSource
他们都implements JRDataSource

主要目地就是不从数据库里面取数据,这个方面和Hibernate结合


你可能感兴趣的:(jasperReport,空数据,Map数组,动态的报表)