http://blog.sina.com.cn/secretletter[订阅]
字体大小: 正文
通过Jasperreport,iReport实现报表定制(2009-06-18 19:38:05)

    Jasperreport是一个报表制作程序,用户需要按照它制定的规则编写 一个XML文件,然后得到用户需要输出的格式文件;它支持输出的文件格式包括PDF,HTML,XML,XLS,CVS等。而iReport就是一个制 作Jasperreport的XML文件的可视化开发工具。

 

下载地址:

ireport地址:

http://ireport.sourceforge.net

jasperreports地址:

http://jasperreports.sourceforge.net 

 

开发步骤:

(1)扩展名为.jrxml的文件为标准的xml文件,该文件定义了报表的格式和数据构成;可以通过iReport进行定制。

(2)jrxml文件经过JasperReports API 编译后将生成扩展名为.jasper的二进制文件。

(3)可以调用JasperReports API针对jasper文件进行数据和参数的填充,生成扩展名为.jrprint的文件。

(4)调用JasperReports API可以将jrprint文件最终导出成PDF、Excel、Html等各种格式的文件

 

工程实践:

(1)report3.jrxml

<?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="report3" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">

       <queryString language="SQL">

              <![CDATA[select * from organization]]>

       </queryString>

       <field name="ORGID" class="java.math.BigDecimal">

              <fieldDescription><![CDATA[]]></fieldDescription>

       </field>

       <field name="PARENTID" class="java.math.BigDecimal">

              <fieldDescription><![CDATA[]]></fieldDescription>

       </field>

       <field name="CODE" class="java.lang.String">

              <fieldDescription><![CDATA[]]></fieldDescription>

       </field>

       <field name="ORGNAME" class="java.lang.String">

              <fieldDescription><![CDATA[]]></fieldDescription>

       </field>

       <field name="DESCRIBE" class="java.lang.String">

              <fieldDescription><![CDATA[]]></fieldDescription>

       </field>

       <field name="MODIFYTIME" class="java.sql.Timestamp">

              <fieldDescription><![CDATA[]]></fieldDescription>

       </field>

       <field name="CREATETIME" class="java.sql.Timestamp">

              <fieldDescription><![CDATA[]]></fieldDescription>

       </field>

       <background>

              <band splitType="Stretch"/>

       </background>

       <title>

              <band height="58" splitType="Stretch">

                     <line>

                            <reportElement x="0" y="8" width="555" height="1"/>

                     </line>

                     <line>

                            <reportElement positionType="FixRelativeToBottom" x="0" y="51" width="555" height="1"/>

                     </line>

                     <staticText>

                            <reportElement x="65" y="13" width="424" height="35"/>

                            <textElement textAlignment="Center">

                                   <font size="26" isBold="true"/>

                            </textElement>

                            <text><![CDATA[Classic template]]></text>

                     </staticText>

              </band>

       </title>

       <pageHeader>

              <band splitType="Stretch"/>

       </pageHeader>

       <columnHeader>

              <band splitType="Stretch"/>

       </columnHeader>

       <detail>

              <band height="140" splitType="Stretch">

                     <staticText>

                            <reportElement x="0" y="0" width="139" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <text><![CDATA[ORGID]]></text>

                     </staticText>

                     <textField>

                            <reportElement x="139" y="0" width="416" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{ORGID}]]></textFieldExpression>

                     </textField>

                     <staticText>

                            <reportElement x="0" y="20" width="139" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <text><![CDATA[PARENTID]]></text>

                     </staticText>

                     <textField>

                            <reportElement x="139" y="20" width="416" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{PARENTID}]]></textFieldExpression>

                     </textField>

                     <staticText>

                            <reportElement x="0" y="40" width="139" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <text><![CDATA[CODE]]></text>

                     </staticText>

                     <textField>

                            <reportElement x="139" y="40" width="416" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <textFieldExpression class="java.lang.String"><![CDATA[$F{CODE}]]></textFieldExpression>

                     </textField>

                     <staticText>

                            <reportElement x="0" y="60" width="139" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <text><![CDATA[ORGNAME]]></text>

                     </staticText>

                     <textField>

                            <reportElement x="139" y="60" width="416" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <textFieldExpression class="java.lang.String"><![CDATA[$F{ORGNAME}]]></textFieldExpression>

                     </textField>

                     <staticText>

                            <reportElement x="0" y="80" width="139" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <text><![CDATA[DESCRIBE]]></text>

                     </staticText>

                     <textField>

                            <reportElement x="139" y="80" width="416" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <textFieldExpression class="java.lang.String"><![CDATA[$F{DESCRIBE}]]></textFieldExpression>

                     </textField>

                     <staticText>

                            <reportElement x="0" y="100" width="139" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <text><![CDATA[MODIFYTIME]]></text>

                     </staticText>

                     <textField>

                            <reportElement x="139" y="100" width="416" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{MODIFYTIME}]]></textFieldExpression>

                     </textField>

                     <staticText>

                            <reportElement x="0" y="120" width="139" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <text><![CDATA[CREATETIME]]></text>

                     </staticText>

                     <textField>

                            <reportElement x="139" y="120" width="416" height="20"/>

                            <textElement>

                                   <font size="12"/>

                            </textElement>

                            <textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{CREATETIME}]]></textFieldExpression>

                     </textField>

              </band>

       </detail>

       <columnFooter>

              <band splitType="Stretch"/>

       </columnFooter>

       <pageFooter>

              <band height="26" splitType="Stretch">

                     <textField uationTime="Report" pattern="" isBlankWhenNull="false">

                            <reportElement key="textField" x="516" y="6" width="36" height="19" forecolor="#000000" backcolor="#FFFFFF"/>

                            <box>

                                   <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>

                                   <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>

                                   <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>

                                   <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>

                            </box>

                            <textElement>

                                   <font size="10"/>

                            </textElement>

                            <textFieldExpression class="java.lang.String"><![CDATA["" + $V{PAGE_NUMBER}]]></textFieldExpression>

                     </textField>

                     <textField pattern="" isBlankWhenNull="false">

                            <reportElement key="textField" x="342" y="6" width="170" height="19" forecolor="#000000" backcolor="#FFFFFF"/>

                            <box>

                                   <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>

                                   <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>

                                   <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>

                                   <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>

                            </box>

                            <textElement textAlignment="Right">

                                   <font size="10"/>

                            </textElement>

                            <textFieldExpression class="java.lang.String"><![CDATA["Page " + $V{PAGE_NUMBER} + " of "]]></textFieldExpression>

                     </textField>

                     <textField pattern="" isBlankWhenNull="false">

                            <reportElement key="textField" x="1" y="6" width="209" height="19" forecolor="#000000" backcolor="#FFFFFF"/>

                            <box>

                                   <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>

                                   <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>

                                   <bottomPen lineWidth="0.0" lineStyle="Solid" 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 splitType="Stretch"/>

       </summary>

</jasperReport>

 

(2)test.jsp

导出excel,pdf:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<%@page import="java.sql.DriverManager,java.util.HashMap,java.util.Map" %>

<%@page import="net.sf.jasperreports.engine.JRException" %>

<%@page import="net.sf.jasperreports.engine.JasperCompileManager" %>

<%@page import="net.sf.jasperreports.engine.JasperFillManager" %>

<%@page import="net.sf.jasperreports.engine.JasperPrint" %>

<%@page import="java.sql.Connection" %>

<%@page import ="net.sf.jasperreports.engine.JasperPrint" %>

<%@page import ="net.sf.jasperreports.engine.export.JRPdfExporter" %>

<%@page import ="net.sf.jasperreports.engine.util.JRLoader" %>

<%@page import ="net.sf.jasperreports.engine.util.JRLoader" %>

<%@page import = "net.sf.jasperreports.engine.JRExporterParameter" %>

<%@page import = "java.io.File" %>

<%@page import ="net.sf.jasperreports.engine.JasperRunManager"  %>

<%@page import="net.sf.jasperreports.engine.export.JRXlsExporter" %>

<%@page import="net.sf.jasperreports.engine.JRExporterParameter" %>

<%@page import="java.io.*" %>

<%@page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter" %>

 

<%

try {

                  System.out.println(new String(request.getRealPath("/report3.jrxml")));

                     JasperCompileManager.compileReportToFile(new String(request.getRealPath("/report3.jrxml")));

                     Map parameters = new HashMap();

                     //parameters.put("ReportTitle", "Address Report");

                     //parameters.put("BaseDir", reportFile.getParentFile());

                     Class.forName("oracle.jdbc.driver.OracleDriver");

               Connection con = DriverManager.getConnection(

               "jdbc:oracle:thin:@192.168.1.63:1521:orcl", "system",

               "ora1110");

               

               Class.forName("oracle.jdbc.driver.OracleDriver");

               Connection conn = DriverManager.getConnection(

               "jdbc:oracle:thin:@192.168.1.63:1521:orcl", "system",

               "ora1110");

                                   

                     JasperPrint jasperPrint =

                            JasperFillManager.fillReport(

                                request.getRealPath("/report3.jasper"),

                                   parameters,

                                   conn

                                   );

                                  

                     ByteArrayOutputStream oStream = new ByteArrayOutputStream();

 

            JRXlsExporter exporter = new JRXlsExporter();

            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

                  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);

                  exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE); // 删除记录最下面的空行

                  exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);// 删除多余的ColumnHeader

                  exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);// 显示边框

                  exporter.exportReport();

  

                   byte[] bytes = oStream.toByteArray();

  

                if(bytes != null && bytes.length > 0) {

                  response.reset();

               response.setContentType("application/vnd.ms-excel");

               response.setContentLength(bytes.length);

               ServletOutputStream ouputStream = response.getOutputStream();

               ouputStream.write(bytes,0,bytes.length);

               ouputStream.flush();

               ouputStream.close();

          }else{

               out.print("bytes were null!");

          }

  

          conn.close();

                System.out.println("****jasperPrint:"+jasperPrint); 

                    

             

                    

             

      

       

              } catch (Exception e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

             

       %>

加载中,请稍候...
  • 评论加载中,请稍候...

验证码:请点击后输入验证码  收听验证码

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

相关博文
读取中...
推荐博文
读取中...