当前位置: 首页 > 教程 > JasperReports >

JasperReports 编译模板

精华
小牛编辑
151浏览
2023-03-14

我们在上一章中生成了 JasperReport 模板(JRXML 文件)。该文件不能直接用于生成报表。它必须编译为 JasperReport 的原生二进制格式,称为Jasper文件。在编译时,我们将 JasperDesign 对象转换为 JasperReport 对象。

接口net.sf.jasperreports.engine.design.JRCompiler在编译过程中起着核心作用。该接口有多种实现,具体取决于用于报表表达式的语言,可以用 Java、Groovy、JavaScript 或任何其他脚本语言编写,只要编译器实现可以在运行时对其进行评估。

编译JRXML文件

JasperReports API 提供了一个外观类net.sf.jasperreports.engine.JasperCompileManager用于编译 JasperReport。此类包含几个用于编译报告模板的公共静态方法。模板源可以是文件、输入流和/或内存对象。

下面代码演示如何将JRXML文件编译成Jasper文件。

项目结构如下:

pom.xml文件:

<dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.17.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.7</version>
        </dependency>

jasper_report_template.jrxml:

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
        "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<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 = "jasper_report_template" language = "groovy" pageWidth = "595"
              pageHeight = "842" columnWidth = "555" leftMargin = "20" rightMargin = "20"
              topMargin = "20" bottomMargin = "20">

    <queryString>
        <![CDATA[]]>
    </queryString>

    <field name = "country" class = "java.lang.String">
        <fieldDescription><![CDATA[country]]></fieldDescription>
    </field>

    <field name = "name" class = "java.lang.String">
        <fieldDescription><![CDATA[name]]></fieldDescription>
    </field>

    <columnHeader>
        <band height = "23">

            <staticText>
                <reportElement mode = "Opaque" x = "0" y = "3"
                               width = "535" height = "15" backcolor = "#70A9A9" />

                <box>
                    <bottomPen lineWidth = "1.0" lineColor = "#CCCCCC" />
                </box>

                <textElement />
                <text><![CDATA[]]> </text>
            </staticText>

            <staticText>
                <reportElement x = "414" y = "3" width = "121" height = "15" />

                <textElement textAlignment = "Center" verticalAlignment = "Middle">
                    <font isBold = "true" />
                </textElement>

                <text><![CDATA[Country]]></text>
            </staticText>

            <staticText>
                <reportElement x = "0" y = "3" width = "136" height = "15" />

                <textElement textAlignment = "Center" verticalAlignment = "Middle">
                    <font isBold = "true" />
                </textElement>

                <text><![CDATA[Name]]></text>
            </staticText>

        </band>
    </columnHeader>

    <detail>
        <band height = "16">

            <staticText>
                <reportElement mode = "Opaque" x = "0" y = "0"
                               width = "535" height = "14" backcolor = "#E5ECF9" />

                <box>
                    <bottomPen lineWidth = "0.25" lineColor = "#CCCCCC" />
                </box>

                <textElement />
                <text><![CDATA[]]> </text>
            </staticText>

            <textField>
                <reportElement x = "414" y = "0" width = "121" height = "15" />

                <textElement textAlignment = "Center" verticalAlignment = "Middle">
                    <font size = "9" />
                </textElement>

                <textFieldExpression class = "java.lang.String">
                    <![CDATA[$F{country}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x = "0" y = "0" width = "136" height = "15" />
                <textElement textAlignment = "Center" verticalAlignment = "Middle" />

                <textFieldExpression class = "java.lang.String">
                    <![CDATA[$F{name}]]>
                </textFieldExpression>
            </textField>

        </band>
    </detail>

</jasperReport>

JasperReportsDemo编译程序:

package cn.xnip;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;

public class JasperReportsDemo {

    public static void main(String[] args) {
        String filePath = "D:/idea_codes/jasper_reports/src/main/java/com/xnip/jasper_report_template.jrxml";
        try {
            JasperCompileManager.compileReportToFile(filePath,"d:/jasper_report_template.jasper");
            System.out.println("编译成功");
        } catch (JRException e) {
            e.printStackTrace();
        }
    }
}

运行JasperReportsDemo程序后,在d:/下可以看到jasper_report_template.jasper文件。

预览Jasper文件

package cn.xnip;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.view.JasperDesignViewer;

public class JasperReportsDemo {

    public static void main(String[] args) {
        String filePath = "d:/jasper_report_template.jasper";
        try {
            JasperDesignViewer.viewReportDesign(filePath,false);
        } catch (JRException e) {
            e.printStackTrace();
        }
    }
}

输出结果为: