目录
创建项目文件夹
项目文件夹可帮助您整理报告。
要创建项目文件夹:
1。 | 选择文件>新建>项目。 |
将出现“ 选择向导”对话框。
选择一个向导 |
|
2。 | 在向导栏中输入Jasper以过滤所有操作。仅显示与Jaspersoft Studio相关的内容 |
3。 | 选择JasperReports项目。单击下一步。 |
在新的JasperReports项目出现向导。
4。 | 输入项目的名称,然后单击“ 完成”。 |
在项目资源管理器将显示您的项目。
|
创建新报告
Jaspersoft Studio提供了从头开始创建报表,添加自己的模板或使用提供的模板之一的选项。所有这些选项都允许您指定数据源。您有三个数据源选项:
一个空记录 - 空行:这是用于创建没有任何数据的报告的默认数据适配器。在将报表连接到数据源之前,它可用于定义报表的布局。在创建报告之后,可以在以后添加或更改数据源。 |
示例DB - 数据库JBDC连接:这是随Jaspersoft Studio安装提供的SQL数据库。 |
创建新的数据适配器:Jaspersoft Studio支持各种数据源。使用“ 新建”按钮可以浏览并连接到现有数据源。 |
要创建新报告:
1。 | 打开“ 文件”菜单,选择“ 新建”,然后单击“ Jasper报告”。 |
将出现“ 新建报告向导”>“报告模板”窗口
新报告向导 |
|
2。 | 选择Coffee,然后单击Next。 |
出现“ 新建报告向导”>“报告文件”窗口。
新报告向导>报告文件 |
|
3。 | 在工作区中选择要放置报告的文件夹,然后命名新报告。 |
您可以使用默认文件夹MyReports,将报告命名为Example example。
如果要为报告创建新项目文件夹,请参阅创建项目文件夹。
4。 | 单击下一步。 |
将出现“ 新建报告向导”>“数据源”窗口。
5。 | 选择Sample DB - Database JDBC Connection。 |
输入查询select * from orders,然后单击Next。
新报告向导>数据源 |
|
将出现“ 新建报告向导”>“域”窗口。
新报告向导>字段 |
|
6。 | 左侧列表显示了所有已发现的字段; 右侧列表显示将添加到报告中的那些。选择以下字段,然后单击向右箭头将其添加到报表中。 |
• | ORDERID |
• | SHIPNAME |
• | SHIPADDRESS |
• | SHIPCITY |
• | SHIPREGION |
7。 | 选择字段后,单击“ 下一步”。 |
将出现“ 新建报告向导”>“分组”窗口。
新报告向导>分组 |
|
8。 | 单击下一步。 |
9。 | 单击完成。 |
Jaspersoft Studio现在构建包含所选字段的报表布局,如“设计视图中的新报表”所示:
设计视图中的新报告 |
|
添加和删除报告元素
创建报告后,您可以添加和删除字段和其他元素,而无需创建新报告。
将字段添加到报表
要向已创建的报告添加字段:
1。 | 从“ 大纲”视图中选择报告的主节点。 |
2。 | 选择“ 属性”选项卡。 |
属性选项卡 |
|
3。 | 在Properties选项卡上,单击Report按钮。 |
4。 | 按“ 编辑查询,筛选和排序”选项按钮。 |
“ 数据集和查询”对话框将打开。
数据集和查询对话框 |
|
5。 | 单击“ 读取字段”按钮添加更多字段。 |
发现的所有字段都将作为新字段添加到报表中。
| 您还可以在同一对话框中编辑或更改查询。如果新查询发现的字段少于现有报表中使用的字段,则会从报表中删除未包含在新查询中的字段。 |
6。 | 单击确定。 |
您将返回“ 设计”视图。
7。 | 扩展字段在大纲视图中看到所有的字段现在可用于报表。 |
字段 |
|
8。 | 要向报表添加字段,请单击“ 大纲”视图中的字段并将其拖到“ 设计”视图中。 |
在详细信息区域内拖动字段对象时,Jaspersoft Studio会创建一个文本字段元素并设置该元素的文本字段表达式。
删除字段
要从报表中删除字段,请执行以下操作:在“ 设计”视图中,右键单击要删除的字段,然后选择“ 删除”。
添加其他元素
要添加其他元素(如线条,图像或图表),请从调色板中拖动元素 如下图所示进入“ 设计”视图,然后根据需要调整大小并进行排列。
元素调色板 |
|
预览报告
要预览报告,请单击“ 预览”选项卡(在“ 设计”视图中朝向屏幕底部)切换到“预览”模式。预览在后台编译报表,并通过JDBC连接填充查询检索的数据。如“报告预览”中所示,详细信息带会针对查询结果中的每一行重复,从而创建一个简单的表格报告:
|
创建Crosstab表
选择Blank_A4_Landscape报告模板
|
state.sql
-- ----------------------------
-- Table structure for state
-- ----------------------------
DROP TABLE IF EXISTS `state`;
CREATE TABLE `state` (
`country` varchar(40) DEFAULT NULL COMMENT '国家',
`city` varchar(40) DEFAULT NULL COMMENT '城市'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of state
-- ----------------------------
INSERT INTO `state` VALUES ('CHINA', 'Beijing');
INSERT INTO `state` VALUES ('CHINA', 'Beijing');
INSERT INTO `state` VALUES ('CHINA', 'Beijing');
INSERT INTO `state` VALUES ('CHINA', 'Shanghai');
INSERT INTO `state` VALUES ('USA', 'Chicago');
INSERT INTO `state` VALUES ('USA', 'Chicago');
INSERT INTO `state` VALUES ('USA', 'Alaska');
INSERT INTO `state` VALUES ('USA', 'Alaska');
INSERT INTO `state` VALUES ('USA', 'Alaska');
删除无用列,拖拽Palette区域crosstab到Summary
创建汇总表
product.sql
-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`cost` decimal(10,0) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES ('1001', 'Iron Iron', '5');
INSERT INTO `product` VALUES ('1001', 'Chair Shoe', '25');
INSERT INTO `product` VALUES ('1001', 'Telephone Clock', '25');
INSERT INTO `product` VALUES ('1001', 'Chair Chair', '25');
INSERT INTO `product` VALUES ('1001', 'Ice Tea Shoe', '13');
INSERT INTO `product` VALUES ('1001', 'Clock Clock', '24');
INSERT INTO `product` VALUES ('1001', 'Ice Tea Chair', '10');
INSERT INTO `product` VALUES ('1001', 'Telephone Shoe', '8');
INSERT INTO `product` VALUES ('1001', 'Ice Tea Clock', '23');
INSERT INTO `product` VALUES ('1001', 'Clock Telephone', '17');
INSERT INTO `product` VALUES ('1001', 'Telephone Ice Tea', '20');
INSERT INTO `product` VALUES ('1001', 'Telephone Iron', '9');
INSERT INTO `product` VALUES ('1001', 'Clock Ice Tea', '17');
INSERT INTO `product` VALUES ('1001', 'Telephone Clock', '18');
INSERT INTO `product` VALUES ('1001', 'Telephone Iron', '12');
INSERT INTO `product` VALUES ('1001', 'Ice Tea Chair', '9');
INSERT INTO `product` VALUES ('1001', 'Ice Tea Shoe', '19');
INSERT INTO `product` VALUES ('1001', 'Clock Ice Tea', '22');
INSERT INTO `product` VALUES ('1001', 'Chair Clock', '17');
INSERT INTO `product` VALUES ('1001', 'Ice Tea Ice Tea', '11');
INSERT INTO `product` VALUES ('1001', 'Ice Tea Telephone', '20');
INSERT INTO `product` VALUES ('1001', 'Chair Chair', '11');
INSERT INTO `product` VALUES ('1001', 'Iron Iron', '7');
INSERT INTO `product` VALUES ('1001', 'Shoe Chair', '8');
INSERT INTO `product` VALUES ('1001', 'Chair Shoe', '7');
INSERT INTO `product` VALUES ('1002', 'Shoe Shoe', '16');
INSERT INTO `product` VALUES ('1002', 'Shoe Shoe', '25');
INSERT INTO `product` VALUES ('1002', 'Telephone Iron', '23');
INSERT INTO `product` VALUES ('1002', 'Clock Iron', '3');
INSERT INTO `product` VALUES ('1002', 'Chair Telephone', '11');
INSERT INTO `product` VALUES ('1002', 'Shoe Iron', '23');
INSERT INTO `product` VALUES ('1002', 'Ice Tea Telephone', '5');
INSERT INTO `product` VALUES ('1002', 'Clock Iron', '19');
INSERT INTO `product` VALUES ('1002', 'Iron Chair', '18');
INSERT INTO `product` VALUES ('1002', 'Chair Iron', '26');
INSERT INTO `product` VALUES ('1002', 'Telephone Shoe', '8');
INSERT INTO `product` VALUES ('1002', 'Ice Tea Iron', '3');
INSERT INTO `product` VALUES ('1002', 'Clock Shoe', '9');
INSERT INTO `product` VALUES ('1002', 'Clock Ice Tea', '22');
INSERT INTO `product` VALUES ('1002', 'Ice Tea Shoe', '15');
INSERT INTO `product` VALUES ('1002', 'Shoe Clock', '3');
INSERT INTO `product` VALUES ('1002', 'Clock Ice Tea', '21');
INSERT INTO `product` VALUES ('1002', 'Iron Chair', '20');
INSERT INTO `product` VALUES ('1002', 'Telephone Clock', '9');
INSERT INTO `product` VALUES ('1002', 'Ice Tea Ice Tea', '10');
INSERT INTO `product` VALUES ('1002', 'Iron Ice Tea', '18');
INSERT INTO `product` VALUES ('1002', 'Ice Tea Clock', '6');
INSERT INTO `product` VALUES ('1002', 'Ice Tea Iron', '18');
INSERT INTO `product` VALUES ('1002', 'Clock Clock', '21');
INSERT INTO `product` VALUES ('1002', 'Iron Iron', '2');
修改属性,调整样式
添加变量total_num和total_sum
预览效果
添加参数whereSql
修改查询sql
SELECT
id,
`name`,
count(cost) num,
sum(cost) sum
FROM
product
WHERE
$P!{whereSql}
GROUP BY
id,
`name`
预览输入参数cost<15
最终product.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="product" pageWidth="792" pageHeight="612" orientation="Landscape" columnWidth="752" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9138ecee-c3d8-405d-b504-a3a317a83215">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="mysql-test"/>
<parameter name="whereSql" class="java.lang.String"/>
<queryString language="SQL">
<![CDATA[SELECT
id,
`name`,
count(cost) num,
sum(cost) sum
FROM
product
WHERE
$P!{whereSql}
GROUP BY
id,
`name`]]>
</queryString>
<field name="id" class="java.lang.Integer">
<property name="com.jaspersoft.studio.field.label" value="id"/>
<property name="com.jaspersoft.studio.field.tree.path" value="product"/>
</field>
<field name="name" class="java.lang.String">
<property name="com.jaspersoft.studio.field.label" value="name"/>
<property name="com.jaspersoft.studio.field.tree.path" value="product"/>
</field>
<field name="num" class="java.lang.Long">
<property name="com.jaspersoft.studio.field.label" value="num"/>
</field>
<field name="sum" class="java.math.BigDecimal">
<property name="com.jaspersoft.studio.field.label" value="sum"/>
</field>
<variable name="total_sum" class="java.math.BigDecimal" calculation="Sum">
<variableExpression><![CDATA[$F{sum}]]></variableExpression>
</variable>
<variable name="total_num" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{num}]]></variableExpression>
</variable>
<group name="id">
<groupExpression><![CDATA[$F{id}]]></groupExpression>
</group>
<group name="name">
<groupExpression><![CDATA[$F{name}]]></groupExpression>
</group>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="41" splitType="Stretch">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
<staticText>
<reportElement stretchType="ContainerHeight" x="0" y="0" width="188" height="41" forecolor="#2838C9" backcolor="#F26B30" uuid="8d0fc348-ad9b-41de-b630-4feece4942d9">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="f660a072-4c3b-4d9e-a045-ab75c05be0ed"/>
</reportElement>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="20" isBold="true"/>
<paragraph lineSpacing="Double"/>
</textElement>
<text><![CDATA[订单编号]]></text>
</staticText>
<staticText>
<reportElement stretchType="ContainerHeight" x="188" y="0" width="188" height="41" forecolor="#2838C9" backcolor="#F26B30" uuid="4e5c394c-bcab-4b74-9083-c7833d67047d">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="c87ead29-a587-4fd2-95db-cec89cda4eea"/>
</reportElement>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="20" isBold="true"/>
<paragraph lineSpacing="Double"/>
</textElement>
<text><![CDATA[订单名称]]></text>
</staticText>
<staticText>
<reportElement stretchType="ContainerHeight" x="376" y="0" width="188" height="41" forecolor="#2838C9" backcolor="#F26B30" uuid="2de0c925-32f9-40a1-affa-9888d62543e4">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="872c9ba9-efc3-4b73-9ed7-4058729eef27"/>
</reportElement>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="20" isBold="true"/>
<paragraph lineSpacing="Double"/>
</textElement>
<text><![CDATA[订单数量]]></text>
</staticText>
<staticText>
<reportElement stretchType="ContainerHeight" x="564" y="0" width="188" height="41" forecolor="#2838C9" backcolor="#F26B30" uuid="6b1ea5d3-9614-4f38-b3c8-db70e5d46574">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="d35e0dbd-5063-4434-9924-1b4526f24796"/>
</reportElement>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="20" isBold="true"/>
<paragraph lineSpacing="Double"/>
</textElement>
<text><![CDATA[订单总价]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="35" splitType="Stretch">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
<printWhenExpression><![CDATA[(($V{REPORT_COUNT}.intValue())%2)==0]]></printWhenExpression>
<textField>
<reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="188" height="35" isPrintInFirstWholeBand="true" printWhenGroupChanges="id" forecolor="#9E2624" backcolor="#570D0C" uuid="cd28bc1c-9d2f-492b-ba41-8741f153063d">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="f660a072-4c3b-4d9e-a045-ab75c05be0ed"/>
<printWhenExpression><![CDATA[(($V{REPORT_COUNT}.intValue())%2)==0]]></printWhenExpression>
</reportElement>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="14"/>
<paragraph lineSpacing="AtLeast"/>
</textElement>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="188" y="0" width="188" height="35" uuid="faed5324-62af-4bba-98a5-bf1c22a8c2b7">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="c87ead29-a587-4fd2-95db-cec89cda4eea"/>
</reportElement>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="376" y="0" width="188" height="35" uuid="f3704ffb-042b-4ad9-9598-3b52661e9e91">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="872c9ba9-efc3-4b73-9ed7-4058729eef27"/>
</reportElement>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{num}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="564" y="0" width="188" height="35" uuid="1fe002b5-2e28-4b4d-823c-899c20e05d98">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="d35e0dbd-5063-4434-9924-1b4526f24796"/>
</reportElement>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{sum}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="48" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="376" height="30" isPrintInFirstWholeBand="true" printWhenGroupChanges="id" backcolor="#331110" uuid="d4400682-d80f-43fc-865d-c11697cbfdf2"/>
<box>
<pen lineWidth="1.0" lineStyle="Solid" lineColor="#070808"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="20" isBold="true"/>
</textElement>
<text><![CDATA[合计]]></text>
</staticText>
<textField>
<reportElement x="376" y="0" width="188" height="30" isPrintInFirstWholeBand="true" printWhenGroupChanges="id" backcolor="#331110" uuid="f4dc9dad-a96f-4b21-a00e-2788e01b61ae"/>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$V{total_num}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="564" y="0" width="188" height="30" isPrintInFirstWholeBand="true" printWhenGroupChanges="id" backcolor="#331110" uuid="f83f41bd-c283-4b16-8165-e552836e0a95"/>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font fontName="myfont" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$V{total_sum}]]></textFieldExpression>
</textField>
</band>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>