iReport jasperReports 生成表格


使用iReport生成表格
 
一 環境:iReport-5.6.0  JDK7
1.注意,iReport的最新版本目前還不支持JDK8,如果項目工程已經配置了JDK8,那也不用去修改環境變量和工程的配置,只要修改iReport的配置文件即可。找到etc路徑下的ireport.conf,將以下一行:
#jdkhome="/path/to/jdk"
改為jdk7的路徑,如:
jdkhome="C:\Program Files\Java\jdk1.7.0_67"        
這樣iReport就可以順利啟動了。
2.打開iReport
有幾個比較常用的工具欄:
Window-->Palette 和 Properties,這兩個可以用來處理元素和樣式,是常用的兩個工具欄
 
二 使用過程
1. 先介紹一下最終希望生成的效果:
 
2. 看一下java端的代碼:
最終iReport要用到的數據是List<OceanRatesEmailMessage>這樣的lists,每個list包含一個title以及對應的label和value
public class OceanRatesEmailMessage {
  private String title;    //對應A. Backendground Information這樣的title
  private List<OceanRatesDetails> oceanRatesDetails;    
  ......
}
 
public class OceanRatesDetails {
  private String label;  //label
  private Object value;   //value
  private Integer combineColumns;   //控制value是分成一行還是兩行
  private boolean needHighLight;  //控制最后一行的顏色
  .......
}
 
3.添加數據源
如果通過JavaBeans set data source的方式來添加數據源,那么就需要提供一個類,這個即類名即圖二的Name參數,這個類的路徑即圖二的Factory class參數,這個類里必須要有一個靜態方法,提供所有的數據,這個方法名即The static method....參數。
這里不通過這種方式添加數據源。程序中並沒有提供靜態方法的類。
 

4.指定要在頁面展示和頁面控制的數據

首先確定所有頁面上要用到的數據,即title和 List<OceanRatesDetails>,將這兩個參數添加到fields里。這里相當於是定義了數據名以及數據類型。這里List類型的數據還要定義List中包含的數據。
這部分的數據可以從java文件里引入。tools-->options-->classpath,將這個模塊下的所有要用到的java文件引用進來:
點擊Add Datasource,這里命名為results,右擊results-->report query-->JavaBean Datasource-->添加自己需要的java對象的路徑-->read attribute-->將自己需要的屬性選中,add selected fields,這樣就可以將所有的數據都加載進來。

5.創建表格

將所有的參數加進來之后,接下來創建一個表格,選擇一個dataset,或者是選擇具體的column數目。
也可以確定table的一些樣式,這里什么樣式都不添加。
6.表格生成之后指定表格數據的來源。
右擊table-->edit table datasource-->指定bean的數據源,sub dataset選擇results, express這里指定為:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{oceanRatesDetails})
表現在xml文件里就是這樣一段話:
<datasetRun subDataset="results" uuid="c355b7b5-7454-4ea4-8dd1-e413601ed152">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{oceanRatesDetails})]]></dataSourceExpression>
</datasetRun>
這樣的話,也就是實現了表格數據源,不需要上面的第三步。
 
6.畫表格。
這里就涉及到如何切分表格和表格的具體樣式。
這里貼一下所有的代碼:
<?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="oceanRatesEmailDetails" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7c39aa93-0bd8-495f-acf2-8c13fccc68d9">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <subDataset name="details" uuid="8fcf3031-129c-40c1-9d56-37fe078bfce7">
        <field name="label" class="java.lang.String"/>
        <field name="value" class="java.lang.Object"/>
        <field name="combineColumns" class="java.lang.Integer"/>
        <field name="needHighLight" class="java.lang.Boolean"/>
    </subDataset>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[""]]></defaultValueExpression>
    </parameter>
    <field name="title" class="java.lang.String"/>
    <field name="oceanRatesDetails" class="java.util.List"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <pageHeader>
        <band height="28" splitType="Stretch">
            <image>
                <reportElement x="10" y="0" width="142" height="28" uuid="4074945c-59ac-4f4a-a931-f6db80a21f5f"/>
                <imageExpression><![CDATA[$P{SUBREPORT_DIR} + "DB-SCHENKER_rgb_M.png"]]></imageExpression>
            </image>
            <textField>
                <reportElement x="298" y="0" width="181" height="28" uuid="4bc7d029-9773-48bf-8be1-8af975e2c464"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="DB Office Global" size="16"/>
                </textElement>
                <textFieldExpression><![CDATA[$R{ocean-rates.slip-sheet-benefit.mail.header.title}]]></textFieldExpression>
            </textField>
            <image>
                <reportElement x="479" y="0" width="142" height="28" uuid="4074945c-59ac-4f4a-a931-f63b80a23f5f"/>
                <imageExpression><![CDATA[$P{SUBREPORT_DIR} + "oceanFrightSolutions.png"]]></imageExpression>
            </image>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="10" y="10" width="336" height="20" uuid="7309912c-bd55-41a0-88ec-142f770790eb"/>
                <textElement>
                    <font fontName="DB Office Global" size="13" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$R{ocean-rates.slip-sheet-benefit.title}]]></textFieldExpression>
            </textField>
        </band>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <componentElement>
                <reportElement key="table" mode="Opaque" x="10" y="20" width="650" height="85" uuid="d17eb789-8d34-4619-bc0d-210e5711bbfe"/>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="details" uuid="c355b7b5-7454-4ea4-8dd1-e413601ed152">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{oceanRatesDetails})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="300" uuid="2c546703-e51e-456c-8fac-d42cc8fece36">
                        <jr:detailCell height="20" rowSpan="1">
                            <textField>
                                <reportElement mode="Opaque" x="0" y="0" width="300" height="20" forecolor="#000000" backcolor="#FFFFFF" uuid="3770bd9e-d595-431a-b0bd-ed6c5813c485">
                                    <printWhenExpression><![CDATA[$F{needHighLight}==false]]></printWhenExpression>
                                </reportElement>
                                <box topPadding="1" leftPadding="2">
                                    <pen lineWidth="0.25"/>
                                    <topPen lineWidth="0.25"/>
                                    <leftPen lineWidth="0.25"/>
                                    <bottomPen lineWidth="0.25"/>
                                    <rightPen lineWidth="0.25"/>
                                </box>
                                <textElement>
                                    <font fontName="DB Office Global" size="11"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$F{label}]]></textFieldExpression>
                            </textField>
                            <textField>
                                <reportElement mode="Opaque" x="0" y="0" width="300" height="20" forecolor="#FFFFFF" backcolor="#003399" uuid="3770bc9e-d595-431a-b0bd-ed6c5823c485">
                                    <printWhenExpression><![CDATA[$F{needHighLight}==true]]></printWhenExpression>
                                </reportElement>
                                <box topPadding="1" leftPadding="2" rightPadding="0">
                                    <pen lineWidth="0.25" lineColor="#000000"/>
                                    <topPen lineWidth="0.25" lineColor="#000000"/>
                                    <leftPen lineWidth="0.25" lineColor="#000000"/>
                                    <bottomPen lineWidth="0.25" lineColor="#000000"/>
                                    <rightPen lineWidth="0.25" lineColor="#000000"/>
                                </box>
                                <textElement>
                                    <font fontName="DB Office Global" size="11"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$F{label}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:columnGroup width="220" uuid="a51ca014-f035-49d2-bf23-cbb80a4ef5e3">
                        <jr:column width="220" uuid="e7eb1ec7-cee4-4be2-a9b6-fe572d5629de">
                            <jr:detailCell height="20">
                                <textField>
                                    <reportElement mode="Opaque" x="0" y="0" width="220" height="20" forecolor="#FFFFFF" backcolor="#003399" uuid="0ab2394e-8a4a-4ca9-af8e-7284795c6e10">
                                        <printWhenExpression><![CDATA[$F{needHighLight}==true && $F{combineColumns}==null]]></printWhenExpression>
                                    </reportElement>
                                    <box topPadding="1" leftPadding="2">
                                        <pen lineWidth="0.25" lineColor="#000000"/>
                                        <topPen lineWidth="0.25" lineColor="#000000"/>
                                        <leftPen lineWidth="0.25" lineColor="#000000"/>
                                        <bottomPen lineWidth="0.25" lineColor="#000000"/>
                                        <rightPen lineWidth="0.25" lineColor="#000000"/>
                                    </box>
                                    <textElement>
                                        <font fontName="DB Office Global" size="11"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[""+$F{value}]]></textFieldExpression>
                                </textField>
                                <textField>
                                    <reportElement mode="Opaque" x="0" y="0" width="220" height="20" forecolor="#000000" backcolor="#FFFFFF" uuid="0ab2294e-8a4a-4ca9-af8e-7284795c6e20">
                                        <printWhenExpression><![CDATA[$F{needHighLight}==false && $F{combineColumns}==null]]></printWhenExpression>
                                    </reportElement>
                                    <box topPadding="1" leftPadding="2">
                                        <pen lineWidth="0.25" lineColor="#000000"/>
                                        <topPen lineWidth="0.25" lineColor="#000000"/>
                                        <leftPen lineWidth="0.25" lineColor="#000000"/>
                                        <bottomPen lineWidth="0.25" lineColor="#000000"/>
                                        <rightPen lineWidth="0.25" lineColor="#000000"/>
                                    </box>
                                    <textElement>
                                        <font fontName="DB Office Global" size="11"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[""+$F{value}]]></textFieldExpression>
                                </textField>
                                <textField>
                                    <reportElement mode="Opaque" x="0" y="0" width="220" height="20" forecolor="#FFFFFF" backcolor="#003399" uuid="0ab2394e-8a4a-04c9-af8e-7284795c6e10">
                                        <printWhenExpression><![CDATA[$F{needHighLight}==true && $F{combineColumns}==1]]></printWhenExpression>
                                    </reportElement>
                                    <box topPadding="1" leftPadding="100">
                                        <pen lineWidth="0.25" lineColor="#000000"/>
                                        <topPen lineWidth="0.25" lineColor="#000000"/>
                                        <leftPen lineWidth="0.25" lineColor="#000000"/>
                                        <bottomPen lineWidth="0.25" lineColor="#000000"/>
                                        <rightPen lineWidth="0.25" lineColor="#000000"/>
                                    </box>
                                    <textElement textAlignment="Center" verticalAlignment="Middle">
                                        <font fontName="DB Office Global" size="11"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[""+$F{value}]]></textFieldExpression>
                                </textField>
                                <textField>
                                    <reportElement mode="Opaque" x="0" y="0" width="220" height="20" forecolor="#000000" backcolor="#FFFFFF" uuid="ab22944e-8a4a-4ca9-af8e-7384795c6e30">
                                        <printWhenExpression><![CDATA[$F{needHighLight}==false && $F{combineColumns}==1]]></printWhenExpression>
                                    </reportElement>
                                    <box topPadding="1" leftPadding="100">
                                        <pen lineWidth="0.25" lineColor="#000000"/>
                                        <topPen lineWidth="0.25" lineColor="#000000"/>
                                        <leftPen lineWidth="0.25" lineColor="#000000"/>
                                        <bottomPen lineWidth="0.25" lineColor="#000000"/>
                                        <rightPen lineWidth="0.25" lineColor="#000000"/>
                                    </box>
                                    <textElement textAlignment="Center" verticalAlignment="Middle">
                                        <font fontName="DB Office Global" size="11"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[""+$F{value}]]></textFieldExpression>
                                </textField>
                                <textField>
                                    <reportElement mode="Opaque" x="0" y="0" width="110" height="20" forecolor="#000000" backcolor="#FFFFFF" uuid="0ab2194e-8a4a-4ca9-af8e-7284795c6e10">
                                        <printWhenExpression><![CDATA[$F{combineColumns}==2]]></printWhenExpression>
                                    </reportElement>
                                    <box topPadding="1" leftPadding="2">
                                        <pen lineWidth="0.25"/>
                                        <topPen lineWidth="0.25"/>
                                        <leftPen lineWidth="0.25"/>
                                        <bottomPen lineWidth="0.25"/>
                                        <rightPen lineWidth="0.25"/>
                                    </box>
                                    <textElement>
                                        <font fontName="DB Office Global" size="11"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[""+((ArrayList<Object>)$F{value}).get(0)]]></textFieldExpression>
                                </textField>
                                <textField>
                                    <reportElement mode="Opaque" x="110" y="0" width="110" height="20" forecolor="#000000" backcolor="#FFFFFF" uuid="01b82940-8a4a-4cc9-af8e-7284795c6e10">
                                        <printWhenExpression><![CDATA[$F{combineColumns}==2]]></printWhenExpression>
                                    </reportElement>
                                    <box topPadding="1" leftPadding="2" rightPadding="0">
                                        <pen lineWidth="0.25"/>
                                        <topPen lineWidth="0.25"/>
                                        <leftPen lineWidth="0.25"/>
                                        <bottomPen lineWidth="0.25"/>
                                        <rightPen lineWidth="0.25"/>
                                    </box>
                                    <textElement>
                                        <font fontName="DB Office Global" size="11"/>
                                    </textElement>
                                    <textFieldExpression><![CDATA[""+((ArrayList<Object>)$F{value}).get(1)]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:columnGroup>
                </jr:table>
            </componentElement>
            <textField>
                <reportElement mode="Opaque" x="10" y="0" width="545" height="20" uuid="0961c008-c4d9-4673-827d-a2b798f5fd3c"/>
                <textElement>
                    <font fontName="DB Office Global" size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
這里有幾點需要注意:
<printWhenExpression>用來控制某一列是否出現
forecolor和backcolor控制前景和背景顏色
顯示三列還是兩列可以通過在同一個<jr:column>里按條件顯示不同的列即可
表格的線條通過<box>的linewidth等來控制
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM