一、画报表
报表需要画的,不画怎么出自定义报表样式呢,对吧?(* ̄︶ ̄)
1.右键 delete 其他 band,保留 Title,Page Header,Detail,Page Footer。
2.parameters 右键 create parameter,创建以下参数,全部是 java.lang.String 。
绘制"静态文本",将 static text 拖入 Detail,创建以下"字段常量"。可以在 Detail 双击 static text 进行编辑。
3.从 outline 视图,拖动 $P{title} 到对应的Band中,居中对齐。其他的可以照图拖动到对应位置。
4.绘制"边框",选中一个对象,看 Properties 视图,点击 Borders 选项卡。
5.重要的设置字体来了!画布中每一个对象都要去 Static text 选项卡中设置字体,不然,无法正常显示中文!有些按钮,用过EXCEL的都知道。
6.结合"画布中常用工具",调整表格。
7.上面所画的是 jrxml,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<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="mapData" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9e664dc2-80df-4720-8782-7c9b53896b22">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="title" class="java.lang.String"/>
<parameter name="date" class="java.lang.String"/>
<parameter name="name" class="java.lang.String"/>
<parameter name="age" class="java.lang.String"/>
<parameter name="dept" class="java.lang.String"/>
<parameter name="gender" class="java.lang.String"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="80" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="555" height="80" uuid="111256bb-0683-4ff0-836a-36e1a143d632"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="华文宋体" size="16" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch">
<textField>
<reportElement x="380" y="0" width="100" height="34" uuid="04008b9e-e897-4c19-bd5e-e7beff265b64"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="60" splitType="Stretch">
<staticText>
<reportElement x="80" y="30" width="100" height="30" uuid="e941a1e1-4b10-421a-8cf5-5a6d25f40349"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="华文宋体" isBold="true"/>
</textElement>
<text><![CDATA[性别:]]></text>
</staticText>
<staticText>
<reportElement x="80" y="0" width="100" height="30" uuid="ba626480-6432-472b-b857-35caff80cea2"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="华文宋体" isBold="true"/>
</textElement>
<text><![CDATA[名称:]]></text>
</staticText>
<staticText>
<reportElement x="280" y="0" width="100" height="30" uuid="74e28280-2580-454d-8a45-4ce721aef9c7"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="华文宋体" isBold="true"/>
</textElement>
<text><![CDATA[年龄:]]></text>
</staticText>
<textField>
<reportElement x="180" y="0" width="100" height="30" uuid="863944ad-a1f1-4d4b-a5f6-8358b87d64a1"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="华文宋体"/>
</textElement>
<textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="180" y="30" width="100" height="30" uuid="e32bae6b-02e2-4a17-8f0c-7ffb7590a640"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="华文宋体"/>
</textElement>
<textFieldExpression><![CDATA[$P{gender}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="280" y="30" width="100" height="30" uuid="34983906-eb0c-49f9-89d6-0ec83a63fc5d"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="华文宋体" isBold="true"/>
</textElement>
<text><![CDATA[部门:]]></text>
</staticText>
<textField>
<reportElement x="380" y="0" width="100" height="30" uuid="2174ccde-5788-483c-851f-6244241de4de"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="华文宋体"/>
</textElement>
<textFieldExpression><![CDATA[$P{age}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="380" y="30" width="100" height="30" uuid="687de8fd-cf79-4c6e-87e4-033d43b1ee92"/>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font fontName="华文宋体"/>
</textElement>
<textFieldExpression><![CDATA[$P{dept}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="54" splitType="Stretch">
<staticText>
<reportElement x="470" y="0" width="100" height="54" uuid="a6224c18-92df-4ec4-a102-54a6a2bf1d71"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[1]]></text>
</staticText>
</band>
</pageFooter>
</jasperReport>
二、画布中常用工具
在画报表时,应用好这些工具,可以节省很多时间。
- 顺序工具
- 在容器工具中对齐
- 尺寸组件工具
- 容器大小工具
- 编排容器工具
三、编译
1.自动编译成 jasper
2.手动编译成 jasper,没有自动的好用,我是这么觉得。最终,我会用自动编译的 jasper去显示或打印。不过,jrxml 的改变,jasper 每次要重新生成。
JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);
四、运行显示
以PDF在浏览器上显示;controller 请求方法代码如下:
/**
* map填充报表
*
* @param response
* @throws IOException
*/
@GetMapping("/showReport")
public void showReport(HttpServletResponse response) throws IOException {
// JasperCompileManager 编译管理器
// JasperFillManager 填充管理器
// JRXmlLoader xml加载器
// JasperPrintManager 打印管理器
// JasperExportManager 导出管理器
// JasperRunManager 运行管理器
// ServletContext context = this.getServletConfig().getServletContext();
// map作为报表数据源
Map<String, Object> parameters = new HashMap<String, Object>(16);
parameters.put("title", "第一份 jasper report");
parameters.put("date", "2020-04-10");
parameters.put("name", "张三");
parameters.put("age", "18");
parameters.put("dept", "IT部门");
parameters.put("gender", "男");
// 编译jrxml文件,生成jasper文件
try {
String jrxml = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "reports/mapData.jrxml")
.getPath();
JasperCompileManager.compileReportToFile(jrxml);
} catch (FileNotFoundException | JRException e2) {
e2.printStackTrace();
}
// 引入jasper文件。由JRXML模板编译生成的二进制文件,用于代码填充数据
FileInputStream fileIn = null;
ServletOutputStream out = null;
try {
String jasperPath = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "reports/mapData.jasper")
.getPath();
fileIn = new FileInputStream(new File(jasperPath));
out = response.getOutputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
JasperRunManager.runReportToPdfStream(fileIn, out, parameters, new JREmptyDataSource());
response.setCharacterEncoding("utf-8");
response.setContentType("application/pdf");
} catch (JRException e) {
e.printStackTrace();
} finally {
out.flush();
out.close();
}
}
javaBean填充报表开发类似。
五、下期预告
- 展示分组报表
- 展示Table 合并列的复杂表头报表