【问题标题】:filling the table component of jasper reports using java result set使用java结果集填充jasper报告的表格组件
【发布时间】:2015-11-23 16:55:32
【问题描述】:

我的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="fancy" language="groovy" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="09087f48-655f-47b7-ba5e-c4e4e1c2b7ad">
            <property name="ireport.zoom" value="1.0"/>
            <property name="ireport.x" value="27"/>
            <property name="ireport.y" value="86"/>
            <style name="Title" fontName="Times New Roman" fontSize="50" isBold="true" pdfFontName="Times-Bold"/>
            <style name="SubTitle" forecolor="#736343" fontName="Arial" fontSize="18"/>
            <style name="Column header" forecolor="#666666" fontName="Arial" fontSize="12" isBold="true"/>
            <style name="Detail" fontName="Arial" fontSize="12"/>
            <style name="Row" mode="Transparent">
                <conditionalStyle>
                    <conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
                    <style backcolor="#E6DAC3"/>
                </conditionalStyle>
            </style>
            <style name="table">
                <box>
                    <pen lineWidth="1.0" lineColor="#000000"/>
                </box>
            </style>
            <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
                <box>
                    <pen lineWidth="0.5" lineColor="#000000"/>
                </box>
            </style>
            <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
                <box>
                    <pen lineWidth="0.5" lineColor="#000000"/>
                </box>
            </style>
            <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
                <box>
                    <pen lineWidth="0.5" lineColor="#000000"/>
                </box>
            </style>
            <style name="table 1">
                <box>
                    <pen lineWidth="1.0" lineColor="#000000"/>
                </box>
            </style>
            <style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">
                <box>
                    <pen lineWidth="0.5" lineColor="#000000"/>
                </box>
            </style>
            <style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">
                <box>
                    <pen lineWidth="0.5" lineColor="#000000"/>
                </box>
            </style>
            <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
                <box>
                    <pen lineWidth="0.5" lineColor="#000000"/>
                </box>
            </style>
            <style name="table 2">
                <box>
                    <pen lineWidth="1.0" lineColor="#000000"/>
                </box>
            </style>
            <style name="table 2_TH" mode="Opaque" backcolor="#F0F8FF">
                <box>
                    <pen lineWidth="0.5" lineColor="#000000"/>
                </box>
            </style>
            <style name="table 2_CH" mode="Opaque" backcolor="#BFE1FF">
                <box>
                    <pen lineWidth="0.5" lineColor="#000000"/>
                </box>
            </style>
            <style name="table 2_TD" mode="Opaque" backcolor="#FFFFFF">
                <box>
                    <pen lineWidth="0.5" lineColor="#000000"/>
                </box>
            </style>
            <subDataset name="New Dataset 1" uuid="aa2fab2d-72e6-47b4-bc09-7c11fe93d3b1">
                <queryString language="SQL">
                    <![CDATA[select * from qol_users]]>
                </queryString>
                <field name="user_id" class="java.lang.Integer">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="Date_format" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="email_id" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="enable" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="expiry_date" class="java.sql.Timestamp">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="first_name" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="last_name" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="password" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="phone_number" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="roles" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="time_zone" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="updated_by" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="updated_date" class="java.sql.Timestamp">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="uploaded_by" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="uploaded_date" class="java.sql.Date">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="user_name" class="java.lang.String">
                    <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <group name="roles">
                    <groupExpression><![CDATA[$F{roles}]]></groupExpression>
                </group>
                <group name="updated_date">
                    <groupExpression><![CDATA[$F{updated_date}]]></groupExpression>
                </group>
            </subDataset>

            <field name="user_id" class="java.lang.Integer">
                <fieldDescription><![CDATA[]]></fieldDescription>
            </field>
            <field name="first_name" class="java.lang.String">
                <fieldDescription><![CDATA[]]></fieldDescription>
            </field>
            <field name="last_name" class="java.lang.String">
                <fieldDescription><![CDATA[]]></fieldDescription>
            </field>
            <field name="email_id" class="java.lang.String">
                <fieldDescription><![CDATA[]]></fieldDescription>
            </field>
            <background>
                <band splitType="Stretch"/>
            </background>
            <title>
                <band height="136" splitType="Stretch">
                    <image>
                        <reportElement x="138" y="47" width="247" height="63" uuid="dcc00d69-2974-4c7b-a56a-bfa75c7bfec2"/>
                        <imageExpression><![CDATA["/home/bvsreddy/Desktop/qolsys_logo.png"]]></imageExpression>
                    </image>
                </band>
            </title>
            <pageHeader>
                <band height="50">
                    <staticText>
                        <reportElement x="182" y="0" width="146" height="36" uuid="870eb702-9d4b-432a-85e7-e46c77ef0896"/>
                        <textElement textAlignment="Center">
                            <font size="16" isBold="true"/>
                        </textElement>
                        <text><![CDATA[USER DETAILS]]></text>
                    </staticText>
                </band>
            </pageHeader>
            <detail>
                <band height="268" splitType="Stretch">
                    <componentElement>
                        <reportElement key="table 2" style="table 2" x="88" y="0" width="360" height="153" uuid="b87156b4-37dd-4d85-9c70-d56b8626cb3d"/>
                        <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="New Dataset 1" uuid="5bb1a22f-d699-46e1-8823-3e7ef9d73da0">
                                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                            </datasetRun>
                            <jr:column width="90" uuid="fd96d212-138d-4897-856d-4e4e5ff49ffa">
                                <jr:columnHeader style="table 2_CH" height="30" rowSpan="1">
                                    <staticText>
                                        <reportElement x="0" y="0" width="90" height="30" uuid="98f3f318-bfca-4ce3-8bbf-c23df16a590e"/>
                                        <textElement textAlignment="Center"/>
                                        <text><![CDATA[User Id]]></text>
                                    </staticText>
                                </jr:columnHeader>
                                <jr:detailCell style="table 2_TD" height="20" rowSpan="1">
                                    <textField>
                                        <reportElement x="0" y="0" width="90" height="20" uuid="7b49c4f7-982a-4cfd-a099-cc87619b0b74"/>
                                        <textElement textAlignment="Center"/>
                                        <textFieldExpression><![CDATA[$F{user_id}]]></textFieldExpression>
                                    </textField>
                                </jr:detailCell>
                            </jr:column>
                            <jr:column width="90" uuid="d68226dc-73d7-4bbb-8c7a-372b7cc924e5">
                                <jr:columnHeader style="table 2_CH" height="30" rowSpan="1">
                                    <staticText>
                                        <reportElement x="0" y="0" width="90" height="30" uuid="31a8a8f1-2d66-40c9-88b5-bd994084fc6f"/>
                                        <textElement textAlignment="Center"/>
                                        <text><![CDATA[First Name]]></text>
                                    </staticText>
                                </jr:columnHeader>
                                <jr:detailCell style="table 2_TD" height="20" rowSpan="1">
                                    <textField>
                                        <reportElement x="0" y="0" width="90" height="20" uuid="e9758f4a-9a9b-45d0-8efd-cd069d131978"/>
                                        <textElement textAlignment="Center"/>
                                        <textFieldExpression><![CDATA[$F{first_name}]]></textFieldExpression>
                                    </textField>
                                </jr:detailCell>
                            </jr:column>
                            <jr:column width="90" uuid="b0e486a4-9df4-4db2-93a8-26d9a9e89df5">
                                <jr:columnHeader style="table 2_CH" height="30" rowSpan="1">
                                    <staticText>
                                        <reportElement x="0" y="0" width="90" height="30" uuid="e72a1b56-339c-4de8-b9e8-59d7006c9ac7"/>
                                        <textElement textAlignment="Center"/>
                                        <text><![CDATA[Last Name]]></text>
                                    </staticText>
                                </jr:columnHeader>
                                <jr:detailCell style="table 2_TD" height="20" rowSpan="1">
                                    <textField>
                                        <reportElement x="0" y="0" width="90" height="20" uuid="04d87c2c-ff0a-42cf-9fa4-2abbe35d9545"/>
                                        <textElement textAlignment="Center"/>
                                        <textFieldExpression><![CDATA[$F{last_name}]]></textFieldExpression>
                                    </textField>
                                </jr:detailCell>
                            </jr:column>
                            <jr:column width="90" uuid="d96b6156-007d-4ae4-b0b0-39260f24b69a">
                                <jr:columnHeader style="table 2_CH" height="30" rowSpan="1">
                                    <staticText>
                                        <reportElement x="0" y="0" width="90" height="30" uuid="d4a189b3-d58c-4f27-8638-3653f0d40ffa"/>
                                        <textElement textAlignment="Center"/>
                                        <text><![CDATA[Email Id]]></text>
                                    </staticText>
                                </jr:columnHeader>
                                <jr:detailCell style="table 2_TD" height="20" rowSpan="1">
                                    <textField>
                                        <reportElement x="0" y="0" width="90" height="20" uuid="ba369562-9505-453a-a046-2da72a2b1658"/>
                                        <textElement textAlignment="Center"/>
                                        <textFieldExpression><![CDATA[$F{email_id}]]></textFieldExpression>
                                    </textField>
                                </jr:detailCell>
                            </jr:column>
                        </jr:table>
                    </componentElement>
                </band>
            </detail>
            <columnFooter>
                <band height="26" splitType="Stretch">
                    <line>
                        <reportElement positionType="FixRelativeToBottom" x="0" y="3" width="555" height="1" uuid="30c3c673-20c8-4ab4-a9f7-88e6a5ad473d"/>
                        <graphicElement>
                            <pen lineWidth="0.5" lineColor="#999999"/>
                        </graphicElement>
                    </line>
                </band>
            </columnFooter>
            <pageFooter>
                <band height="25" splitType="Stretch">
                    <frame>
                        <reportElement mode="Opaque" x="-21" y="1" width="597" height="24" forecolor="#D0B48E" backcolor="#F2EBDF" uuid="04480c15-33dd-4f95-9544-6b1c27980b06"/>
                        <textField evaluationTime="Report">
                            <reportElement style="Column header" x="533" y="0" width="40" height="20" forecolor="#736343" uuid="81ddfb0c-8809-4160-99d9-36f05049d373"/>
                            <textElement verticalAlignment="Middle">
                                <font size="10" isBold="false"/>
                            </textElement>
                            <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement style="Column header" x="453" y="0" width="80" height="20" forecolor="#736343" uuid="f69661cc-9569-42bc-8e24-029539753602"/>
                            <textElement textAlignment="Right" verticalAlignment="Middle">
                                <font size="10" isBold="false"/>
                            </textElement>
                            <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
                        </textField>
                        <textField pattern="EEEEE dd MMMMM yyyy">
                            <reportElement style="Column header" x="22" y="1" width="197" height="20" forecolor="#736343" uuid="09521691-6218-4f06-818c-3d823163e384"/>
                            <textElement verticalAlignment="Middle">
                                <font size="10" isBold="false"/>
                            </textElement>
                            <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
                        </textField>
                    </frame>
                </band>
            </pageFooter>
            <summary>
                <band splitType="Stretch"/>
            </summary>
        </jasperReport>'

我的 java 文件如下,任何快速帮助表示赞赏....谢谢

    import java.io.File;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import net.sf.jasperreports.engine.JRExporter;
    import net.sf.jasperreports.engine.JRExporterParameter;
    import net.sf.jasperreports.engine.JRResultSetDataSource;
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
    import net.sf.jasperreports.engine.export.JRPdfExporter;

    public class Fancy
    {
      public static void main(String[] args) throws SQLException
      {
        Connection connection = null;
        ResultSet resultSet = null;
        Statement statement = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/mydb?user=root&password=root";
          String query = " select user_id, first_name, last_name, email_id from users";
          connection = DriverManager.getConnection(url);
          statement = connection.createStatement();

          resultSet = statement.executeQuery(query);

          File reportFile = new File("/home/bvsreddy/Desktop/fancy.jasper");
          Map hm = new HashMap();
          String outFileName = "/home/bvsreddy/Desktop/fancy.pdf";
          List<CustomUser> users = new ArrayList<>();
          while(resultSet.next()){
              CustomUser customUser = new CustomUser();
              customUser.setUser_id(resultSet.getInt(1));
              customUser.setFirst_name(resultSet.getString(2));
              customUser.setLast_name(resultSet.getString(3));
              customUser.setEmail_id(resultSet.getString(4));
              users.add(customUser);
          }
          System.out.println(users.size());
          JasperPrint print = JasperFillManager.fillReport(reportFile.getPath(), hm, new JRBeanCollectionDataSource(users,false));

          // Create a PDF exporter
          JRExporter exporter = new JRPdfExporter();

          // Configure the exporter (set output file name and print object)
          exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
          exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);

          // Export the PDF file
          exporter.exportReport();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
        finally
        {
          //boilerplate code to close the result set, statement, and exception
        }
      }
    }

我试过了,但是我得到了空桌子

【问题讨论】:

  • 非常感谢任何快速帮助....
  • 你检查了用户列表吗?也许它是空的......
  • 不,我们有用户列表

标签: java jasper-reports


【解决方案1】:

表格组件必须放置在摘要区域中(有时在标题区域中)。不详!

并且在Table Dataset 1中查询(在table中作为dataSource使用)不对应ArrayList(dataSource for report)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多