2012-05-09 46 views
1

我在碧玉報告有一個查詢,並通過用戶給定的輸入產生的結果集的一個可能被這些數據說明如下:對數據集分組連續編號中的JasperReports(iReport的)

memberId stockNo refineryCode constantSerialNo serialNo 
45  1  IAR   A-    98729 
45  1  IAR   A-    98730 
45  1  IAR   A-    98731 
45  1  IAR   A-    98733 
45  1  IAR   A-    98734 
45  1  IAR   A-    98736 
45  1  IAR   A-    98737 
45  1  IAR   A-    98738 
45  1  IAR   A-    98739 

而且如果serialNo是連續的,我想在一行上顯示這些數據。所以,如果我想在報表中顯示上述數據,我在下面陳述的方式來顯示它們:

memberId stockNo refineryCode constantSerialNo serialNo 
45  1  IAR   A-    98729 - 98731 
45  1  IAR   A-    98733 - 98734 
45  1  IAR   A-    98736 - 98739 

我知道可能有一些解決方案使用的SQL遊標或使用ORM在OOP語言像Java一樣發送到碧玉報告。然而,出於好奇,我想問一下,是否有可能使用iReport的表達式或組或任何其他現在沒有想到的可以使我的生活更輕鬆的動態解決方案。

+0

在orcale中有一種方法可以創建一個查詢,以便爲您提供這樣的結果。除了它會列出SerialNo爲98729,98730,98731 – Sibster

+0

你是對的,必須有一種方法實現我想要使用查詢,但我想通過使用jasper報告解決方案 - iReport。 –

+0

您可能可以使用ireport中的數據透視表執行此操作。 – Sibster

回答

3

這裏有一種方法,假設serialNo是某種數值而不是字符串。

  1. sql查詢結果的數據集必須按serialNo排序,如上所示。
  2. 使用以下組表達式創建Report Group$V{GroupCount} == new BigDecimal(0) ? $F{serialNo}:$F{serialNo}.subtract($V{GroupCount})。這將在serialNo序列中每次出現間隙時創建一個新組。
  3. 創建一個變量GroupCount,該變量計算當前組中連續的serialNo值的數量。該變量用於跟蹤下一個預期值serialNo的值。
  4. 創建一個變量StartRange,該變量將保存給定範圍的連續serialNo值的起始值。該值應在新組的開始時重置,並具有以下值:$V{GroupCount}.intValue() == 1 ? $F{ID}:$V{StartRange}
  5. serialNo字段的Evaluation Time值設置爲ReportGroup。把下面的值放在它裏面:$V{StartRange}+" - " + $F{serialNo}
  6. Detail頻段的Print When Expression更改爲以下網址:new Boolean($V{GroupCount}.intValue() == 1)

我已在下面發佈完整的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="Group_By_Consecutive" 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 1 as id, 'Name 1' as name from dual union all 
select 2 as id, 'Name 2' as name from dual union all 
select 3 as id, 'Name 3' as name from dual union all 
select 4 as id, 'Name 4' as name from dual union all 
select 6 as id, 'Name 6' as name from dual union all 
select 7 as id, 'Name 7' as name from dual union all 
select 8 as id, 'Name 8' as name from dual union all 
select 9 as id, 'Name 9' as name from dual union all 
select 10 as id, 'Name 10' as name from dual union all 
select 11 as id, 'Name 11' as name from dual union all 
select 14 as id, 'Name 14' as name from dual union all 
select 15 as id, 'Name 15' as name from dual union all 
select 16 as id, 'Name 16' as name from dual union all 
select 17 as id, 'Name 17' as name from dual union all 
select 23 as id, 'Name 23' as name from dual union all 
select 24 as id, 'Name 24' as name from dual union all 
select 25 as id, 'Name 25' as name from dual union all 
select 26 as id, 'Name 26' as name from dual union all 
select 27 as id, 'Name 27' as name from dual union all 
select 28 as id, 'Name 28' as name from dual]]> 
</queryString> 
    <field name="ID" class="java.math.BigDecimal" /> 
    <field name="NAME" class="java.lang.String" /> 
    <variable name="GroupCount" class="java.math.BigDecimal" resetType="Group" resetGroup="Consecutive" calculation="Count"> 
    <variableExpression> 
     <![CDATA[$F{ID}]]> 
</variableExpression> 
    <initialValueExpression> 
     <![CDATA[new BigDecimal(0)]]> 
</initialValueExpression> 
    </variable> 
    <variable name="StartRange" class="java.math.BigDecimal" resetType="Report" calculation="Nothing"> 
    <variableExpression> 
     <![CDATA[$V{GroupCount}.intValue() == 1 ? $F{ID}:$V{StartRange}]]> 
</variableExpression> 
    </variable> 
    <group name="Consecutive"> 
    <groupExpression> 
     <![CDATA[$V{GroupCount} == new BigDecimal(0) ? $F{ID}:$F{ID}.subtract($V{GroupCount})]]> 
</groupExpression> 
    <groupHeader> 
     <band height="0" isSplitAllowed="true"></band> 
    </groupHeader> 
    <groupFooter> 
     <band height="0" isSplitAllowed="true"></band> 
    </groupFooter> 
    </group> 
    <background> 
    <band height="0" isSplitAllowed="true"></band> 
    </background> 
    <title> 
    <band height="0" isSplitAllowed="true"></band> 
    </title> 
    <pageHeader> 
    <band height="0" isSplitAllowed="true"></band> 
    </pageHeader> 
    <columnHeader> 
    <band height="18" isSplitAllowed="true"> 
     <staticText> 
     <reportElement x="0" y="0" width="200" height="18" key="staticText-1" /> 
     <box></box> 
     <textElement> 
      <font /> 
     </textElement> 
     <text> 
      <![CDATA[serialNo]]> 
</text> 
     </staticText> 
     <staticText> 
     <reportElement x="200" y="0" width="100" height="18" key="staticText-3" /> 
     <box></box> 
     <textElement> 
      <font /> 
     </textElement> 
     <text> 
      <![CDATA[Name]]> 
</text> 
     </staticText> 
     <staticText> 
     <reportElement x="300" y="0" width="100" height="18" key="staticText-4" /> 
     <box></box> 
     <textElement> 
      <font /> 
     </textElement> 
     <text> 
      <![CDATA[MetaData(Ignore)]]> 
</text> 
     </staticText> 
    </band> 
    </columnHeader> 
    <detail> 
    <band height="18" isSplitAllowed="true"> 
     <printWhenExpression> 
     <![CDATA[new Boolean($V{GroupCount}.intValue() == 1)]]> 
</printWhenExpression> 
     <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self"> 
     <reportElement x="200" y="0" width="100" height="18" key="textField" /> 
     <box></box> 
     <textElement> 
      <font /> 
     </textElement> 
     <textFieldExpression class="java.lang.String"> 
      <![CDATA[$F{NAME}]]> 
</textFieldExpression> 
     </textField> 
     <textField isStretchWithOverflow="false" pattern="##0.00" isBlankWhenNull="false" evaluationTime="Group" evaluationGroup="Consecutive" hyperlinkType="None" hyperlinkTarget="Self"> 
     <reportElement x="0" y="0" width="200" height="18" key="textField" /> 
     <box></box> 
     <textElement> 
      <font /> 
     </textElement> 
     <textFieldExpression class="java.lang.String"> 
      <![CDATA[$V{StartRange}+" - " + $F{ID}]]> 
</textFieldExpression> 
     </textField> 
     <textField isStretchWithOverflow="false" pattern="##0.00" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self"> 
     <reportElement x="300" y="0" width="100" height="18" key="textField" /> 
     <box></box> 
     <textElement> 
      <font /> 
     </textElement> 
     <textFieldExpression class="java.math.BigDecimal"> 
      <![CDATA[$V{GroupCount}]]> 
</textFieldExpression> 
     </textField> 
    </band> 
    </detail> 
    <columnFooter> 
    <band height="0" isSplitAllowed="true"></band> 
    </columnFooter> 
    <pageFooter> 
    <band height="0" isSplitAllowed="true"></band> 
    </pageFooter> 
    <summary> 
    <band height="0" isSplitAllowed="true"></band> 
    </summary> 
</jasperReport> 
+0

這是非常令人印象深刻的,你的一部分創意。唯一的辦法是將第一個Textfield列更改爲「$ V {StartRange} == $ F {ID}? $ F {ID} .toString():$ V {StartRange} +「 - 」+ $ F {ID}'當組中只有一個項目時,它不打印破折號。看看我的意思是選擇id爲10.這會給你11個小組。 –

+0

此外,由於您只是打印組中的第一個項目,所以GroupCount列總是有1,但我不認爲在這種情況下很重要,因爲我們並不在乎組中有多少項目。 –

+0

@jschoen很好的接收,我從來沒有想過這種情況。 – user845279