JFreeReport是一个非常不错的开源的东东,更多详细的关于此东东的基础性和相关介绍请参考其官方网站(http://www.jfree.org/jfreereport/)。
我去年在开发一个SWING的应用系统的时候就是使用JFreeReport来处理的报表问题。它同时还支持将报表输出为PDF,XSL等格式,功能之强大让我觉得值得去研究和关注她。
JFreeReport主要有两种生成报表的形式,一种是在程序中直接生成,当然这种方式肯定不利于维护和修改,同时在程序中生成报表也比较麻烦,网上有相关的文章介绍,大家可以去百度或是GOOGLE一下,在此就不再冗述了。
第二种方法就是通过程序+XML配置文件的方法来控制报表的格式。主要分为如下几个步骤来实现:
1、导入相关的包和类
1 import org.jfree.report.JFreeReport;
2 import org.jfree.report.ReportProcessingException;
3 import org.jfree.report.modules.gui.base.PreviewFrame;
4 import org.jfree.report.modules.gui.base.components.ActionButton;
5 import org.jfree.report.modules.gui.base.components.ActionMenuItem;
6 import org.jfree.report.modules.parser.base.ReportGenerator;
7 import org.jfree.report.util.Log;
8 import org.jfree.report.util.WaitingImageObserver;
9 import org.jfree.report.modules.gui.base.PreviewDialog;
2、将数据放入一个TableModel中
1 JTable jt=new JTable(aaa,v);
2 // 获得创建报表需要用到的数据
3 sdata=jt.getModel();
3、读入报表格式控制文件(即相应的XML文件)
1 try {
2 if (report == null) {
3 URL in = getClass().getResource("/zmx/chosun/ScorePrintNew.xml");
4 this.report = parseReport(in);
5 }
6 if (report == null) {
7 JOptionPane.showMessageDialog(new JFrame(), "The report definition is null");
8 return;
9 }
10 }
11 catch (Exception e) {
12 e.printStackTrace();
13 }
4、将报表和数据结合起来
1 this.report.setData(sdata);
5、调用并打开预览窗口
1 try {
2 //将生成的报表放到预览窗口中
3 final PreviewDialog preview = new PreviewDialog(report);
4 preview.addWindowListener(new WindowAdapter() {
5 public void windowClosing(final WindowEvent event) {
6 preview.dispose();
7 }
8
9 });
10 preview.pack();
11 //显示报表预览窗口
12 preview.setVisible(true);
13 }
14 catch (ReportProcessingException e) {
15 System.out.println(e);
16 }
6、OK,大功告成。
关于XML的报表格式控制文件的编写我将会在后续的文章中详细讲解。
欢迎大家批评指正!
本程序源代码如下:
1 import javax.swing.table.TableModel;
2 import com.borland.dx.sql.dataset.*;
3 import javax.swing.*;
4 import com.borland.dbswing.*;
5 import java.awt.event.WindowAdapter;
6 import java.awt.event.WindowEvent;
7 import java.net.URL;
8 import java.awt.print.Paper;
9 import java.awt.print.PageFormat;
10 import java.awt.*;
11 import java.sql.Statement;
12 import java.sql.ResultSet;
13 import java.util.Vector;
14
15
16
17 import org.jfree.report.JFreeReport;
18 import org.jfree.report.ReportProcessingException;
19 import org.jfree.report.modules.gui.base.PreviewFrame;
20 import org.jfree.report.modules.gui.base.components.ActionButton;
21 import org.jfree.report.modules.gui.base.components.ActionMenuItem;
22 import org.jfree.report.modules.parser.base.ReportGenerator;
23 import org.jfree.report.util.Log;
24 import org.jfree.report.util.WaitingImageObserver;
25 import org.jfree.report.modules.gui.base.PreviewDialog;
26
27
28 public class ScorePrint{
29 public TableModel sdata=null;
30 JFreeReport report = null;
31 Database database = new Database();
32 QueryDataSet queryDataSet = new QueryDataSet();
33 JPanel jPanel1 = new JPanel();
34 TableScrollPane tableScrollPane1 = new TableScrollPane();
35 JdbTable jdbTable1 = new JdbTable();
36
37
38 public ScorePrint() {
39 try {
40 jbInit();
41 }
42 catch(Exception e) {
43 e.printStackTrace();
44 }
45 }
46
47 private void jbInit() throws Exception {
48 database.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:odbc:zhong", "", "", false, "sun.jdbc.odbc.JdbcOdbcDriver"));
49 Statement stmt=database.getJdbcConnection().createStatement();
50
51 ResultSet rs=stmt.executeQuery("select bmxh,xm,chd,chw,mad,maw,[end],enw,phd,phw,pod,pow,totd from bmk order by bydm,bmxh");
52
53 Vector v=new Vector();
54 Vector aaa=new Vector();
55 v.add("bmxh");
56 v.add("xm");
57 v.add("chd");
58 v.add("chw");
59 v.add("mad");
60 v.add("maw");
61 v.add("end");
62 v.add("enw");
63 v.add("phd");
64 v.add("phw");
65 v.add("pod");
66 v.add("pow");
67 v.add("totd");
68 while(rs.next()){
69 Vector vv=new Vector();
70 vv.add(rs.getString(1));
71 vv.add(rs.getString(2));
72 vv.add(rs.getString(3));
73 vv.add(new Integer(rs.getInt(4)).toString());
74 vv.add(rs.getString(5));
75 vv.add(new Integer(rs.getInt(6)).toString());
76 vv.add(rs.getString(7));
77 vv.add(new Integer(rs.getInt(8)).toString());
78 vv.add(rs.getString(9));
79 vv.add(new Integer(rs.getInt(10)).toString());
80 vv.add(rs.getString(11));
81 vv.add(new Integer(rs.getInt(12)).toString());
82 vv.add(new Integer(rs.getInt("totd")).toString());
83 aaa.add(vv);
84 }
85 JTable jt=new JTable(aaa,v);
86
87
88 // 获得创建报表需要用到的数据
89 sdata=jt.getModel();
90
91 //获得报表要用到的报表定义内容
92
93 try {
94 if (report == null) {
95 URL in = getClass().getResource("/zmx/chosun/ScorePrintNew.xml");
96 this.report = parseReport(in);
97 }
98 if (report == null) {
99 JOptionPane.showMessageDialog(new JFrame(), "The report definition is null");
100 return;
101 }
102 }
103 catch (Exception e) {
104 e.printStackTrace();
105 }
106
107
117
118
119 //将报表定义和数据结合
120 this.report.setData(sdata);
121
122
123 try {
124 //将生成的报表放到预览窗口中
125 final PreviewDialog preview = new PreviewDialog(report);
126 preview.addWindowListener(new WindowAdapter() {
127 public void windowClosing(final WindowEvent event) {
128 preview.dispose();
129 }
130
131 });
132 preview.pack();
133 //显示报表预览窗口
134 preview.setVisible(true);
135 }
136 catch (ReportProcessingException e) {
137 System.out.println(e);
138 }
139
140
141
142
143
144 }
145
146 JFreeReport parseReport(URL templateURL) {
147
148 JFreeReport result = null;
149 ReportGenerator generator = ReportGenerator.getInstance();
150 try {
151 result = generator.parseReport(templateURL);
152 }
153 catch (Exception e) {
154 Log.error("Failed to parse the report definition", e);
155 }
156 return result;
157
158 }
159
160
161 }
本程序使用XML文件如下:
1 <?xml version="1.0" encoding="GBK"?>
2 <!DOCTYPE report PUBLIC "-//JFreeReport//DTD report definition//EN//simple"
3 "http://jfreereport.sourceforge.net/report.dtd">
4 <report name="成绩打印" pageformat="A4" leftmargin="20" topmargin="20" >
5
6 <!-- REPORT HEADER -->
7 <!-- This band appears once at the beginning of the report. In this example, -->
8 <!-- the header contains four labels. -->
9 <configuration>
10 <property name="org.jfree.report.modules.output.pageable.pdf.Encoding">Identity-H</property>
11 <property name="org.jfree.report.modules.output.pageable.pdf.EmbedFonts">true</property>
12 <property name="org.jfree.report.targets.table.TableWriter.SheetNameFunction">sheetNameExpression</property>
13 </configuration>
14
15 <pagefooter height="14"
16 onfirstpage="true" onlastpage="true"
17 fontname="宋体" fontstyle="plain" fontsize="9">
18 <string-field fieldname="PageNumber" x="0" y="2" width="100%" height="12" alignment="center"/>
19
20
21 </pagefooter>
22
23 <groups height="55">
24 <group name="bmxh">
25 <groupheader height="22" fontname="SansSerif" fontstyle="plain" fontsize="11" pagebreak="false">
26 <label x="0" y="10" width="100%" height="11" fontsize="11" alignment="left"
27 >报 名 号 姓 名 语文 位置值 数学 位置值 英语 位置值 理化 位置值 政史 位置值 总位置值</label>
28
29 </groupheader>
30 <fields>
31 <field>bmxh</field>
32 </fields>
33 <groupfooter height="5" fontstyle="plain" fontsize="11" pagebreak="false">
34 <line x1="0" y1="5" x2="0" y2="5" color="#CFCFCF" weight="0.5"/>
35 </groupfooter>
36 </group>
37 </groups>
38
39 <!-- THE ITEM BAND -->
40 <!-- This band is printed once for each row of data in the TableModel. -->
41 <items height="0" fontname="SansSerif" fontstyle="plain" fontsize="11">
42
43 <string-field x="0" y="1" width="80" height="12" alignment="left" fontname="宋体" fontsize="10"
44 fieldname="bmxh"/>
45 <string-field x="64" y="1" width="60" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
46 fieldname="xm"/>
47 <string-field x="110" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
48 fieldname="chd"/>
49 <string-field x="144" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
50 fieldname="chw"/>
51 <string-field x="175" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
52 fieldname="mad"/>
53 <string-field x="212" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
54 fieldname="maw"/>
55 <string-field x="240" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
56 fieldname="end"/>
57 <string-field x="273" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
58 fieldname="enw"/>
59 <string-field x="305" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
60 fieldname="phd"/>
61 <string-field x="339" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
62 fieldname="phw"/>
63 <string-field x="370" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
64 fieldname="pod"/>
65 <string-field x="405" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
66 fieldname="pow"/>
67 <string-field x="450" y="1" width="40" height="12" alignment="left" fontname="宋体" fontstyle="plain" fontsize="10"
68 fieldname="totd"/>
69
70
71
72
73 </items>
74
75 <!-- FUNCTIONS -->
76 <!-- Declares the functions used in this report. -->
77 <functions>
78
79 <function name="PageNumber" class="org.jfree.report.function.PageFunction">
80 </function>
81
82
83 </functions>
84
85 </report>
posted on 2006-02-28 11:51
Robin's Programming World 阅读(3296)
评论(0) 编辑 收藏 所属分类:
Java