本文介绍了如何填补一个ArrayList报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从的ArrayList 填写我的碧玉报告,所以我创建的字符串参数这一点。然后,我有这样的方法:

 公共无效formatPurchaseOrder(字符串订单编号,字符串reportdir){
        ArrayList的所有= PurchaseOrders.listPurchaseOrderDetails(订单编号);
        INT大小= all.size();
        的ClassLoader类加载器=的getClass()getClassLoader()。
        JREmptyDataSource数据源=新JREmptyDataSource();
        地图参数=新的HashMap();
        的for(int i = 0; I<大小;我++){
            ArrayList的1 =(ArrayList的)all.get(I)
            parameters.put(PRODUCT_NAME(字符串)one.get(2));
            parameters.put(PRODUCT_UNITS(字符串)one.get(5));
            parameters.put(UNIT_COST(字符串)one.get(4));
            parameters.put(TOTAL_COST(字符串)one.get(6));
        }
        尝试{
            SimpleDateFormat的SDF =新的SimpleDateFormat(dd_MMM_YYYY);
            InputStream的URL = classLoader.getResourceAsStream(COM /原稿/股票/ JRXML / porder.jrxml);
            JasperReport的JasperReport的= JasperCompileManager.compileReport(URL);
            reportDestination = reportdir +/ Statement_+订单编号+_+ sdf.format(新的Date())+.PDF
            JasperPrint jasperPrint = JasperFillManager.fillReport(JasperReport的,参数,数据源);
            JasperExportManager.exportReportToPdfFile(jasperPrint,reportDestination);
        }赶上(例外ASD){
            的System.out.println(asd.getMessage());
        }
    }

我的报告仅具有数据的一行时,阵列具有多个记录。难道我要覆盖其它值还是什么我做错了?

我的JRXML:

<?XML版本=1.0编码=UTF-8&GT?;
<的JasperReport的xmlns =htt​​p://jasperreports.sourceforge.net/jasperreports的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instanceXSI:=的schemaLocationHTTP:// JasperReports的。 sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsdNAME =porderLANGUAGE =常规页宽=595pageHeight =842columnWidth时=555LEFTMARGIN =20 rightMargin =20TOPMARGIN =20bottomMargin =20UUID =05f8fa07-cc11-4951-91d0-927112a15a81>
    <属性名=ireport.zoom值=1.0/>
    <属性名=ireport.x值=0/>
    <属性名=ireport.y值=0/>
    <参数名称=PRODUCT_NAME级=java.lang.String中/>
    <参数名称=PRODUCT_UNITS级=java.lang.String中/>
    <参数名称=UNIT_COST级=java.lang.String中/>
    <参数名称=TOTAL_COST级=java.lang.String中/>
    <背景与GT;
        <乐队的splittype =拉伸/>
    < /背景>
    <标题>
        <带高度=8的s​​plittype =拉伸/>
    < /标题>
    < pageHeader>
        <带高度=35的splittype =拉伸>
            <&STATICTEXT GT;
                < reportElement X =177Y =15WIDTH =100HEIGHT =20UUID =41147e24-6284-475c-b00c-96e980c79bbc/>
                <文本><![CDATA [采购订单]>< /文字和GT;
            < / STATICTEXT>
        < /乐队>
    < / pageHeader>
    <&的columnHeader GT;
        <带高度=61的splittype =拉伸>
            <&STATICTEXT GT;
                < reportElement X =15Y =41WIDTH =100HEIGHT =20UUID =9dfd8b36-bc9b-49de-b9a9-b6d769d412d6/>
                <文本><![CDATA [产品名称]]>< /文字和GT;
            < / STATICTEXT>
            <&STATICTEXT GT;
                < reportElement X =136Y =41WIDTH =100HEIGHT =20UUID =0eb87f16-3251-4736-9c7a-8ec69652da8c/>
                <文本><![CDATA [产品单元]>< /文字和GT;
            < / STATICTEXT>
            <&STATICTEXT GT;
                < reportElement X =242Y =41WIDTH =100HEIGHT =20UUID =8a5f0b4c-b468-4791-9a5c-7b99431bef00/>
                <文本><![CDATA [单位成本]>< /文字和GT;
            < / STATICTEXT>
            <&STATICTEXT GT;
                < reportElement X =353Y =41WIDTH =100HEIGHT =20UUID =7aadd922-f446-4e8b-B16B-fa61545936d6/>
                <文本><![CDATA [总成本]>< /文字和GT;
            < / STATICTEXT>
        < /乐队>
    < /&的columnHeader GT;
    <详情>
        <带高度=23的splittype =拉伸>
            <&文本框GT;
                &所述; reportElement X =14Y =3宽度=112HEIGHT =20的uuid =faddcb7e-d047-4a35-9296-dd8a7d384e57/>
                < textFieldEx pression><![CDATA [$ P {PRODUCT_NAME}]>< / textFieldEx pression>
            < /文本框>
            <&文本框GT;
                < reportElement X =138Y =3WIDTH =100HEIGHT =20UUID =ee974e76-1698-4d41-95ca-e3254e6d8537/>
                < textFieldEx pression><![CDATA [$ P {} PRODUCT_UNITS]>< / textFieldEx pression>
            < /文本框>
            <&文本框GT;
                < reportElement X =243Y =3WIDTH =100HEIGHT =20UUID =7de5517f-5baa-489b-a4ad-b152df7523fb/>
                < textFieldEx pression><![CDATA [$ P {} UNIT_COST]>< / textFieldEx pression>
            < /文本框>
            <&文本框GT;
                < reportElement X =355Y =3WIDTH =100HEIGHT =20UUID =02a9f9a9-a81b-4f71-9e38-85b0ebdd0937/>
                < textFieldEx pression><![CDATA [$ P {} TOTAL_COST]>< / textFieldEx pression>
            < /文本框>
        < /乐队>
    < /详情>
    < columnFooter>
        <带高度=45的splittype =拉伸/>
    < / columnFooter>
    < pageFooter>
        <带高度=54的splittype =拉伸/>
    < / pageFooter>
    <总结>
        <带高度=42的splittype =拉伸/>
    < /总结>
< / JasperReport的>


解决方案

您有关于什么是参数什么有些困惑的是数据源

您回路

 的for(int i = 0; I<大小;我++){
   ArrayList的1 =(ArrayList的)all.get(I)
   parameters.put(PRODUCT_NAME(字符串)one.get(2));
}

已经没有意义,您将设置参数 PRODUCT_NAME 来最后的值(它的一个地图<弦乐,对象> ,在一个键就只能有一个值),此外当试图通过一个数据源,但只传递参数(查询,为路径图像ECC你不应该用这个。)

为了从您列表℃的数据源,列表与LT;弦乐>> 您可以使用,但你需要将其转换为列表与LT;对象[]> 。然而,我将它转换为列表与LT; MyBean> 和使用 JRBeanCollectionDataSource 。在 MyBean 类是类似于

 公共类MyBean {
    私人字符串产品名称;
    ...所有其他领域
    私人双TOTALCOST;
    ... getter和setter
}

,然后在JRXML定义这些为字段的S代替参数

 <字段名称=产品名称级=java.lang.String中/>
<字段名=TOTALCOST级=java.lang.Double中的/>

因此​​,它会在你的细节带迭代没有参数的数据源

I am attempting to fill my jasper report from an ArrayList, so I have created String parameters for this. Then I have this method:

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());
        }
    }

My report is only having one row of data when the array has several records. Could I be overwriting the other values or what am I doing wrong?

My 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>
解决方案

You have some confusion about what is the parameters and what is the datasource.

Your loop

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

has no sense, you will set the parameter PRODUCT_NAME to last value (its a Map<String,Object>, on one key you can only have one value) and furthermore you should not use this when trying to pass a datasource, but only to pass parameters (for query, for path to images ecc.)

To generate a datasource from you List<List<String>> you can use the ListOfArrayDataSource, but you need to convert it to a List<Object[]>. I however would have converted it to a List<MyBean> and used the JRBeanCollectionDataSource. The MyBean class is similar to

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

and then define in your jrxml these as fields instead of parameter

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

Hence its the datasource that will be iterated in your detail band not the parameters.

这篇关于如何填补一个ArrayList报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:06