我使用 iReport 设计了我的报告,并在 Java 中进行了编译。
当我通过 Java 生成 PDF 格式的报告时,我得到空值。
我在派对数据集中得到Key为null。错误,输出 PDF 或 html 具有 null 值。
我的代码:
public class PdfFromJasperFile {
public static void main(String[] args) throws JRException, IOException {
JasperReport jasperReport;
JasperPrint jasperPrint;
Map param = new HashMap();
try {
String sourceFileName = "C:/Users/Sadagopan/Documents/report3.jrxml";
jasperReport = JasperCompileManager.compileReport(sourceFileName);
jasperPrint = JasperFillManager.fillReport(jasperReport, param,
new JREmptyDataSource());
JasperExportManager.exportReportToHtmlFile(jasperPrint,
"C:/Users/Sadagopan/Documents/sample.html");
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的 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="report3" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3d69a5d5-0c1e-4b1f-9424-513fa329f402">
<queryString>
<![CDATA[Select * from person where name ='sadagopan']]>
</queryString>
<field name="id" class="java.lang.Long">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="address" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="phonenumber" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="salary" class="java.math.BigDecimal">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="pension" class="java.math.BigDecimal">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<group name="id">
<groupExpression><![CDATA[$F{id}]]></groupExpression>
</group>
<group name="name">
<groupExpression><![CDATA[$F{name}]]></groupExpression>
</group>
<group name="phonenumber">
<groupExpression><![CDATA[$F{phonenumber}]]></groupExpression>
</group>
<group name="salary">
<groupExpression><![CDATA[$F{salary}]]></groupExpression>
</group>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="60" splitType="Stretch">
<staticText>
<reportElement uuid="a006c204-9feb-4302-90b1-fe5bdddbe3d8" x="89" y="11" width="384" height="49"/>
<textElement>
<font size="36"/>
</textElement>
<text><![CDATA[Person Salary report]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="27" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="25" splitType="Stretch">
<staticText>
<reportElement uuid="b74a5413-2dee-4cd5-ac06-e73a022343bc" x="0" y="5" width="48" height="20"/>
<textElement/>
<text><![CDATA[ID ]]></text>
</staticText>
<staticText>
<reportElement uuid="9df2c923-e5b6-4bb0-95f0-5c437495ea53" x="63" y="5" width="100" height="20"/>
<textElement/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement uuid="b054ca79-f62e-4c80-817f-270e9ee4839a" x="163" y="5" width="176" height="20"/>
<textElement/>
<text><![CDATA[Address]]></text>
</staticText>
<staticText>
<reportElement uuid="e49b408e-8c6f-4aea-af7b-15ab31eb4643" x="329" y="5" width="100" height="20"/>
<textElement/>
<text><![CDATA[Phonenumber]]></text>
</staticText>
<staticText>
<reportElement uuid="4bf8ce1f-5284-4642-a45a-90aeff267abd" x="429" y="5" width="59" height="20"/>
<textElement/>
<text><![CDATA[salary]]></text>
</staticText>
<staticText>
<reportElement uuid="f99f4ce6-d8df-41b6-b1cb-8a5ef17b46c3" x="488" y="5" width="100" height="20"/>
<textElement/>
<text><![CDATA[pension]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="53" splitType="Stretch">
<textField>
<reportElement uuid="e91cc334-4623-42a2-bc78-e6c248606f65" x="0" y="0" width="48" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="3b8498fb-8560-4178-86bf-183ab6855d49" x="63" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="78c2be84-eebd-4465-b8c2-53d201e3246a" x="163" y="0" width="166" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="813442d3-a459-4fc9-83fb-ac170bf5a9a8" x="329" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{phonenumber}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="37d739d1-f413-465f-aecc-6bc2a3b8ff72" x="429" y="0" width="59" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{salary}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="99c32bab-b3d5-4ade-bb47-b3858ce6e2d6" x="488" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{pension}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="236" splitType="Stretch">
<pieChart>
<chart>
<reportElement uuid="e214bf8f-78d8-424b-a617-4b417385a9e5" x="48" y="21" width="440" height="215"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<keyExpression><![CDATA[$F{id}]]></keyExpression>
<valueExpression><![CDATA[$F{salary}]]></valueExpression>
</pieDataset>
<piePlot>
<plot/>
<itemLabel/>
</piePlot>
</pieChart>
</band>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
答案 0 :(得分:1)
这是因为您将JREmptyDataSource()
传递给了报告,这就是$F{id}
为空的原因
以下是您可以传递给报告的datasources。
答案 1 :(得分:0)
任何人参考的工作代码
`public class TestJasper {
public static void main(String[] args) {
String reportPath = "C:/Users/Sadagopan/Documents/report3.jasper";
Map<String, Object> params = new HashMap<String, Object>();
Connection connection;
try {
// JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/person","root","password");
System.out.println("Filling report...");
JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, params, connection);
JasperExportManager.exportReportToHtmlFile(jasperPrint, "C:/Users/Sadagopan/Documents/sample.html");
JasperViewer.viewReport(jasperPrint, false);
connection.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}