如何生成XML报告?

时间:2016-03-11 16:10:56

标签: jasper-reports export-to-xml

我需要生成XML报告,我需要使用Database中的值填充XML。

我开始使用Jasper Reports,我可以生成PDF,xls和CSV的报告,但我无法生成XML。

我有一个jrxml文件,我编译并使用下面的代码段生成PDF,CSV和XLS,但生成XML似乎是一个问题:

if(contentType == 'text/csv'){
    params.response.setHeader("Content-disposition", "attachment;filename=" + params.reportName + ".csv")
    exporter = new net.sf.jasperreports.engine.export.JRCsvExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    if(params.feed){
        exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER, "|");
    }
    fileName = reportTempDir + "/" + fileName + ".csv"
}else if(contentType== 'application/pdf'){
    params.response.setHeader("Content-disposition", "attachment;filename=" + params.reportName + ".pdf")
    exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    fileName = reportTempDir + "/" + fileName + ".pdf"
}else{
    params.response.setHeader("Content-disposition", "attachment;filename=" + params.reportName + ".xls")
    exporter = new net.sf.jasperreports.engine.export.JExcelApiExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,true);
    exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET ,false);
    exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS ,true);
    exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS ,true);
    exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND ,false);
    exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS ,false);
    exporter.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN ,false);
    exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_CELL_BORDER ,false);
    exporter.setParameter(JRXlsExporterParameter.IS_FONT_SIZE_FIX_ENABLED ,true);
    exporter.setParameter(JRXlsExporterParameter.SHEET_NAMES ,[sheetName]as String[]);
    fileName = reportTempDir + "/" + fileName + ".xls"
}
FileOutputStream osGenre = new FileOutputStream(fileName);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, osGenre);
exporter.exportReport();

1 个答案:

答案 0 :(得分:0)

JRXmlExporter

JRXmlExporter exporter  = new JRXmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleXmlExporterOutput(outputSteam));
exporter.exportReport();

这将生成JasperPrint的xml文件,但它不是一个干净的数据提取,而只是一个xml表示的打印

示例

<?xml version="1.0" encoding="UTF-8"?>
<jasperPrint xmlns="http://jasperreports.sourceforge.net/jasperreports/print" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/print http://jasperreports.sourceforge.net/xsd/jasperprint.xsd" name="ReportStat" pageWidth="595" pageHeight="842" topMargin="30" leftMargin="20" bottomMargin="30" rightMargin="20" locale="it_IT" timezone="Europe/Berlin">
    <origin band="title"/>
    <origin band="columnHeader"/>
    <origin band="detail"/>
    <origin band="summary"/>
    <page>
       <text verticalAlignment="Middle" textHeight="12.578125" lineSpacingFactor="1.2578125" leadingOffset="-2.1972656">
            <reportElement uuid="dfe13f55-12a6-4c33-b5ba-00dd61f37c96" mode="Opaque" x="20" y="335" width="100" height="20" forecolor="#000000" backcolor="#CCCCCC" origin="1" srcId="2" printId="1"/>
            <box leftPadding="2">
                <topPen lineWidth="0.25"/>
                <leftPen lineWidth="0.25"/>
                <bottomPen lineWidth="0.25"/>
                <rightPen lineWidth="0.25"/>
            </box>
            <textContent><![CDATA[TOTALE]]></textContent>
        </text>
        ....
    </page>
</jasperPrint>

如果您需要更清晰地提取数据,则需要创建自己的CustomExporter,一个良好的开端可以是覆盖JRCsvExporter并实现您自己的逻辑。