2

当我将jrml导出到csvpdf时,我会反复获取标题。

我想避免这种情况,请告诉我解决方案。

这是我的 JRML

<?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="Finance Report" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <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>
    <subDataset name="FinanceDataSet">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="additionName" class="java.lang.String">
            <fieldDescription><![CDATA[additionName]]></fieldDescription>
        </field>
        <field name="cost" class="java.lang.String">
            <fieldDescription><![CDATA[cost]]></fieldDescription>
        </field>
        <field name="projectNumber" class="java.lang.String">
            <fieldDescription><![CDATA[projectNumber]]></fieldDescription>
        </field>
        <field name="projectType" class="java.lang.String">
            <fieldDescription><![CDATA[projectType]]></fieldDescription>
        </field>
        <field name="sidNumber" class="java.lang.String">
            <fieldDescription><![CDATA[sidNumber]]></fieldDescription>
        </field>
        <field name="softCost" class="java.lang.String">
            <fieldDescription><![CDATA[softCost]]></fieldDescription>
        </field>
        <field name="yearContrbToAssests" class="java.lang.String">
            <fieldDescription><![CDATA[yearContrbToAssests]]></fieldDescription>
        </field>
    </subDataset>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="68" splitType="Stretch">
            <staticText>
                <reportElement x="305" y="30" width="183" height="35"/>
                <textElement textAlignment="Center">
                    <font size="24" isBold="true"/>
                </textElement>
                <text><![CDATA[Finance Report]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="26" splitType="Stretch"/>
    </pageHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <componentElement>
                <reportElement key="table" style="table" x="60" y="0" width="267" height="85"/>
                <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="FinanceDataSet">
                        <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="90">
                        <jr:tableHeader style="table_TH" height="1" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement key="col_header" x="0" y="0" width="90" height="30"/>
                                <textElement verticalAlignment="Middle"/>
                                <text><![CDATA[Project Number]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="21" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="21"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{projectNumber}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90">
                        <jr:tableHeader style="table_TH" height="1" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement key="col_header" x="0" y="0" width="90" height="30"/>
                                <textElement verticalAlignment="Middle"/>
                                <text><![CDATA[Project Type]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="21" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{projectType}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90">
                        <jr:tableHeader style="table_TH" height="1" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement key="col_header" x="0" y="0" width="90" height="30"/>
                                <textElement verticalAlignment="Middle"/>
                                <text><![CDATA[Cost]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="21" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{cost}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90">
                        <jr:tableHeader style="table_TH" height="1" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement key="col_header" x="0" y="0" width="90" height="30"/>
                                <textElement verticalAlignment="Middle"/>
                                <text><![CDATA[Soft Cost]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="21" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{softCost}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90">
                        <jr:tableHeader style="table_TH" height="1" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement key="col_header" x="0" y="0" width="90" height="30"/>
                                <textElement verticalAlignment="Middle"/>
                                <text><![CDATA[Sid Number]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="21" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{sidNumber}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="143">
                        <jr:tableHeader style="table_TH" height="1" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement key="col_header" x="0" y="0" width="143" height="30"/>
                                <textElement verticalAlignment="Middle"/>
                                <text><![CDATA[Addition Name]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="21" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="143" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{additionName}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="113">
                        <jr:tableHeader style="table_TH" height="1" rowSpan="1"/>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement key="col_header" x="0" y="0" width="110" height="30"/>
                                <textElement verticalAlignment="Middle"/>
                                <text><![CDATA[YearContrbToAssests]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="21" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="113" height="21"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{yearContrbToAssests}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

对于CSV导出,控制器是:

 outputStream = response.getOutputStream();
            jasperReport = (JasperReport) JRLoader.loadObject(sourceFile);
            jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(),jrDataSource);
            jasperPrint.getPropertiesMap().setProperty("net.sf.jasperreports.export.csv.exclude.origin.band.pageFooter", "pageFooter");
            jasperPrint.getPropertiesMap().setProperty("net.sf.jasperreports.export.csv.exclude.origin.band.title", "title");
            JRCsvExporter exporter = new JRCsvExporter();
            exporter.setParameter(JRCsvExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRCsvExporterParameter.OUTPUT_STREAM, outputStream);
            exporter.exportReport();

对于PDF导出:

 sourceFile = new File(request.getSession().getServletContext().getRealPath("WEB-INF/classes/Finance Report.jasper"));
                response.setHeader("Content-Disposition", "attachment;filename=FinanceReport.pdf");
                financeReportBeanList = (List<FinanceReportBean>) request.getSession().getAttribute("financeReportList");
                financeReportBeanList.get(0).setCurrentDate(DateUtil.getCurrentDate());
                jrDataSource = new JRBeanCollectionDataSource(financeReportBeanList);


    JasperReport jasperReport=null;
            JasperPrint jasperPrint=null;
            OutputStream outputStream=null;
            outputStream = response.getOutputStream();
            jasperReport = (JasperReport) JRLoader.loadObject(sourceFile);
            jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(),jrDataSource);
            JRPdfExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
            JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
            exporter.exportReport();
4

1 回答 1

3

如图所示,在您的 jrxml 中添加以下两个属性,这将排除pageHeader和格式化。csvpdf

<property name="net.sf.jasperreports.export.csv.exclude.origin.keep.first.band.1" value="pageHeader"/>
<property name="net.sf.jasperreports.export.pdf.exclude.origin.keep.first.band.2" value="pageHeader"/>

如下:

<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="Finance Report" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
--> <property name="net.sf.jasperreports.export.csv.exclude.origin.keep.first.band.1" value="pageHeader"/>
--> <property name="net.sf.jasperreports.export.pdf.exclude.origin.keep.first.band.2" value="pageHeader"/>
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>

编辑:

添加key到您columnHeader的如下所示:

<jr:columnHeader style="table_CH" height="30" rowSpan="1" key="col_header">

现在在下面再添加一个property来隐藏它。

<property name="net.sf.jasperreports.export.csv.exclude.key.col_header"/>

于 2013-08-30T10:27:28.503 回答