1.创建空的模板
文件 - NEW file
![在这里插入图片描述](https://img-blog.csdnimg.cn/66c426db6cd04a6e90e8edd45e34d9cf.png
2. 模板区域说明
- title 标题,只在第一页显示
- pageHeader 页眉,每一页都会显示
- columnHeader 列标题,每一页都会显示
- detail 1 列明细,一般为多条记录,循环显示,超过一页的默认分页显示
- columnFooter 列页脚,每一页都会显示并显示到页的最底部
- pageFooter 页脚,每一页都会显示并显示最底部,如果有lastPageFooter最后一页不显示
- lastPageFooter 最后一页页脚,只在最后一页显示并显示到页的最底部
- summary 摘要,在detail下显示,一般用来展示合计信息
3. 常用属性介绍
- styles 样式
- parameters 参数,内置部分参数,也可以自定义参数显示 $P{name}
- fields 字段,自定义参数显示 $F{name}
- variables 变量,内置部分变量 $V{name}
- dataset 数据源,可以使用jdbc连接,也可以使用自定义javaBean
- title 同名模板区域内容显示,标题,只在第一页显示
- pageHeader 同名模板区域内容显示,页眉,每一页都会显示
- columnHeader 同名模板区域内容显示,列标题,每一页都会显示
- detail 1 同名模板区域内容显示,列明细,一般为多条记录,循环显示,超过一页的默认分页显示
- columnFooter 同名模板区域内容显示,列页脚,每一页都会显示并显示到页的最底部
- pageFooter 同名模板区域内容显示,页脚,每一页都会显示并显示最底部,如果有lastPageFooter最后一页不显示
- lastPageFooter 同名模板区域内容显示,最后一页页脚,只在最后一页显示并显示到页的最底部
- summary 同名模板区域内容显示,摘要,在detail下显示,一般用来展示合计信息
4. 常用的组件介绍
- image 图片,显示logo
- list 数组,显示明细数据
- textField 展示动态数据 $F{name}
- staticText 静态文本
5. 实现简单的模板
<?xml version="1.0" encoding="UTF-8"?>
<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="qpls_spareParts" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8c7022ae-28ee-4535-9227-8d7557015ea4">
<property name="ireport.zoom" value="1.7715610000000452"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="dataset1" uuid="3ac670f8-9fda-453e-864a-167bc73fba8d">
<field name="stuCode" class="java.lang.String"/>
<field name="stuName" class="java.lang.String"/>
<field name="amount" class="java.lang.String"/>
</subDataset>
<parameter name="logo" class="java.lang.String"/>
<parameter name="school" class="java.lang.String"/>
<parameter name="class" class="java.lang.String"/>
<parameter name="totalAmount" class="java.lang.String"/>
<parameter name="list" class="java.util.List"/>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="90" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement stretchType="RelativeToBandHeight" x="50" y="50" width="505" height="20" uuid="361a1be1-e186-4cc3-be81-a4a21b52b460"/>
<textElement verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression><![CDATA[$P{school}]]></textFieldExpression>
</textField>
<image>
<reportElement x="0" y="0" width="200" height="50" uuid="95dff8ea-9dea-4e95-be30-29d9782db079"/>
<imageExpression><![CDATA[$P{logo}]]></imageExpression>
</image>
<textField isBlankWhenNull="true">
<reportElement stretchType="RelativeToBandHeight" x="50" y="70" width="505" height="20" uuid="8f09cb02-d41d-4fde-95c5-1244ff811a60"/>
<textElement verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression><![CDATA[$P{class}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="555" height="50" uuid="1ab20e5a-e3e3-4dc3-8fae-3a4798140bd6"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" size="24" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[学生缴费单]]></text>
</staticText>
<staticText>
<reportElement x="0" y="50" width="50" height="20" uuid="5608445d-9f14-45a7-8f72-f2de7aedba2b"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[学校:]]></text>
</staticText>
<staticText>
<reportElement x="0" y="70" width="50" height="20" uuid="25ccb484-a213-4556-8291-e137d779479c"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[班级:]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="40" splitType="Stretch">
<staticText>
<reportElement stretchType="RelativeToBandHeight" x="0" y="20" width="25" height="20" isPrintWhenDetailOverflows="true" uuid="eb7ebf15-fa73-4834-960e-11297ec98dc4"/>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
<topPen lineWidth="1.0" lineStyle="Solid"/>
<leftPen lineWidth="1.0" lineStyle="Solid"/>
<bottomPen lineWidth="1.0" lineStyle="Solid"/>
<rightPen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[序次]]></text>
</staticText>
<staticText>
<reportElement stretchType="RelativeToBandHeight" x="25" y="20" width="120" height="20" isPrintWhenDetailOverflows="true" uuid="9ca114e8-ce87-42ea-abdc-d2eb437ed250"/>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
<topPen lineWidth="1.0" lineStyle="Solid"/>
<leftPen lineWidth="1.0" lineStyle="Solid"/>
<bottomPen lineWidth="1.0" lineStyle="Solid"/>
<rightPen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[学生编号]]></text>
</staticText>
<staticText>
<reportElement stretchType="RelativeToBandHeight" x="145" y="20" width="290" height="20" isPrintWhenDetailOverflows="true" uuid="d224cf2a-9b74-46ed-a558-370abb0172a3"/>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
<topPen lineWidth="1.0" lineStyle="Solid"/>
<leftPen lineWidth="1.0" lineStyle="Solid"/>
<bottomPen lineWidth="1.0" lineStyle="Solid"/>
<rightPen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[学生姓名]]></text>
</staticText>
<staticText>
<reportElement stretchType="RelativeToBandHeight" x="452" y="0" width="10" height="20" uuid="da291c77-48fb-4619-8553-e59c15e1d705"/>
<textElement verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[共]]></text>
</staticText>
<staticText>
<reportElement stretchType="RelativeToBandHeight" x="482" y="0" width="32" height="20" uuid="16793c56-8a7e-44d3-a672-6b1f54a4c261"/>
<textElement verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[页,第]]></text>
</staticText>
<textField>
<reportElement stretchType="RelativeToBandHeight" x="514" y="0" width="20" height="20" uuid="755bb1bc-bd9b-4f5a-a73b-dd2652e261f3"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="462" y="0" width="20" height="20" uuid="4b7c6a70-bb7a-4c1a-adb6-90f92b6f4dec"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
<staticText>
<reportElement stretchType="RelativeToBandHeight" x="534" y="0" width="21" height="20" uuid="a89d89fb-e8c3-4841-b955-9ab559ab334d"/>
<textElement verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[页]]></text>
</staticText>
<staticText>
<reportElement stretchType="RelativeToBandHeight" x="435" y="20" width="120" height="20" isPrintWhenDetailOverflows="true" uuid="0d181067-f98a-4375-a5a9-9dfea131503b"/>
<box>
<pen lineWidth="1.0" lineStyle="Solid"/>
<topPen lineWidth="1.0" lineStyle="Solid"/>
<leftPen lineWidth="1.0" lineStyle="Solid"/>
<bottomPen lineWidth="1.0" lineStyle="Solid"/>
<rightPen lineWidth="1.0" lineStyle="Solid"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[缴费金额]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<componentElement>
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="555" height="20" isPrintWhenDetailOverflows="true" uuid="cfb42d62-693a-478e-af49-429e1e8c1b37"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="dataset1" uuid="af2cfd10-ec8f-4648-8d53-8cd355702fdf">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{list})]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="20" width="555">
<textField>
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="25" height="20" isPrintWhenDetailOverflows="true" uuid="cbbd184f-e305-44b4-8d55-77e0115d179b"/>
<box>
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement stretchType="RelativeToBandHeight" x="25" y="0" width="120" height="20" isPrintWhenDetailOverflows="true" uuid="a78ac480-ffb7-4268-aa6b-095702d5b314"/>
<box>
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression><![CDATA[$F{stuCode}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToBandHeight" x="145" y="0" width="290" height="20" isPrintWhenDetailOverflows="true" uuid="70166dba-ab9d-4e97-90d5-97d226c005ff"/>
<box>
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression><![CDATA[$F{stuName}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement stretchType="RelativeToBandHeight" x="435" y="0" width="120" height="20" isPrintWhenDetailOverflows="true" uuid="f37b6ae6-add1-4bea-8a20-b5ad5a4d6e2f"/>
<box>
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression><![CDATA[$F{amount}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
<pageFooter>
<band height="50">
<staticText>
<reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="555" height="50" isPrintWhenDetailOverflows="true" uuid="8b665259-e2b9-4dbb-8b53-1811aa0583ab"/>
<box>
<pen lineWidth="0.0" lineStyle="Dashed"/>
<topPen lineWidth="0.0" lineStyle="Dashed"/>
<leftPen lineWidth="0.0" lineStyle="Dashed"/>
<bottomPen lineWidth="0.0" lineStyle="Dashed"/>
<rightPen lineWidth="0.0" lineStyle="Dashed"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font fontName="宋体" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[教务处]]></text>
</staticText>
</band>
</pageFooter>
<summary>
<band height="20" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement stretchType="RelativeToBandHeight" x="435" y="0" width="120" height="20" isPrintWhenDetailOverflows="true" uuid="361c793a-4eb5-4ace-9c01-7bb16b08f0ce"/>
<textElement verticalAlignment="Middle">
<font fontName="宋体" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression><![CDATA[$P{totalAmount}]]></textFieldExpression>
</textField>
<staticText>
<reportElement stretchType="RelativeToBandHeight" x="375" y="0" width="60" height="20" isPrintWhenDetailOverflows="true" uuid="e4ab8673-1f70-4564-ba64-5f93c1a6c4f7"/>
<textElement verticalAlignment="Middle">
<font fontName="宋体" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[合计:]]></text>
</staticText>
</band>
</summary>
</jasperReport>
6. 如何使用javaBean实现
6.1 parameters 的 list 类型为 java.util.List
6.2 detail1的组件list需要设置数据源 dataset1(初始化即可无需配置),然后将list参数如图配置在组件list内。
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{list})
6.3 java代码中,list的数据类型为 List<Map<String, Object>>,整体数据以Map<String, Object>传递。
//打印代码
public void print(HttpServletResponse response) {
Map<String, Object> parametersMap = new HashMap<>(4);
parametersMap.put("school", "某某学校");
parametersMap.put("class", "三年级二班");
parametersMap.put("totalAmount", 100);
parametersMap.put("logo", this.getClass().getResource("/jasper/123.png"));
List<Map<String, Object>> list = new ArrayList<>();
parametersMap.put("list", list);
for (int i = 0; i < 50; i++) {
Map<String, Object> map = new HashMap<>(3);
map.put("stuCode", "学生编码"+ i);
map.put("stuName", "学生姓名"+ i);
map.put("amount", i);
list.add(map);
}
String jasperPath = "jasper/report1.jasper";
InputStream jasperStream = null;
OutputStream outputStream = null;
try {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline;");
response.setCharacterEncoding("utf-8");
outputStream = response.getOutputStream();
ClassPathResource resource = new ClassPathResource(jasperPath);
jasperStream = resource.getInputStream();
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametersMap, new JREmptyDataSource());
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
outputStream.flush();
} catch (IOException | JRException e) {
e.printStackTrace();
} finally {
try {
if(null != jasperStream){
jasperStream.close();
}
if(null != outputStream){
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 导出excel
* @param parametersMap 参数
* @param response 响应
* @return: void
* @Author: wyd
* @Date: 2021/7/1 14:29
*/
public static void toExcel(Map<String, Object> parametersMap, String fileName, HttpServletRequest request, HttpServletResponse response){
InputStream jasperStream = null;
OutputStream outputStream = null;
try {
String jasperPath = "/jasper/report1_excel.jasper";
response.reset();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + ExcelExportUtil.encodeFileName(fileName + ".xlsx", request));
outputStream = response.getOutputStream();
ClassPathResource resource = new ClassPathResource(jasperPath);
jasperStream = resource.getInputStream();
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametersMap, new JREmptyDataSource());
File excel = File.createTempFile("qplsPrintExcelTemplate", ".xlsx");
JRXlsxExporter exporter = new JRXlsxExporter();
SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration();
conf.setWhitePageBackground(false);
conf.setDetectCellType(true);
exporter.setConfiguration(conf);
ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
exporter.setExporterInput(exporterInput);
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(excel.getAbsolutePath());
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
FileUtil.writeToStream(excel, outputStream);
outputStream.flush();
outputStream.close();
FileUtil.del(excel);
} catch (IOException | JRException e) {
e.printStackTrace();
} finally {
try {
if(null != jasperStream){
jasperStream.close();
}
if(null != outputStream){
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
7. 图片
8. 常用属性
8.1 pageformat 设置纸张,页边距
8.2 padding and borders 设置边框线
8.3 print when expression(打印时表达式)
设置是否隐藏,需要boolean值,$P{productCode}.equals( Object )
8.4 ignore pagination(忽略分页)
不分页,默认分页
8.5 blank when null(null时为空)
当参数值为null的时候显示空白
8.6 stretch with overflow (溢出时伸展)
截断,当内容过多单元格空间不足时,不显示多余内容。
9.常见问题
9.1 pdf打印时,不显示中文
9.2 导出excel单元格变形
可能原因:部分单元格内容过长,导致变形
解决办法:加长单元格或使用属性 stretch with overflow 截断