JasperReports导出CSV,空输出

时间:2012-03-28 08:52:27

标签: csv jasper-reports export-to-csv

使用JasperReports导出为PDF工作。但是当我导出到CSV时,我得到一个空文件:/

jasper .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="Unnamed" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30"> 

<field name="id" class="java.lang.Long"/>

<detail>
    <band height="100">
        <textField isStretchWithOverflow="true">
            <reportElement positionType="Float" x="0" y="5" width="50" height="15"/>
            <box leftPadding="10" rightPadding="10"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
        </textField>        
    </band>
</detail>

javacode:

JasperReport jasperReport =       JasperCompileManager.compileReport(this.getClass().getClassLoader()
            .getResourceAsStream("template.jrxml"));

Map<String, Object> parameters = new HashMap<String, Object>();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, getDataSource());

ByteArrayOutputStream baos = new ByteArrayOutputStream();
JRExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
return baos.toByteArray();

2 个答案:

答案 0 :(得分:0)

我看不到你在哪里调用exporter.exportReport()。

答案 1 :(得分:0)

是的,您只需要添加exporter.exportReport();

exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,baos)之后;

工作正常。