Java jasperreport呈现空pdf

Java jasperreport呈现空pdf,java,jasper-reports,Java,Jasper Reports,拥有: -报表生成器类(Test.class)。此类使用伪非空值填充JRBeanArrayDataSource javabean(OficioSipoData) 简单的报告 我得到的只是一张空白的pdf 检查是否存在异常。main()方法不抛出任何内容 传递给数据源的值列表不是空的 谢谢 课程包括: public class Test { public static void main(String[] args) { Test t = new Test();

拥有: -报表生成器类(Test.class)。此类使用伪非空值填充JRBeanArrayDataSource

  • javabean(OficioSipoData)
  • 简单的报告
我得到的只是一张空白的pdf

  • 检查是否存在异常。main()方法不抛出任何内容
  • 传递给数据源的值列表不是空的
谢谢

课程包括:

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;
    }
}  
公共类测试{
公共静态void main(字符串[]args){
测试t=新测试();
试一试{
t、 dostuff();
}捕获(JRE){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
私有void dostuff()引发JRException,IOException{
Map params=新的HashMap();
文件f=新文件(“C:\\report1.jrxml”);
InputStream fis=新文件InputStream(f);
JasperReport jrsub=jaspecompilemanager.compileReport(fis);
列表l=新的ArrayList();
对于(int i=0;i<5;i++){
addDummyOSD(l,i);
}       
对象[]os=l.toArray();
JRDataSource ds=新的JRBeanArrayDataSource(os);
runReportToPdf(jrsub,params,ds);
byte[]bytes=JasperRunManager.runReportToPdf(jrsub,params,ds);
FileOutputStream fos=新的FileOutputStream(新文件(“C:\\report1.pdf”);
fos.写入(字节);
fos.close();
系统输出打印号(“罚款”);
}
私有void adddummysd(列表A,int i){
OficioSipoData osd=新的OficioSipoData();
osd.setDireccion(“asasa”+i);
osd.setFecha(“2013年11月11日”);
osd.setOperativo(“DSDS”+i);
添加(osd);
}           
}
Iiosipodata的公共类{
私有字符串fecha;
私有字符串指令;
私有字符串运算符;
公共字符串getDireccion(){
返回指令;
}
公共无效设置目录(字符串目录){
this.direccion=direccion;
}
公共字符串getFecha(){
返回fecha;
}
公共void setFecha(字符串fecha){
this.fecha=fecha;
}
公共字符串getOperativo(){
返回操作;
}
公共void setOperativo(字符串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>

您已调用该方法两次

这是因为在第一次调用期间,引擎已经迭代了集合。您可以在第二次调用
jasperunmanager.runReportToPdf
方法之前检查
jrdasource.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");
}
private void dostuff()引发JRException,IOException{
Map params=新的HashMap();
文件f=新文件(“C:\\report1.pdf”);
InputStream fis=新文件InputStream(f);
JasperReport jrsub=jaspecompilemanager.compileReport(fis);
列表l=新的ArrayList();
对于(int i=0;i<5;i++){
addDummyOSD(l,i);
}
byte[]bytes=JasperRunManager.runReportToPdf(jrsub,params,
新的JRBeanarray数据源(l.toArray());
FileOutputStream fos=新的FileOutputStream(新文件(“C:\\report1.pdf”);
fos.写入(字节);
fos.close();
系统输出打印号(“罚款”);
}

您可以看到使用导出器优化代码的示例和其他示例(随JasperReports库包-
JasperReports-x.y.z\demo\samples\
文件夹一起分发)