本文主要是介绍struts2+ireport进行报表开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先去官方网站将所需要的资源包下载下来。
http://jasperforge.org/
然后,通过JasperReports产生一个jrxml文件(可以生成静态的,也可以生成与数据库对应的),本例生成一个与数据库MyDatabase中表student对应的classic.jrxml文件。
- <?xml version="1.0" encoding="UTF-8" ?>
- <!-- Created with iReport - A designer for JasperReports -->
- <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
- <jasperReport
- name="classic"
- columnCount="1"
- printOrder="Vertical"
- orientation="Portrait"
- pageWidth="595"
- pageHeight="842"
- columnWidth="535"
- columnSpacing="0"
- leftMargin="30"
- rightMargin="30"
- topMargin="20"
- bottomMargin="20"
- whenNoDataType="NoPages"
- isTitleNewPage="false"
- isSummaryNewPage="false">
- <property name="ireport.scriptlethandling" value="0" />
- <property name="ireport.encoding" value="UTF-8" />
- <import value="java.util.*" />
- <import value="net.sf.jasperreports.engine.*" />
- <import value="net.sf.jasperreports.engine.data.*" />
- <queryString><![CDATA[select * from student]]></queryString>
- <field name="xuehao" class="java.lang.String"/>
- <field name="userName" class="java.lang.String"/>
- <field name="userPwd" class="java.lang.String"/>
- <field name="czxt" class="java.lang.Integer"/>
- <field name="wjyl" class="java.lang.Integer"/>
- <field name="sjjg" class="java.lang.Integer"/>
- <group name="xuehao" >
- <groupExpression><![CDATA[$F{xuehao}]]></groupExpression>
- <groupHeader>
- <band height="25" isSplitAllowed="true" >
- <rectangle>
- <reportElement
- mode="Opaque"
- x="0"
- y="0"
- width="535"
- height="25"
- forecolor="#8080FF"
- backcolor="#000000"
- key="rectangle"/>
- <graphicElement stretchType="NoStretch">
- <pen lineWidth="0.0" lineStyle="Solid"/>
- </graphicElement>
- </rectangle>
- <staticText>
- <reportElement
- x="3"
- y="2"
- width="96"
- height="23"
- forecolor="#FFFFFF"
- backcolor="#FFFFFF"
- key="staticText"/>
- <box> <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
- <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
- <bottomPen lineWidth="0.0" lineColor="#000000"/>
- <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
- </box>
- <textElement>
- <font size="16"/>
- </textElement>
- <text><![CDATA[xuehao]]></text>
- </staticText>
- .
- .
- .
- .
- <line direction="TopDown">
- <reportElement
- x="0"
- y="3"
- width="535"
- height="0"
- key="line"/>
- <graphicElement stretchType="NoStretch"/>
- </line>
- <textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluati hyperlinkType="None" hyperlinkTarget="Self" >
- <reportElement
- x="1"
- y="6"
- width="209"
- height="19"
- forecolor="#000000"
- backcolor="#FFFFFF"
- key="textField"/>
- <box> <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
- <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
- <bottomPen lineWidth="0.0" lineColor="#000000"/>
- <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
- </box>
- <textElement>
- <font size="10"/>
- </textElement>
- <textFieldExpression class="java.util.Date"><![CDATA[new Date()]]></textFieldExpression>
- </textField>
- </band>
- </pageFooter>
- <summary>
- <band height="0" isSplitAllowed="true" >
- </band>
- </summary>
- </jasperReport>
Java的环境配置好后,就可以MyCompile.java产生PDF、XML、EXCEL等格式。
- /**
- * @作者 Jcuckoo
- * @创建日期 2008-12-08
- * @版本 V 1.0
- */
- public class MyCompile {
- public static void main(String[] args) throws Exception {
- //导出jasper格式
- JasperCompileManager.compileReportToFile("classic.jrxml","classic.jasper");
- System.out.println("成功编译成JasperReport文件(classic.jasper)");
- //填充时,使用数据源数据连接,产生带有数据的jasper
- JasperFillManager.fillReportToFile("classic.jasper" , null ,dbPool.getConnection());
- System.out.println("成功填充了一个报表文件(*.jrprint)");
- //填充时,即使没有使用数据源,也必须指定一个新建的JREmptyDataSource实例,而不能直接使用null
- //JasperRunManager.runReportToPdfFile("classic.jasper" , "classic.pdf" , null , new JREmptyDataSource());
- JasperRunManager.runReportToPdfFile("classic.jasper" , "classic.pdf" , null , dbPool.getConnection());
- System.out.println("成功编译成JasperReport文件Jasper2Pdf");
- //导出XML格式
- JasperExportManager.exportReportToXmlFile("classic.jrprint" , "classic.xml" , true);
- System.out.println("成功创建了一个XML文档");
- //到处excel格式
- JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject("classic.jrprint");
- JRXlsExporter exporter = new JRXlsExporter();
- //设置要导出的jasperPrint
- exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
- exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "classic.xls");
- exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
- exporter.exportReport();
- System.out.println("成功创建了一个excel文档");
- JRViewer jr = new JRViewer("classic.jrprint" , false);
- jr.setPreferredSize(new Dimension(900, 730));
- JFrame jf = new JFrame("报表预览");
- jf.add(jr);
- jf.pack();
- jf.setVisible(true);
- }
- }
将Strut2的环境配置好后,就可以通过web的方式进行访问了。
index.jsp
- <body>
- <a href="htmlexport.action">html格式报表</a>
- <hr>
- <a href="pdfexport.action">pdf格式报表</a>
- <hr>
- <a href="xmlexport.action">XML格式报表</a>
- <hr>
- <a href="excelexport.action">excel格式报表</a>
- </body>
struts.xml
- <package name="jCuckoo" extends="jasperreports-default">//此处要注意。
- <action name="htmlexport" class="jCuckoo.JasperAction">
- <result name="success" type="jasper">
- <param name="location">jasper/classic.jasper</param>
- <param name="format">HTML</param>
- <param name="dataSource">studentList</param>
- </result>
- </action>
- <action name="pdfexport" class="jCuckoo.JasperAction">
- <result name="success" type="jasper">
- <param name="location">jasper/classic.jasper</param>
- <param name="format">PDF</param>
- <param name="dataSource">studentList</param>
- </result>
- </action>
- <action name="xmlexport" class="jCuckoo.JasperAction">
- <result name="success" type="jasper">
- <param name="location">jasper/classic.jasper</param>
- <param name="format">XML</param>
- <param name="dataSource">studentList</param>
- </result>
- </action>
- <action name="excelexport" class="jCuckoo.JasperAction">
- <result name="success" type="jasper">
- <param name="location">jasper/classic.jasper</param>
- <param name="format">XLS</param>
- <param name="dataSource">studentList</param>
- </result>
- </action>
- </package>
struts2-jasperreports-plugin-2.0.8.jar中的struts2-jasperreports-plugin-2.0.8.jar需要修改成:
该处要注意
- <package name="jasperreports-default" extends="struts-default">
- <result-types>
- <result-type name="jasper" class="org.apache.struts2.views.jasperreports.JasperReportsResult"/>
- </result-types>
- </package>
JasperAction.java
- public class JasperAction extends ActionSupport
- {
- public List getStudentList()
- {
- StudentDAO studao=new StudentDAO();
- return studao.getAllStudent();
- }
- public String execute() throws Exception {
- return SUCCESS;
- }
- }
StudentDAO.java
- public class StudentDAO {
- public StudentDAO() {
- }
- Connection conn;
- Statement st;
- ResultSet rs;
- public List getAllStudent(){
- String sql="select * from student ";
- List students=new ArrayList();
- try {
- conn=dbPool.getConnection();
- st=conn.createStatement();
- rs=st.executeQuery(sql);
- while(rs.next()){
- Student student=new Student(rs.getString("xuehao"),
- rs.getString("userName"),
- rs.getString("userPwd"),
- rs.getInt("czxt"),
- rs.getInt("wjyl"),
- rs.getInt("sjjg")
- );
- students.add(student);
- }
- } catch (SQLException e) {
- try {
- conn.close();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- return null;
- }
- return students;
- }
- }
dbPool.java
- /**
- * @作者 Jcuckoo
- * @创建日期 2008-12-06
- * @版本 V 1.0
- */
- public class dbPool{
- private static dbPool instance = null;
- //取得连接
- public static ResultSet getAllStudent(){
- String sql="select * from student ";
- Connection conn=null;
- Statement st;
- ResultSet rs=null;
- try {
- conn=dbPool.getConnection();
- st=conn.createStatement();
- rs=st.executeQuery(sql);
- return rs;
- } catch (SQLException e) {
- try {
- conn.close();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- return null;
- }
- }
- public static synchronized Connection getConnection() {
- if (instance == null){
- instance = new dbPool();
- }
- return instance._getConnection();
- }
- private dbPool(){
- super();
- }
- private Connection _getConnection(){
- try{
- String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
- String sConnection = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDatabase";
- String sUser = "sa";
- String sPassword = null;
- Class.forName(sDBDriver).newInstance();
- return DriverManager.getConnection(sConnection,sUser,sPassword);
- }
- catch(Exception se){
- System.out.println(se);
- return null;
- }
- }
- //释放资源
- public static void dbClose(Connection conn,PreparedStatement ps,ResultSet rs)
- throws SQLException
- {
- rs.close();
- ps.close();
- conn.close();
- }
- }
这篇关于struts2+ireport进行报表开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!