如何从ArrayList填充报告?

时间:2016-03-31 02:44:44

标签: java arraylist jasper-reports

我正在尝试从ArrayList填充我的jasper报告,所以我为此创建了String参数。然后我有这个方法:

public void formatPurchaseOrder(String orderNumber, String reportdir) {
        ArrayList all = PurchaseOrders.listPurchaseOrderDetails(orderNumber);
        int size = all.size();
        ClassLoader classLoader = getClass().getClassLoader();
        JREmptyDataSource datasource = new JREmptyDataSource();
        Map parameters  = new HashMap();
        for (int i = 0; i < size; i++) {
            ArrayList one = (ArrayList) all.get(i);  
            parameters.put("PRODUCT_NAME", (String) one.get(2));
            parameters.put("PRODUCT_UNITS", (String) one.get(5));
            parameters.put("UNIT_COST", (String) one.get(4));
            parameters.put("TOTAL_COST", (String) one.get(6));
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd_MMM_YYYY");
            InputStream url = classLoader.getResourceAsStream("com/orig/stock/jrxml/porder.jrxml");
            JasperReport jasperReport = JasperCompileManager.compileReport(url);
            reportDestination = reportdir + "/Statement_" + orderNumber + "_" + sdf.format(new Date()) + ".pdf";
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, datasource);
            JasperExportManager.exportReportToPdfFile(jasperPrint, reportDestination);
        } catch (Exception asd) {
            System.out.println(asd.getMessage());
        }
    }

当数组有多条记录时,我的报告只有一行数据。我可以覆盖其他价值观,或者我做错了什么?

我的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="porder" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="05f8fa07-cc11-4951-91d0-927112a15a81">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="PRODUCT_NAME" class="java.lang.String"/>
    <parameter name="PRODUCT_UNITS" class="java.lang.String"/>
    <parameter name="UNIT_COST" class="java.lang.String"/>
    <parameter name="TOTAL_COST" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="8" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch">
            <staticText>
                <reportElement x="177" y="15" width="100" height="20" uuid="41147e24-6284-475c-b00c-96e980c79bbc"/>
                <text><![CDATA[PURCHASE ORDER]]></text>
            </staticText>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="15" y="41" width="100" height="20" uuid="9dfd8b36-bc9b-49de-b9a9-b6d769d412d6"/>
                <text><![CDATA[PRODUCT NAME]]></text>
            </staticText>
            <staticText>
                <reportElement x="136" y="41" width="100" height="20" uuid="0eb87f16-3251-4736-9c7a-8ec69652da8c"/>
                <text><![CDATA[PRODUCT UNITS]]></text>
            </staticText>
            <staticText>
                <reportElement x="242" y="41" width="100" height="20" uuid="8a5f0b4c-b468-4791-9a5c-7b99431bef00"/>
                <text><![CDATA[UNIT COST]]></text>
            </staticText>
            <staticText>
                <reportElement x="353" y="41" width="100" height="20" uuid="7aadd922-f446-4e8b-b16b-fa61545936d6"/>
                <text><![CDATA[TOTAL COST]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="23" splitType="Stretch">
            <textField>
                <reportElement x="14" y="3" width="112" height="20" uuid="faddcb7e-d047-4a35-9296-dd8a7d384e57"/>
                <textFieldExpression><![CDATA[$P{PRODUCT_NAME}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="138" y="3" width="100" height="20" uuid="ee974e76-1698-4d41-95ca-e3254e6d8537"/>
                <textFieldExpression><![CDATA[$P{PRODUCT_UNITS}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="243" y="3" width="100" height="20" uuid="7de5517f-5baa-489b-a4ad-b152df7523fb"/>
                <textFieldExpression><![CDATA[$P{UNIT_COST}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="355" y="3" width="100" height="20" uuid="02a9f9a9-a81b-4f71-9e38-85b0ebdd0937"/>
                <textFieldExpression><![CDATA[$P{TOTAL_COST}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

1 个答案:

答案 0 :(得分:2)

您对parameters以及datasource是什么感到困惑。

你的循环

for (int i = 0; i < size; i++) {
   ArrayList one = (ArrayList) all.get(i); 
   parameters.put("PRODUCT_NAME", (String) one.get(2));
}

没有任何意义,你将参数PRODUCT_NAME设置为最后一个值(它是一个Map<String,Object>,在一个键上你只能有一个值)而且你试图通过时不应该使用它数据源,但仅用于传递参数(用于查询,用于图像路径ecc。)

要从List<List<String>>生成数据源,您可以使用ListOfArrayDataSource,但需要将其转换为List<Object[]>。然而,我会将其转换为List<MyBean>并使用JRBeanCollectionDataSourceMyBean类类似于

public class MyBean {
    private String productName; 
    ... all other fields
    private double totalCost;
    ... getters and setters
}

然后在你的jrxml中将这些定义为 field 而不是参数

<field name="productName" class="java.lang.String"/>
<field name="totalCost" class="java.lang.Double"/>

因此,数据源将在您的详细信息频段而不是参数中进行迭代。