Ireport5.6.0
依赖 jdk7 需要先安装jdk7
1. 下载安装
windows版
- Ireport
百度云盘:链接:https://pan.baidu.com/s/1Up4scDy_F9-rSC8I5DW6QA
提取码:ascy
阿里云盘:链接:https://www.aliyundrive.com/s/QmdjjDzVnMs
- jdk7
百度云盘:链接:https://pan.baidu.com/s/1FB1gtVVp_YadquUVuvMPtQ
提取码:mwyv
阿里云盘:链接:https://www.aliyundrive.com/s/bjbazD4xs1T
- 安装完成后修改 etc文件下的
ireport.conf
文件
- 添加中文的jar包
安装目录下\ireport\modules\ext\iTextAsian.jar
2. 使用 ireport
参考:B站Ireport视频
集成web应用
1. 报文模板
<?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="23c96341-fdfd-46f5-aa9b-709a4bd4d618">
<property name="ireport.zoom" value="1.3310000000000004"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="SQL">
</queryString>
<field name="id" class="java.lang.Integer">
</field>
<field name="name" class="java.lang.String">
</field>
<field name="phone" class="java.lang.String">
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="44" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="555" height="43" uuid="6e92a4c7-44bf-41b8-861d-b486b87c5905"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" size="25" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[用户信息]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="24" splitType="Stretch">
<staticText>
<reportElement x="137" y="0" width="100" height="23" uuid="f726c123-1d3a-45e3-9443-70b591fe5769"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" size="10" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[编号]]></text>
</staticText>
<staticText>
<reportElement x="237" y="0" width="100" height="23" uuid="df117bee-844e-49e0-928c-952033d86065"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" size="10" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[姓名]]></text>
</staticText>
<staticText>
<reportElement x="337" y="0" width="100" height="23" uuid="022007a3-8529-4019-b5c4-1f8a4f979880"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="宋体" size="10" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[手机号]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="137" y="0" width="100" height="20" uuid="d32a4cad-c9b1-4586-9a33-d4495ea48f58"/>
<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" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="337" y="0" width="100" height="20" uuid="383c1468-0e2c-4dcc-b8ba-a0399441903d"/>
<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" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{phone}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="237" y="0" width="100" height="20" uuid="baba669d-594e-4598-9c22-19dc67a9dd0f"/>
<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" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
</jasperReport>
1. springboot项目导入依赖
这里只放了 ireport相关的依赖,其它web相关的依赖自己导入吧
<!--========== ireport报表相关 start=============-->
<!-- 该包的作用完成ireport中Preview按扭功能。通过该包可以通过java来ireport生成摸班(.jrxml或.jasper)填充数据源并导出pdf,excel,html等格式文件 -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<!-- 生成pdf所依赖的包 -->
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--将.jrxml编译成.jasper.如果你不使用java编译,而使用ireport工具编译则无须导入该 包 -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.13</version>
</dependency>
<!--========== ireport报表相关 end=============-->
2. resource
resource下创建 ireport 目录,用于存放自己画的报表
3. 工具类
@Slf4j
public class JasperHelper {
/**
* @param response httpServletResponse
* @param fileName 显示/保存 的文件名,不需要(.pdf)后缀
* @param jrxmlTemplate 报表模板位置如:ireport/report1.jrxml
* @param data Fields 数据集合
* @param params 其它 params参数
* @param downLoad 是否下载 true:下载 false:在线预览
*/
public static void exportPDF(HttpServletResponse response, String fileName, String jrxmlTemplate, List<?> data, Map<String,Object> params, boolean downLoad) throws IOException, JRException {
// 1. 获取数据源
JRDataSource jrDataSource = new JRBeanCollectionDataSource(data);
// 2. 获取模板文件
File jasperFile = ResourceUtils.getFile("classpath:" + jrxmlTemplate);
// 3. 编译 jrxml
JasperReport jasperReport = JasperCompileManager.compileReport(jasperFile.getPath());
// 4. 获取报表
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, jrDataSource);
// 通过outputStream输出
JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
// 5. 设置响应格式
response.setCharacterEncoding("utf-8");
response.setContentType("application/pdf");
if(downLoad){
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".pdf");
}else{
response.setHeader("content-disposition", "inline;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".pdf");
}
}
}
4. controller
@GetMapping("/test")
public void print(HttpServletResponse response) throws Exception {
// 模拟数据
List<Map<String,Object>> data = new ArrayList<>();
Map<String,Object> map = new HashMap<>();
map.put("id",1);
map.put("name","张三");
map.put("phone","123456");
data.add(map);
JasperHelper.exportPDF(response,"用户数据表","ireport/report1.jrxml",data,null,false);
}