我编写了一个小型 Java 程序来练习 Jasper Reports。
下面是我的 java 程序,它将生成报告并导出为 PDF。
public class BaseReporter {
public static void main(String[] args) {
try {
InputStream inputStream = BaseReporter.class.getResourceAsStream("/reports/helloworld.jasper");
DataBeanMaker dataBeanMaker = new DataBeanMaker();
ArrayList<DataBean> dataBeanList = dataBeanMaker.addDataBean();
JRBeanCollectionDataSource JRbeancollectiondatasource = new JRBeanCollectionDataSource(dataBeanList);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("Created By", "xxx");
parameters.put("StylePath", "d:/Learn-WS/Jasper/src/reports/jr.jrtx");
// Export to PDF
JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, JRbeancollectiondatasource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "d:/Learn-WS/Jasper/src/reports/helloworld.pdf");
System.out.println("report process completed ....");
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
我的包结构是
project
src
com
BaseReport.java
DataBean.java
DataBeanMaker.java
reports
helloworld.jasper
helloworld.jrxml
jr.jrtx
jr.properties
在jr.properties文件中,我添加了要在报告中使用的属性。 report.name="测试报告"
我已将我的资源包名称包含在 jasperReport 标记的 helloworld.jrxml 文件中,其属性名为 resourceBundle="jr"。
我在报告中将资源包属性称为
<title>
<band height="30">
<staticText>
<reportElement uuid="6fbe7eb7-04cd-4c03-bc6e-b2cda3026d3b" mode="Opaque" x="0" y="3" width="535" height="23"/>
<textElement textAlignment="Center">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[$R{report.name}]]></text>
</staticText>
</band>
</title>
但是资源包属性未在报告中正确加载/定位。
请帮助我并提前致谢。
最佳答案
我已经找到了位于报告文件中的资源包的解决方案。在报告文件 *.jrxml 文件中,我们使用了标签 textFieldExpression 和父标签 textField,而不是标签 text,效果很好。
<title>
<band height="30">
<textField>
<reportElement uuid="ec4f36de-cf2b-4575-a585-3e860ad8faec" mode="Opaque" x="199" y="0" width="125" height="20" forecolor="#0000FF"/>
<textElement textAlignment="Center">
<font size="10"/>
</textElement>
<textFieldExpression><![CDATA[$R{report.name}]]></textFieldExpression>
</textField>
</band>
</title>
关于java - 在 Jasper 报告中查找资源包属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17068912/