DynamicReport好不好用我不知道,但是它是開源的,文檔還算全?用戶的需求是想自己設計,但是ireport是給開發級別的使用的,據說用戶並不想學,他們只想拖拖拽拽就出來一個報表。
功能需求總結:
1、用戶自主設計報表樣式
2、數據動態加載
解決方案:
將用戶拖拽的結果,參照jxml的格式生成jxml文件,作為模板加載到DynamicReport里面;
數據源使用xml格式數據,可以實現動態
本文版翻譯權歸__饞貓和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,否則保留追究法律責任的權利。
DynamicReport官方文檔上面,這兩個例子都有很詳細的教程,鏈接如下:
使用模板:http://www.dynamicreports.org/examples/examples-overview#templatedesign
使用xml數據源:http://www.dynamicreports.org/examples/examples-overview#datasource
這里主要給出二者相結合使用的例子,小白級別步驟入下:
1、確定工程里面導入了DynamicReport所需要的各種jar包;
2、將 模板:testTable_subreport1.jxml和 數據源:NameList.xml添加到TestXML.Java同一包下面;
3、run即可;

package a.test.xmlfile; <?xml version="1.0" encoding="UTF-8"?> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="testTable_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="8b0d8ddc-641f-46ec-a711-a5212faff8c4"> <property name="ireport.zoom" value="1.0"/> <property name="ireport.x" value="0"/> <property name="ireport.y" value="0"/> <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false"> <defaultValueExpression><![CDATA["F:\\Users\\Desktop\\"]]></defaultValueExpression> </parameter> <queryString language="xPath"> <![CDATA[/NameList/Person]]> </queryString> <field name="Name" class="java.lang.String"> <fieldDescription><![CDATA[Name]]></fieldDescription> </field> <field name="Gender" class="java.lang.String"> <fieldDescription><![CDATA[Gender]]></fieldDescription> </field> <field name="Age" class="java.lang.String"> <fieldDescription><![CDATA[Age]]></fieldDescription> </field> <group name="GroupName" footerPosition="ForceAtBottom" keepTogether="true"> <groupExpression><![CDATA[$F{Gender}]]></groupExpression> <groupHeader> <band splitType="Immediate"/> </groupHeader> </group> <title> <band height="79" splitType="Stretch"/> </title> <pageHeader> <band height="35" splitType="Stretch"/> </pageHeader> <columnHeader> <band height="61" splitType="Stretch"> <staticText> <reportElement mode="Transparent" x="141" y="41" width="100" height="20" uuid="506dc39b-345d-40cf-af57-e6d2264b4324"/> <text><![CDATA[Name]]></text> </staticText> <staticText> <reportElement x="41" y="41" width="100" height="20" uuid="eb47f825-0875-488a-be9f-d047df0cc967"/> <text><![CDATA[Gender]]></text> </staticText> <staticText> <reportElement x="241" y="41" width="100" height="20" uuid="06a53662-6d2a-4f55-b7ca-88880fbf55a4"/> <text><![CDATA[Age]]></text> </staticText> </band> </columnHeader> <detail> <band height="21" splitType="Stretch"> <rectangle> <reportElement x="41" y="1" width="100" height="20" uuid="8016a9b4-352b-4a7b-a4db-281be1b1acff"/> </rectangle> <rectangle> <reportElement x="141" y="1" width="100" height="20" uuid="9fcc48a6-004b-4298-a1a0-8096d1f98a5d"/> </rectangle> <rectangle> <reportElement x="241" y="1" width="100" height="20" uuid="155963b8-fb26-4e85-8d94-49baae947444"/> </rectangle> <textField> <reportElement x="141" y="1" width="100" height="20" uuid="2f3e4e70-3bd1-41a1-b815-de41a96fd504"/> <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression> </textField> <textField> <reportElement x="41" y="1" width="100" height="20" uuid="51f31db4-daf2-4521-bf60-988384f93caa"/> <textFieldExpression><![CDATA[$F{Gender}]]></textFieldExpression> </textField> <textField> <reportElement x="241" y="0" width="100" height="20" uuid="80c2563c-ae7c-41ed-adcd-6ba8e09bb53d"/> <textFieldExpression><![CDATA[$F{Age}]]></textFieldExpression> </textField> </band> </detail> </jasperReport> testTable_subreport1

1 package a.test.xmlfile; 2 3 import java.io.InputStream; 4 5 import net.sf.dynamicreports.examples.Templates; 6 import net.sf.dynamicreports.jasper.builder.JasperReportBuilder; 7 import net.sf.dynamicreports.report.builder.DynamicReports; 8 import net.sf.dynamicreports.report.builder.style.StyleBuilder; 9 import net.sf.jasperreports.engine.JRException; 10 import net.sf.jasperreports.engine.data.JRXmlDataSource; 11 12 public class TestXML { 13 14 public TestXML() { 15 build(); 16 } 17 18 private void build() { 19 JRXmlDataSource dataSource = null; 20 try { 21 dataSource = new JRXmlDataSource(TestXML.class.getResourceAsStream("NameList.xml"), "/NameList/Person"); 22 } catch (JRException e1) { 23 // TODO Auto-generated catch block 24 e1.printStackTrace(); 25 } 26 27 InputStream is = TestXML.class.getResourceAsStream("testTable_subreport1.jrxml"); 28 JasperReportBuilder report = DynamicReports.report();// 創建空報表 29 StyleBuilder chineseStl = DynamicReports.stl.style().setFontName("FreeUniversalMSY").setFontSize(12); 30 try { 31 // report; 32 33 report.setTemplate(Templates.reportTemplate).setTitleStyle(chineseStl).setTemplateDesign(is) 34 .setDataSource(dataSource).show(); 35 } catch (Exception e) { 36 e.printStackTrace(); 37 } 38 } 39 40 public static void main(String[] args) { 41 new TestXML(); 42 } 43 44 }

1 <?xml version="1.0" encoding="utf-8" ?> 2 <NameList> 3 <Person> 4 <Name>阿美</Name> 5 <Gender>女</Gender> 6 <Age>23</Age> 7 </Person> 8 <Person> 9 <Name>李麗</Name> 10 <Gender>女</Gender> 11 <Age>58</Age> 12 </Person> 13 <Person> 14 <Name>強尼</Name> 15 <Gender>男</Gender> 16 <Age>56</Age> 17 </Person> 18 <Person> 19 <Name>傑森</Name> 20 <Gender>男</Gender> 21 <Age>21</Age> 22 </Person> 23 <Person> 24 <Name>劉三</Name> 25 <Gender>男</Gender> 26 <Age>21</Age> 27 </Person> 28 </NameList>
本文版翻譯權歸__饞貓和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,否則保留追究法律責任的權利。
這里需要注意的是,如果你想自己寫jxml文件,一定要注意順序,有些標簽出現順序有排位,如果錯位的話,DynamicReport里面會報錯。還有好多坑,,,下次貼代碼。