有: - 报告生成器类(Test.class)。此类使用虚拟非空值填充JRBeanArrayDataSource。
我得到的只是一个空白的PDF格式。
由于
课程:
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 :(得分:1)
这是 wroing ,因为在第一次调用期间,引擎已经迭代了集合。您可以在第二次调用JRDataSource.next()
方法之前检查JasperRunManager.runReportToPdf
方法的结果。
您的正确代码将是:
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\
文件夹一起分发),以优化您的代码。