2016-03-11 162 views
0

我有一個要求,我需要生成XML報告,我需要用來自數據庫的值填充XML。如何生成XML報告?

我開始使用Jasper Reports來生成PDF,xls和CSV報告,但是我無法生成XML。

我有我做了編譯和使用下面的代碼段,用於生成PDF,CSV和XLS一個JRXML文件,但生成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(); 

回答

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和實現自己的邏輯。