我编写了这段代码来使用iReport 4.7.1显示一个简单的报告。
private void printReport() {
try {
JRTableModelDataSource dataSource = new JRTableModelDataSource(
new JTable().getModel());
String reportSource = "C:\\Reports\\report1.jrxml";
Map<String, Object> params = new HashMap<String, Object>();
params.put("Name", "SriLanka");
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
e.printStackTrace();
}
}
这是我的图书馆:
- commons-beanutils-1.8.0.jar
- commons-collections-2.1.1.jar
- commons-digester-2.1.jar
- commons-logging-1.1.1.jar
- jasperrreports-4.7.1.jar
- jasperreports-applet-4.7.1.jar
- jasperreports-javaflow-4.7.1.jar
- groovy-all-1.7.5.jar
NetBeans输出显示此警告
2012 年 10 月 8 日下午 2:57:31 net.sf.jasperreports.engine.component.ComponentsEnvironment findBundles 警告:找到命名空间http://jasperreports.sourceforge.net/jasperreports/components的两个组件
这是.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="595" pageHeight="842" whenNoDataType="BlankPage" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5174594e-de3c-4b09-932b-c6665bf6a34b">
<parameter name="Name" class="java.lang.String" isForPrompting="false"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement uuid="fd02a8bc-548b-41c1-aed5-2a7c2e257704" x="210" y="23" width="157" height="36"/>
<textElement>
<font size="18"/>
</textElement>
<textFieldExpression><![CDATA[$P{Name}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch"/>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
报告打开但不显示任何文本。