一、畫報表
報表需要畫的,不畫怎麼出自定義報表樣式呢,對吧?(* ̄︶ ̄)
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 合併列的複雜表頭報表