java - 无法删除重复的列标题和页脚

标签 java jasper-reports

我需要以三种不同的格式为我的 Web 应用程序创建一些报告:PDF、Excel 和 MSWord。我能够正确生成 pdf 和 .doc 格式的报告。但是,当我将同一个报表转换为 Excel 时,列标题和页脚会多次填充在同一个 Excel 工作表中。

我认为在 jrxml 文件中添加以下代码行可以解决我的问题,

<property name="net.sf.jasperreports.export.xls.exclude.origin.band.1"  alue="columnHeader"/>
<property name="net.sf.jasperreports.export.xls.exclude.origin.band.2" value="pageFooter"/>

但我仍然收到带有多个列标题的 Excel 格式的报告。如何删除这个?

我的完整 Jrxml 文件是

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.2.final using JasperReports Library version 6.2.2  -->
<!-- 2016-06-02T10:05:45 -->
<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="HelthReport" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3135d1b0-6264-49a2-ada6-e9fff1d6ed86">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<property name="net.sf.jasperreports.export.xls.exclude.origin.band.1" value="columnHeader"/>
<property name="net.sf.jasperreports.export.xls.exclude.origin.band.2" value="pageFooter"/>
<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="#D4E8FC"/>
    </conditionalStyle>
</style>
<queryString>
    <![CDATA[]]>
</queryString>
<field name="health" class="java.lang.String">
    <fieldDescription><![CDATA[health]]></fieldDescription>
</field>
<field name="groupColor" class="java.lang.String">
    <fieldDescription><![CDATA[groupColor]]></fieldDescription>
</field>
<field name="ip" class="java.lang.String">
    <fieldDescription><![CDATA[ip]]></fieldDescription>
</field>
<field name="rtc" class="java.lang.String">
    <fieldDescription><![CDATA[rtc]]></fieldDescription>
</field>
<field name="siteId" class="java.lang.Integer">
    <fieldDescription><![CDATA[siteId]]></fieldDescription>
</field>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="43" splitType="Stretch">
        <frame>
            <reportElement mode="Opaque" x="1" y="0" width="801" height="43" backcolor="#127196" uuid="ac378a6a-2972-4cb3-bffb-4471fe268224"/>
        </frame>
        <staticText>
            <reportElement style="Title" x="302" y="0" width="226" height="39" forecolor="#FFFFFF" uuid="3a1f4ac6-b143-4ad0-ad6f-074db8b81aef"/>
            <textElement verticalAlignment="Middle">
                <font size="24" pdfFontName="Times-Roman"/>
            </textElement>
            <text><![CDATA[Health Status Report]]></text>
        </staticText>
    </band>
</title>
<pageHeader>
    <band splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="24" splitType="Stretch">
        <frame>
            <reportElement mode="Opaque" x="1" y="0" width="801" height="24" backcolor="#8CC1ED" uuid="ee52a0d7-20d0-4b48-85b3-34df95379e13"/>
        </frame>
        <staticText>
            <reportElement x="50" y="4" width="130" height="20" uuid="9c58e2de-8331-4a2c-933d-e8ba4d5d0eb5"/>
            <textElement textAlignment="Center">
                <font fontName="Arial" size="12" isBold="true"/>
            </textElement>
            <text><![CDATA[RTC]]></text>
        </staticText>
        <staticText>
            <reportElement x="49" y="4" width="100" height="20" uuid="16f37b57-7817-4e17-a923-ba804b94cc80"/>
            <textElement textAlignment="Center">
                <font fontName="Arial" size="12"/>
            </textElement>
            <text><![CDATA[Site ID]]></text>
        </staticText>
        <staticText>
            <reportElement x="478" y="4" width="134" height="20" uuid="6ce29c18-fb1a-4360-bb9d-59cd26964b4b"/>
            <textElement textAlignment="Center">
                <font fontName="Arial" size="12" isBold="true"/>
            </textElement>
            <text><![CDATA[Group Lamp Colour]]></text>
        </staticText>
        <staticText>
            <reportElement x="336" y="4" width="102" height="20" uuid="edbfd0a1-56d6-4536-8c07-f482ff7b1dee"/>
            <textElement textAlignment="Center">
                <font fontName="Arial" size="12" isBold="true"/>
            </textElement>
            <text><![CDATA[Health]]></text>
        </staticText>
        <staticText>
            <reportElement x="666" y="4" width="100" height="20" uuid="86d6feb5-371a-4f4e-b6e6-498b7bf29c96"/>
            <textElement textAlignment="Center">
                <font fontName="Arial" size="12" isBold="true"/>
            </textElement>
            <text><![CDATA[IP]]></text>
        </staticText>
    </band>
</columnHeader>
<detail>
    <band height="21" splitType="Stretch">
        <frame>
            <reportElement style="Row" mode="Opaque" x="0" y="0" width="801" height="21" uuid="5265c0b1-ffe5-40dc-b6a2-0972c41a6bc8"/>
            <textField>
                <reportElement x="478" y="0" width="134" height="21" uuid="3adc89e7-c253-4570-87d6-3cd21d8897b0"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{groupColor}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="50" y="0" width="130" height="21" uuid="622912bf-b67f-449c-88f2-4901e42d9a5e"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{rtc}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="49" y="0" width="100" height="21" uuid="80c4e081-c861-4dd8-b6ae-5bd3c58ba39e"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{siteId}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="330" y="0" width="120" height="21" uuid="0173c072-b617-41af-9b53-d4062287925c"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{health}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="666" y="0" width="100" height="21" uuid="8e94a19a-5894-4bb3-b6f0-7cc3a140264f"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{ip}]]></textFieldExpression>
            </textField>
        </frame>
    </band>
</detail>
<columnFooter>
    <band splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="22" splitType="Stretch">
        <frame>
            <reportElement mode="Opaque" x="0" y="1" width="802" height="21" forecolor="#000000" backcolor="#B3D9FC" uuid="12131ca3-805b-479a-91b1-fbb2c6b58127"/>
            <textField evaluationTime="Report">
                <reportElement style="Column header" x="742" y="1" width="40" height="20" forecolor="#000000" uuid="9bd68a2c-6c46-4948-89a9-c726fd2b1936"/>
                <textElement verticalAlignment="Middle">
                    <font size="10" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="Column header" x="662" y="1" width="80" height="20" forecolor="#000000" uuid="ce68a3c3-8b34-466e-abd6-70ddf2200db0"/>
                <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="#000000" uuid="1a66d15c-30d0-40d3-9b8c-d03328944f03"/>
                <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>

我生成报告的java代码是

public byte[] generateReportXls (HttpServletRequest request, Map<String, Object> parameters, String fileName, 
        JRBeanCollectionDataSource beanColDataSource)throws JRException, NamingException,  IOException {
    byte[] xlsReport = null;
    JasperReport jasperReport = getCompiledFile(fileName, request); 
    if(jasperReport==null){
        return null;
    }
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters, beanColDataSource);
    if (jasperPrint != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JRXlsExporter exporter = new JRXlsExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
        SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
        configuration.setOnePagePerSheet(false);
        configuration.setDetectCellType(true);
        configuration.setCollapseRowSpan(false);
        configuration.setWhitePageBackground(false);
        configuration.setIgnoreCellBackground(true);
        configuration.setRemoveEmptySpaceBetweenColumns(true);
        configuration.setRemoveEmptySpaceBetweenRows(true);

        exporter.setConfiguration(configuration); 
        exporter.exportReport();
        xlsReport = baos.toByteArray();         
    }
    return xlsReport;
}

最佳答案

@RequestMapping(method = RequestMethod.POST,params={"xls"})
public void toXls(HttpServletRequest request,HttpServletResponse response,
        @RequestParam("from_date") String fromDate, @RequestParam("to_date") String toDate)throws IOException, URISyntaxException{

    String sourceFileName = "LoginActivityExcelReport";
    Map<String, Object> parameters = new HashMap<String, Object>();
    **parameters.put(JRParameter.IS_IGNORE_PAGINATION, Boolean.TRUE);**
    List<ActivityReportItem> beanCollection = activityReportService.getActivityListForDownload(fromDate, toDate);
    byte[] xlsReport = new byte[1]; 
    try {           
        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(beanCollection);
        logger.info("!!! Compiling and Generating Excel Report from "+sourceFileName+".jrxml file");
        xlsReport = jasperUtil.generateReportXls(request, parameters, sourceFileName, beanColDataSource);
    } catch (Exception e) {
        logger.error("Error while generating pdf file", e);
    }
    response.addCookie(new Cookie("downloadCompleted","true"));
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", "LoginActivityReport.xls"));
    response.setContentLength(xlsReport.length);
    response.getOutputStream().write(xlsReport);
    response.getOutputStream().flush();
    response.getOutputStream().close(); 
}

关于java - 无法删除重复的列标题和页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37727456/

相关文章:

java - 我如何与 rxAndroid 一起下载多个图像并在 OnNext 方法中获得单独的回调

java - 使用 WAS7 进行 Maven 安装

java - 如何更改默认通知标题?

java - 无法在 JasperReports 中使用子报表

java - 创建 "export to eclipse"功能。我在哪里可以获得项目文件规范?

java - Netty 和预定执行器服务

java - Jasper 报告零值的十进制格式给出 null

java - 两个不同服务器生成的报告之间的差异

jasper-reports - JFreeChartRenderer 类型已被弃用,我应该使用什么方法?

jasper-reports - Jasper中表名的动态参数