jasper表中单个列内的多行

时间:2014-07-02 10:05:47

标签: jasper-reports ireport

我只是jasper报告的初学者,我在问题中被困了几天。 我有两张桌子

药物{ID,药物名称} 和 dose_time {ID,medication_id,dose_time}

申请

SELECT m.id,m.medication_name,d_t.dose_time FROM medication `m`
LEFT JOIN dose_time d_t ON m.id=d_t.medication_id;

给了我三个结果enter image description here

现在我想要的是跟随jasper报告

enter image description here

但我可能会卡住并且只能生成以下jasper格式

enter image description here

我的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="PracticeReport" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="67dfb372-a5be-403b-9007-61ab07fe88e7">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <subDataset name="Medication" uuid="1cccb880-701b-491e-8094-c133d4bd3819">
        <queryString>
            <![CDATA[SELECT m.id,m.medication_name,d_t.dose_time FROM medication `m`
LEFT JOIN dose_time d_t ON m.id=d_t.medication_id;]]>
        </queryString>
        <field name="id" class="java.lang.Integer">
            <fieldDescription><![CDATA[]]></fieldDescription>
        </field>
        <field name="medication_name" class="java.lang.String">
            <fieldDescription><![CDATA[]]></fieldDescription>
        </field>
        <field name="dose_time" class="java.lang.String">
            <fieldDescription><![CDATA[]]></fieldDescription>
        </field>
        <group name="id">
            <groupExpression><![CDATA[$F{id}]]></groupExpression>
        </group>
    </subDataset>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement uuid="8bc54002-3e51-4bd6-8801-9856ef627b99" x="154" y="28" width="240" height="20"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Practicing Jasper]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <componentElement>
                <reportElement uuid="5976a679-e65a-4880-a9e7-74fd65f9e80d" key="" isPrintRepeatedValues="false" x="0" y="0" width="555" height="125">
                    <printWhenExpression><![CDATA[$V{REPORT_COUNT}==1]]></printWhenExpression>
                </reportElement>
                <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" whenNoDataType="AllSectionsNoDetail">
                    <datasetRun subDataset="Medication" uuid="5f6c54e4-9c5c-4120-8ac7-32793ec39d00"/>
                    <jr:column width="232" uuid="c5a6dca0-3be4-422f-8f13-4462d2e4caee">
                        <jr:detailCell height="124" rowSpan="1">
                            <textField>
                                <reportElement uuid="080e50f3-23a0-4bf2-8d6f-b63618dfdc51" isPrintRepeatedValues="false" x="0" y="0" width="232" height="124"/>
                                <box>
                                    <pen lineWidth="0.75"/>
                                    <topPen lineWidth="0.75"/>
                                    <leftPen lineWidth="0.75"/>
                                    <bottomPen lineWidth="0.75"/>
                                    <rightPen lineWidth="0.75"/>
                                </box>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{id} + ","+$F{medication_name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="232" uuid="b7bef864-31db-4b77-a929-a0d59e754b36">
                        <jr:detailCell height="124" rowSpan="1">
                            <textField>
                                <reportElement uuid="7b3c68b6-6048-4690-b8e7-14a286f94297" x="0" y="0" width="232" height="124"/>
                                <box>
                                    <pen lineWidth="0.75"/>
                                    <topPen lineWidth="0.75"/>
                                    <leftPen lineWidth="0.75"/>
                                    <bottomPen lineWidth="0.75"/>
                                    <rightPen lineWidth="0.75"/>
                                </box>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{dose_time}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

请伙计们,我需要你的帮助。

2 个答案:

答案 0 :(得分:0)

您可以使用交叉表。转到Window并单击Palette。比调色板会出现正确。最后,拖动交叉号&#39;到摘要部分并填写向导。

答案 1 :(得分:0)

这可能会有所帮助https://community.jaspersoft.com/wiki/how-merge-table-rows-repeated-values

在此解决方案中,使用了 2 个数据集,第一个数据集对数据进行分组,而第二个数据集用于创建嵌入在带区中的表。

编辑:我应用了另一种方法来做到这一点。我用一张桌子来做整件事。表格的第一列用于药物并包含一个文本字段。表的第二列用于计时。它是一个子报表,子报表又包含另一个带有时间的表。

我没有使用 SQL,而是在 Java 中创建了手动数据。这是我的java代码。


import static com.vroozi.api.requests.util.JasperUtils.compileJrxmlToJasper;

import com.vroozi.api.requests.util.JasperUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Service;

@Service
public class PDFServiceImpl {

  public String generate() throws JRException {
    final String format = DATE_TIME_FORMAT.format(new Date());
    final String outputFileName = String.format("PO_%s_%s_PDF.pdf", "rajshree", format);
    final String purchaseReportsPath = "/home/oem";
    Map<String, Object> parameters = new HashMap<>();

    Map<String, Object> cetamolMap = new HashMap<>();
    cetamolMap.put("medicine", "Cetamol");

    Map<String, Object> painKillerMap = new HashMap<>();
    painKillerMap.put("medicine", "Pain killer");

    List<Map<String, Object>> medicineMapList = Arrays.asList(cetamolMap, painKillerMap);

    Map<String, Object> time1 = new HashMap<>();
    time1.put("time", "11 a.m.");
    Map<String, Object> time2 = new HashMap<>();
    time2.put("time", "2 p.m.");

    List<Map<String, Object>> timeMapList = Arrays.asList(time1, time2);

    cetamolMap.put("timeList", new JRBeanCollectionDataSource(timeMapList));
    painKillerMap.put("timeList", new JRBeanCollectionDataSource(new ArrayList<>()));

    // Adding subreport for each medicine
    cetamolMap.put("timeTableSubReport",
        compileJrxmlToJasper("/subreport.jrxml"));
    painKillerMap.put("timeTableSubReport",
        compileJrxmlToJasper("/reports/subreport.jrxml"));

    parameters.put("medicineMapList", new JRBeanCollectionDataSource(medicineMapList));

    return JasperUtils.generatePdfReport("/reports/report.jrxml",
        parameters, purchaseReportsPath, outputFileName);
  }

}


这里是“report.jrxml”

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="catalogPOCR" pageWidth="559" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="559" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" whenResourceMissingType="Empty" uuid="42c67c01-6232-438f-8149-de04399dfcd2">
  <property name="ireport.zoom" value="1.0"/>
  <property name="ireport.x" value="0"/>
  <property name="ireport.y" value="0"/>
  <property name="ireport.scriptlethandling" value="0"/>
  <property name="ireport.encoding" value="UTF-8"/>
  <import value="net.sf.jasperreports.engine.*"/>
  <import value="org.apache.commons.collections.CollectionUtils"/>
  <import value="java.util.*"/>
  <import value="net.sf.jasperreports.engine.data.*"/>
  <style name="table 1">
    <box>
      <pen lineWidth="0.0" lineColor="#000000"/>
    </box>
  </style>
  <style name="table 1_TH" mode="Opaque" backcolor="#FFFFFF">
    <box>
      <pen lineWidth="0.0" lineColor="#000000"/>
      <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>
  </style>
  <style name="table 1_CH" mode="Opaque" backcolor="#FFFFFF">
    <box>
      <pen lineWidth="0.0" lineColor="#000000"/>
    </box>
  </style>
  <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
      <pen lineWidth="0.0" lineColor="#000000"/>
    </box>
    <conditionalStyle>
      <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
      <style backcolor="#FFFFFF"/>
    </conditionalStyle>
  </style>
  <subDataset name="Table LineItems" uuid="183918c2-b66e-4347-ae7b-e891ab0a1d88">
    <field name="medicine" class="java.lang.String"/>
    <field name="timeTableSubReport" class="net.sf.jasperreports.engine.JasperReport"/>
    <field name="timeList" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
  </subDataset>
  <subDataset name="Dataset1" uuid="183918c2-b66e-4347-ae7b-e891ab0a1d88">
    <field name="order" class="java.lang.String"/>
  </subDataset>
  <parameter name="medicineMapList" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
  <parameter name="timeTableSubReport" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false"/>
  <background>
    <band splitType="Stretch"/>
  </background>
  <title>
    <band splitType="Stretch"/>
  </title>
  <detail>
    <band height="170">
      <componentElement>
        <reportElement key="table 1" style="table 1" positionType="Float" x="0" y="0" width="504" height="70" uuid="eb4057df-dd6a-40dd-870e-baac8c8afe17">
          <property name="local_mesure_unitwidth" value="inch"/>
          <property name="com.jaspersoft.studio.unit.width" value="inch"/>
          <printWhenExpression><![CDATA[$V{REPORT_COUNT} == 1]]></printWhenExpression>
        </reportElement>
        <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="Table LineItems" uuid="8a44aec8-cdd4-4be3-b47a-620e00bb910a">
            <datasetParameter name="REPORT_DATA_SOURCE">
              <datasetParameterExpression><![CDATA[$P{medicineMapList}]]></datasetParameterExpression>
            </datasetParameter>
          </datasetRun>
          <jr:column width="70" uuid="baa7f786-d12a-4234-b9d7-faef6c9f25df">
            <jr:detailCell style="table 1_TD" height="20" rowSpan="1">
              <box>
                <pen lineWidth="0.0"/>
                <topPen lineWidth="0.0"/>
                <leftPen lineWidth="0.0"/>
                <bottomPen lineWidth="0.0"/>
                <rightPen lineWidth="0.0"/>
              </box>
              <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement x="0" y="0" width="70" height="20" uuid="5f84b7ac-c2fe-4426-b91f-6714b470a2bc"/>
                <box topPadding="5" leftPadding="2" bottomPadding="5" rightPadding="2">
                  <topPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                  <font size="8" isBold="false"/>
                  <paragraph rightIndent="4"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{medicine}]]></textFieldExpression>
              </textField>
            </jr:detailCell>
          </jr:column>
          <jr:column width="70" uuid="baa7f786-d12a-4234-b9d7-faef6c9f25df">
            <jr:detailCell style="table 1_TD" height="20" rowSpan="1">
              <box>
                <pen lineWidth="0.0"/>
                <topPen lineWidth="0.0"/>
                <leftPen lineWidth="0.0"/>
                <bottomPen lineWidth="0.0"/>
                <rightPen lineWidth="0.0"/>
              </box>
              <frame>
                <reportElement positionType="Float" isPrintRepeatedValues="false" x="0" y="0" width="50" height="20" isRemoveLineWhenBlank="true" isPrintInFirstWholeBand="true" uuid="62f0ab9e-28c4-4a58-935b-f98d26d600e3">
                  <property name="ShowOutOfBoundContent" value="false"/>
                </reportElement>
                <subreport>
                  <reportElement positionType="Float" x="0" y="0" width="50" height="20" uuid="48a7bbe4-b8ce-4a0d-a6e1-6ddd288e5602"/>
                  <parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
                  <subreportParameter name="timeList">
                    <subreportParameterExpression><![CDATA[$F{timeList}]]></subreportParameterExpression>
                  </subreportParameter>
                  <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
                  <subreportExpression><![CDATA[$F{timeTableSubReport}]]></subreportExpression>
                </subreport>
              </frame>
            </jr:detailCell>
          </jr:column>
        </jr:table>
      </componentElement>
    </band>
  </detail>
  <columnFooter>
    <band splitType="Stretch"/>
  </columnFooter>
  <pageFooter>
    <band splitType="Stretch"/>
  </pageFooter>
  <summary>
    <band splitType="Stretch"/>
  </summary>
</jasperReport>

这里是 subreport.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="timeTableSubReport" pageWidth="559" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="559" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" whenResourceMissingType="Empty" uuid="42c67c01-6232-438f-8149-de04399dfcd2">
  <property name="ireport.zoom" value="1.0"/>
  <property name="ireport.x" value="0"/>
  <property name="ireport.y" value="0"/>
  <property name="ireport.scriptlethandling" value="0"/>
  <property name="ireport.encoding" value="UTF-8"/>
  <import value="net.sf.jasperreports.engine.*"/>
  <import value="org.apache.commons.collections.CollectionUtils"/>
  <import value="java.util.*"/>
  <import value="net.sf.jasperreports.engine.data.*"/>

  <style name="table 1">
    <box>
      <pen lineWidth="0.0" lineColor="#000000"/>
    </box>
  </style>
  <style name="table 1_TH" mode="Opaque" backcolor="#FFFFFF">
    <box>
      <pen lineWidth="0.0" lineColor="#000000"/>
      <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>
  </style>
  <style name="table 1_CH" mode="Opaque" backcolor="#FFFFFF">
    <box>
      <pen lineWidth="0.0" lineColor="#000000"/>
    </box>
  </style>
  <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
      <pen lineWidth="0.0" lineColor="#000000"/>
    </box>
    <conditionalStyle>
      <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
      <style backcolor="#FFFFFF"/>
    </conditionalStyle>
  </style>
  <subDataset name="Table LineItems" uuid="183918c2-b66e-4347-ae7b-e891ab0a1d88">
    <field name="time" class="java.lang.String"/>
  </subDataset>
  <parameter name="timeList" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
  <background>
    <band splitType="Stretch"/>
  </background>
  <title>
    <band splitType="Stretch"/>
  </title>
  <detail>
    <band height="70">
      <componentElement>
        <reportElement key="table 1" style="table 1" positionType="Float" x="0" y="0" width="504" height="70" uuid="eb4057df-dd6a-40dd-870e-baac8c8afe17">
          <property name="local_mesure_unitwidth" value="inch"/>
          <property name="com.jaspersoft.studio.unit.width" value="inch"/>
          <printWhenExpression><![CDATA[$V{REPORT_COUNT} == 1]]></printWhenExpression>

        </reportElement>
        <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="Table LineItems" uuid="8a44aec8-cdd4-4be3-b47a-620e00bb910a">
            <datasetParameter name="REPORT_DATA_SOURCE">
              <datasetParameterExpression><![CDATA[$P{timeList}]]></datasetParameterExpression>
            </datasetParameter>
          </datasetRun>
          <jr:column width="70" uuid="baa7f786-d12a-4234-b9d7-faef6c9f25df">
            <jr:detailCell style="table 1_TD" height="20" rowSpan="1">
              <box>
                <pen lineWidth="0.0"/>
                <topPen lineWidth="0.0"/>
                <leftPen lineWidth="0.0"/>
                <bottomPen lineWidth="0.0"/>
                <rightPen lineWidth="0.0"/>
              </box>
              <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement x="0" y="0" width="70" height="20" uuid="5f84b7ac-c2fe-4426-b91f-6714b470a2bc"/>
                <box topPadding="5" leftPadding="2" bottomPadding="5" rightPadding="2">
                  <topPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                  <font size="8" isBold="false"/>
                  <paragraph rightIndent="4"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{time}]]></textFieldExpression>
              </textField>
            </jr:detailCell>
          </jr:column>
        </jr:table>
      </componentElement>
    </band>
  </detail>
  <columnFooter>
    <band splitType="Stretch"/>
  </columnFooter>
  <pageFooter>
    <band splitType="Stretch"/>
  </pageFooter>
  <summary>
    <band splitType="Stretch"/>
  </summary>
</jasperReport>


输出看起来像这样pdf

附言我并没有真正注意这里的造型。希望这对某人有所帮助,尽管我迟到了 7 年。