一、介绍

1. JasperReport介绍

JasperReport 是一个开源的Java报表引擎。Jasper Reports有提供丰富的内容到屏幕上,到打印机,或转换成PDF,HTML,XLS,RTF,ODT,CSV,TXT和XML文件的能力。Jasper Reports是一个Java类库,需要嵌入到Java应用程序中。Jasper Report的特点如下:

  • 具有灵活的报表布局;

  • 可以用文字或图形显示数据;

  • 可以接受来自多个数据源的数据(JDBC,ODBC,JavaBean等);

  • 可以生成水印(水印是这样的方式被放置在主图像的副图像);

  • 可以生成子报表;

  • 能够导出报表到多种格式的。

2.开发流程

image ①设计阶段:JasperReport解析的文件位xml文件,他的文件后缀位jrxml实则也是xml文件,设计时可以直接对xml文件进行编辑,这需要你对JasperReport库里的标签有足够的了解,另一种是可视化拖拽方式进行设计JasperReport Studio ②编译阶段:JasperReport通过库里的API 将jrxml文件便以为jasper文件,其中会对文件进行语法和标签等进行校验 ③数据填充阶段:将报表模板编译好后,我们需要对模板你的数据字段进行插值,而通过病痛的数据源进行map映射进模板中的插值中。 ④打印导出阶段:这个阶段会将数据填充好后的报表,以自己预设的导出或打印参数格式进行输出。

3. 原理过程

image 源码解析:

public String compileToFile(String sourceFileName) throws JRException
{
    File sourceFile = new File(sourceFileName);
    //加载xml源文件
    JasperDesign jasperDesign = JRXmlLoader.load(sourceFileName);
​
    File destFile = new File(sourceFile.getParent(), jasperDesign.getName() + ".jasper");
    String destFileName = destFile.toString();
    //对文件进行编译
    compileToFile(jasperDesign, destFileName);
        
    return destFileName;
}
/**
*compileToFile(jasperDesign, destFileName)->compile(JasperDesign jasperDesign)
*而该方法调用JRCompiler的抽象类实现中的方法
*compileReport(JasperDesign jasperDesign)进行处理(该方法较长哦,
*大家可以自己拉代码下来看一下,关键的地方官方是有给注释的)
*/

二、JasperReport Studio可视化工具下载使用

这个工具是有社区提供的,是基于eclipse开发的报表设计工具,之前使用的是iReport,后面更新了界面。更加美观了些(但也存在不少bug所以更新的也很快哦) 下载地址

https://community.jaspersoft.com/community-download

下载后打开安装,都选默认的就好 这是整个的操作界面 image.pngProject Explorer中默认会帮我们将jasperReport的相关依赖包和服务导入进来,我们只要去在默认的MyReports或自己新建一个项目里进行新建模板 image.png

数据源选择:可以为空 image.png

操作界面说明 image.png

源码界面 image.png

预览界面 image.png

四、例子:制作一个简单报表

创建数据源连接,这里我们使用jdbc数据连接源 image.png

选择合适的数据源连接 image.png

image.png

在这里我们还需要添加数据库连接驱动包,不然无法连接成功 image.png

image.png

①:文本sql编写 ②:概述sql命令插入 ③:简图sql模型 image.png

完成后会在数据展示域中的Fileds里展示出来 image.png

image.png

源文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.20.0.final using JasperReports Library version 6.20.0-2bc7ab61c56f459e8176eb05c7705e145cd400ad  -->
<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="simple" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3152ff1b-cd90-4327-ba89-4bbc90663721">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="postgresJDBCSourceData"/>
    <property name="com.jaspersoft.studio.data.sql.tables">
        <![CDATA[ZW1wbG95ZWVzIEFTICwxMzYsMTU4LGRkOWJiNGQwLTIzMGUtNDU4ZS05OWFlLWJhMGMzMmU5YzU4
Nzs=]]>
    </property>
    <property name="com.jaspersoft.studio.property.dataset.dialog.DatasetDialog.sash.w1" value="642"/>
    <property name="com.jaspersoft.studio.property.dataset.dialog.DatasetDialog.sash.w2" value="357"/>
    <queryString>
        <![CDATA[SELECT name,
    sex,
    age,
    position
FROM employees]]>
    </queryString>
    <field name="name" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.name" value="name"/>
        <property name="com.jaspersoft.studio.field.label" value="name"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="employees"/>
    </field>
    <field name="sex" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.name" value="sex"/>
        <property name="com.jaspersoft.studio.field.label" value="sex"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="employees"/>
    </field>
    <field name="age" class="java.lang.Integer">
        <property name="com.jaspersoft.studio.field.name" value="age"/>
        <property name="com.jaspersoft.studio.field.label" value="age"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="employees"/>
    </field>
    <field name="position" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.name" value="position"/>
        <property name="com.jaspersoft.studio.field.label" value="position"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="employees"/>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="50" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="555" height="50" uuid="814641ac-64c0-43cc-b115-cc36735fa756"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="仿宋" size="22"/>
                </textElement>
                <text><![CDATA[我的第一个报表]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="30" uuid="49a4dc68-69f7-433b-b1b3-b5f37ddce82b">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="02bb75af-4edd-470b-87c5-a4de4f2f6932"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <text><![CDATA[name]]></text>
            </staticText>
            <staticText>
                <reportElement x="100" y="0" width="100" height="30" uuid="a7229bff-58b6-4677-a5b6-6772d970a004">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="aa541505-e043-4b32-ab07-7d02a14276aa"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <text><![CDATA[sex]]></text>
            </staticText>
            <staticText>
                <reportElement x="200" y="0" width="100" height="30" uuid="40d17a15-8618-49f9-8357-59c553d064d1">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="39831a14-c866-4c28-bd40-e80c3466401d"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <text><![CDATA[age]]></text>
            </staticText>
            <staticText>
                <reportElement x="300" y="0" width="100" height="30" uuid="9dacdf69-a4f4-4db9-82bd-3b1ce699d324">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="a3805d61-a3a5-4221-880e-784f084b9629"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <text><![CDATA[position]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="30" uuid="55ddb82a-ee90-4b27-a0a7-cf13a00bcae5">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="02bb75af-4edd-470b-87c5-a4de4f2f6932"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.5"/>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="100" height="30" uuid="a3b192d8-745a-4add-bb71-2cd215b6ff0e">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="aa541505-e043-4b32-ab07-7d02a14276aa"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.5"/>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{sex}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="200" y="0" width="100" height="30" uuid="b186109d-fd73-4418-b67c-0bdd9f6f5d84">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="39831a14-c866-4c28-bd40-e80c3466401d"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.5"/>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="300" y="0" width="100" height="30" uuid="614b3b1b-ce2f-4499-9356-f66c38b82bf0">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="a3805d61-a3a5-4221-880e-784f084b9629"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.5"/>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{position}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="47" splitType="Stretch">
            <staticText>
                <reportElement x="178" y="17" width="100" height="30" uuid="13974a7a-04a5-42dd-8bf6-162fe6756ba8"/>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <text><![CDATA[Static Text]]></text>
            </staticText>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch">
            <staticText>
                <reportElement x="209" y="5" width="100" height="30" uuid="c3190f61-e779-4159-b13f-1e9c14b01f90"/>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <text><![CDATA[我是页尾]]></text>
            </staticText>
        </band>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch">
            <staticText>
                <reportElement x="200" y="0" width="100" height="30" uuid="9d7e6f0f-9934-46f0-ae54-6522c346c23c"/>
                <textElement>
                    <font fontName="新宋体" size="22"/>
                </textElement>
                <text><![CDATA[总结域]]></text>
            </staticText>
        </band>
    </summary>
</jasperReport>

预览:

Q.E.D.