2013-04-24 78 views
0

有: - 报告生成器类(Test.class)。该类使用虚拟非空值填充JRBeanArrayDataSource。jasperreport渲染为空pdf

  • 一个JavaBean(OficioSipoData)
  • 一个简单的报告

我得到的是一个空白的PDF文件。

  • 检查异常。 main()方法抛出任何东西
  • 传递给DataSource的值列表不为空。

感谢

的类:

public class Test { 
    public static void main(String[] args) { 
     Test t = new Test(); 
     try { 
      t.dostuff(); 
     } catch (JRException e) {    
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void dostuff() throws JRException, IOException {   
     Map<String,Object> params = new HashMap<String, Object>();        
     File f = new File("C:\\report1.jrxml"); 
     InputStream fis = new FileInputStream(f); 
     JasperReport jrsub = JasperCompileManager.compileReport(fis); 


     List<OficioSipoData> l = new ArrayList<OficioSipoData>(); 
     for (int i = 0; i < 5; i++) { 
      addDummyOSD(l,i); 
     }  

     Object[] os = l.toArray(); 
     JRDataSource ds = new JRBeanArrayDataSource(os);   
     JasperRunManager.runReportToPdf(jrsub, params, ds); 
     byte[] bytes = JasperRunManager.runReportToPdf(jrsub, params, ds); 
     FileOutputStream fos = new FileOutputStream(new File("C:\\report1.pdf")); 
     fos.write(bytes); 
     fos.close(); 
     System.out.println("fine"); 
    } 

    private void addDummyOSD(List<OficioSipoData> lista, int i) { 
     OficioSipoData osd = new OficioSipoData(); 
     osd.setDireccion("asasa " + i); 
     osd.setFecha("11/11/2013"); 
     osd.setOperativo("dsdsds" + i); 
     lista.add(osd); 
    }   
} 


public class OficioSipoData { 
    private String fecha; 
    private String direccion; 
    private String operativo; 

    public String getDireccion() { 
     return direccion; 
    } 
    public void setDireccion(String direccion) { 
     this.direccion = direccion; 
    } 

    public String getFecha() { 
     return fecha; 
    } 
    public void setFecha(String fecha) { 
     this.fecha = fecha; 
    } 

    public String getOperativo() { 
     return operativo; 
    } 
    public void setOperativo(String operativo) { 
     this.operativo = operativo; 
    } 
} 

的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="report1" language="groovy" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c79c5925-d8ca-40e8-bad2-1eaadb05c38f"> 
    <property name="ireport.zoom" value="1.0"/> 
    <property name="ireport.x" value="0"/> 
    <property name="ireport.y" value="0"/> 
    <field name="direccion" class="java.lang.String"/> 
    <field name="fecha" class="java.lang.String"/> 
    <background> 
     <band splitType="Stretch"/> 
    </background> 
    <title> 
     <band height="113" splitType="Stretch"> 
      <staticText> 
       <reportElement uuid="e412065c-5a73-462f-9390-0096e54f80b3" x="226" y="23" width="100" height="20"/> 
       <textElement/> 
       <text><![CDATA[wiii]]></text> 
      </staticText> 
     </band> 
    </title> 
    <pageHeader> 
     <band height="58" splitType="Stretch"> 
      <staticText> 
       <reportElement uuid="ffc0130b-fee2-4861-b33a-a1078456813f" x="216" y="13" width="100" height="20"/> 
       <textElement/> 
       <text><![CDATA[waaa]]></text> 
      </staticText> 
     </band> 
    </pageHeader> 
    <columnHeader> 
     <band splitType="Stretch"/> 
    </columnHeader> 
    <detail> 
     <band height="132" splitType="Stretch"> 
      <staticText> 
       <reportElement uuid="e34f4c70-8e1c-405b-b3f7-90e11e5e9b7e" x="0" y="4" width="100" height="20"/> 
       <textElement/> 
       <text><![CDATA[direccion]]></text> 
      </staticText> 
      <textField> 
       <reportElement uuid="ab77eb85-4dfa-44da-aa5e-3ad98b7b426e" x="100" y="4" width="100" height="20"/> 
       <textElement/> 
       <textFieldExpression><![CDATA[$F{direccion}]]></textFieldExpression> 
      </textField> 
     </band> 
    </detail> 
    <columnFooter> 
     <band splitType="Stretch"/> 
    </columnFooter> 
    <pageFooter> 
     <band splitType="Stretch"/> 
    </pageFooter> 
    <summary> 
     <band splitType="Stretch"/> 
    </summary> 
</jasperReport> 

回答

0

你调用的方法JasperRunManager.runReportToPdf(JasperReport , java.util.Map<java.lang.String,java.lang.Object>, JRDataSource)两次。

而这是结合,因为在第一次调用时引擎已经迭代了集合。在第二次调用JasperRunManager.runReportToPdf方法之前,您可以检查JRDataSource.next()方法的结果。

你正确的代码将是:

private void dostuff() throws JRException, IOException { 
    Map<String, Object> params = new HashMap<String, Object>(); 
    File f = new File("C:\\report1.pdf"); 
    InputStream fis = new FileInputStream(f); 
    JasperReport jrsub = JasperCompileManager.compileReport(fis); 

    List<OficioSipoData> l = new ArrayList<OficioSipoData>(); 
    for (int i = 0; i < 5; i++) { 
     addDummyOSD(l, i); 
    } 

    byte[] bytes = JasperRunManager.runReportToPdf(jrsub, params, 
      new JRBeanArrayDataSource(l.toArray())); 
    FileOutputStream fos = new FileOutputStream(new File(""C:\\report1.pdf"")); 
    fos.write(bytes); 
    fos.close(); 
    System.out.println("fine"); 
} 

你可以看到JavaBean Data Sources样品和其他样品(有的JasperReports库包分发 - jasperreports-x.y.z\demo\samples\文件夹)使用出口商优化你的代码。