2012-03-28 104 views
0

使用JasperReports導出爲PDF作品。但是,當我導出爲CSV,我得到一個空文件:/JasperReports導出CSV,空輸出

碧玉的.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(); 
+1

從您發佈的代碼看起來很好,但是您沒有將代碼寫入實際文件的代碼。你確定這個工作正常嗎? byte []數組是否返回null或空? – 2012-03-28 19:30:03

回答

0

我沒有看到你在哪裏調用exporter.exportReport()。