文章目录
- 前言
- 一、开发工具下载安装
- 二、开始制作
- 1.新建
- 1.文本
- 2.图片
- 3. 表格
- 4.时间
- 三.遇到的问题
- 1.中文不显示
- 2.detail模块与column Footer之间有空白。怎么调节也消不掉
- 四.完整代码
- 总结
前言
公司最近要做报销系统。需求就是将报销申请、报销审批。并将报销信息打印出来。
javabean 作为数据源。最后面会有完整代码。
因为之前也没有学过这个方面的。都是网上看了,但是看太多了,分不清哪个是哪个了。所以以下内容都是摘超别人的。汇总之后自己按照需求做出来。整理的
效果图:
一、开发工具下载安装
- 官网地址
官网
- 正常安装即可
- 了解下页面,用过eclipse的大概都能看明白。
1)项目列表
2)页面层级结构
3)制作报表的页面
4)第一个按钮是页面,第二个源代码,xml ,第三个是预览。点击能显示完成的的结果
5)组件,拖拽到主页面就能增加了
6)组件的属性。可以调整页面大小。组件边框等等。
二、开始制作
1.新建
- 新建项目 File->new->project
- 新建文件 File->new->jasper Report
1.文本
-
静态文本(不需要外部传入参数的)
拖拽static text即可。 文本内容用空格分割了。 右下的框是调整大小。用于微调。
下面的线是用line拖拽即可。
上面或者属性中调整文本位置。上下左右居中。
设置边框
设置文字纵向排列,文字间用table。设置左边距(我也是研究的,大概这个意思)
-
动态文本
将1拖拽到2位置 调整大小。
在parameter右键 create Parameter。 输入属性、字段类型
点击2,选择字段注意:属性小写,为了统一所有都是小写。
2.图片
跟文本一样操作。拖拽,创建parameter,字段类型字符串即可。
代码赋值为 外网可访问的地址 比如用文件存储oss、minio等。
图片转为base64的也可以。我看网上很多都是,但是我试了很多次,没有弄出来。这个项目有文件存储,所以不浪费时间。
parameters.put("department_leader_img", "http://10.10.10.31:80/api/minio/test//aa50254ba99940a894e8d3ea0f722f6c.jpg");
举个例子,拖拽,双击text field选择即可
3. 表格
正常的表格是使用table组件完成。 网上有很多教程。我最开始也用的是table。但是又合并又省事 直接自己拼接
与上面不同的是。表格的头放到column header块中。表格信息放到detail中。
detail会将接收的列表进行循环展示
4.时间
这里做的时间就是传值。还可以用系统时间。
三.遇到的问题
1.中文不显示
-
选择你用的字体打成jar包
找出你要用的字体。win字体在
C:\Windows\Fonts
比如我用的华文, 复制到
D:\fonts
点击project->properties.找到fonts->Configure workspace settings->add->添加->填写按照下图中的。->finish->点击export 会导出jar包
-
将jar包引入到项目中
maven 安装jar 或者可以扔到远程仓库 直接引用。
mvn install:install-file -Dfile=D:\fonts\font.jar -DgroupId=site.yunqi.report -DartifactId=font -Dversion=1.0.0 -Dpackaging=jar
pom引用
<dependency>
<groupId>site.yunqi.report</groupId>
<artifactId>font</artifactId>
<version>1.0.0</version>
</dependency>
jar包名称小写,我试了大写不好用
2.detail模块与column Footer之间有空白。怎么调节也消不掉
点击空白处,勾选
四.完整代码
目录
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.16</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>jaspe</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jaspe</name>
<description>jaspe</description>
<properties>
<java.version>1.8</java.version>
<hutool.version>5.7.16</hutool.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- SpringBoot Web容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- JasperReports 报表开发所需依赖 START -->
<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.20.0</version>
<exclusions>
<!--
排除自带的itext依赖,因为自带的itext版本是 2.1.7.js10
这个版本在中央仓库里面没有,无法下载
-->
<exclusion>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入itext依赖,因为JasperReports中使用了itext操作PDF -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>site.yunqi.report</groupId>
<artifactId>font</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.21</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
打包将文件放到resource下的jaspers中
工具类。这个是抄别人的
package com.example.jaspe.utils;
import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.export.HtmlExporterOutput;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleHtmlReportConfiguration;
import org.springframework.core.io.ClassPathResource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
/**
* Author: xyyz150
* Date: 2020/4/9 19:40
* Description:
*/
@Slf4j
public class JasperReportUtil {
final static String jasperDir = "jaspers";
public static String getJasperFileDir(String fileName) {
return jasperDir + File.separator + fileName + ".jasper";
}
private static String getContentType(ReportType type) {
String contentType;
switch (type) {
case HTML:
contentType = "text/html;charset=utf-8";
break;
case PDF:
contentType = "application/pdf";
break;
case XLS:
contentType = "application/vnd.ms-excel";
break;
case XLSX:
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case XML:
contentType = "text/xml";
break;
case RTF:
contentType = "application/rtf";
break;
case CSV:
contentType = "text/plain";
break;
case DOC:
contentType = "application/msword";
break;
default:
contentType = "text/html;charset=utf-8";
}
return contentType;
}
static JasperPrint getJasperPrint(InputStream jasperStream, Map parameters, List<?> list) throws JRException {
JRDataSource dataSource = null;
if (null == list || list.size() == 0) {
dataSource = new JREmptyDataSource();
} else {
dataSource = new JRBeanCollectionDataSource(list);
}
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperStream, parameters, dataSource);
return jasperPrint;
}
public static void exportToPdf(String jasperPath, Map parameters, List<?> list, HttpServletResponse response) throws Exception {
OutputStream outputStream = response.getOutputStream();
try {
ClassPathResource resource = new ClassPathResource(jasperPath);
response.setContentType(getContentType(ReportType.PDF));
InputStream jasperStream = resource.getInputStream();
JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
} catch (Exception e) {
log.error("读取报表异常", e);
outputStream.write("读取报表异常".getBytes());
} finally {
outputStream.flush();
outputStream.close();
}
}
public static void exportToPdfFile(String jasperPath, Map parameters, List<?> list, HttpServletResponse response) throws Exception {
try {
ClassPathResource resource = new ClassPathResource(jasperPath);
response.setContentType(getContentType(ReportType.PDF));
InputStream jasperStream = resource.getInputStream();
JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
JasperExportManager.exportReportToPdfFile(jasperPrint, "/pdf/pdf导出" + ".pdf");
} catch (Exception e) {
log.error("读取报表异常", e);
}
}
public static void exportToXml(String jasperPath, Map parameters, List<?> list, HttpServletResponse response) throws Exception {
OutputStream outputStream = response.getOutputStream();
try {
ClassPathResource resource = new ClassPathResource(jasperPath);
response.setContentType(getContentType(ReportType.XML));
InputStream jasperStream = resource.getInputStream();
JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
JasperExportManager.exportReportToXmlStream(jasperPrint, outputStream);
} catch (Exception e) {
log.error("读取报表异常", e);
outputStream.write("读取报表异常".getBytes());
} finally {
outputStream.flush();
outputStream.close();
}
}
public static void exportToHtml(String jasperPath, Map parameters, List<?> list, HttpServletResponse response) throws Exception {
response.setHeader("Content-type", "text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType(getContentType(ReportType.HTML));
PrintWriter out = response.getWriter();
HtmlExporter exporter = new HtmlExporter();
try {
ClassPathResource resource = new ClassPathResource(jasperPath);
InputStream jasperStream = resource.getInputStream();
JasperPrint jasperPrint = getJasperPrint(jasperStream, parameters, list);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlReportConfiguration configuration = new SimpleHtmlReportConfiguration();
exporter.setConfiguration(configuration);
HtmlExporterOutput outPut = new SimpleHtmlExporterOutput(out);
exporter.setExporterOutput(outPut);
exporter.exportReport();
} catch (Exception e) {
log.error("读取报表异常", e);
out.write("读取报表异常");
} finally {
out.flush();
out.close();
}
}
public enum ReportType {
HTML,
PDF,
XLS,
XLSX,
XML,
RTF,
CSV,
DOC
}
}
bean
package com.example.jaspe.entity;
import lombok.Data;
/**
* @author sunhl
*/
@Data
public class ReimSpecialCostInfo {
private String task_name;
private String reim_content;
private String cost_type;
private String bill_type;
private String untaxed_amount;
private String taxes;
private String amount_includ_tax;
private String department;
}
测试类
package com.example.jaspe.controller;
import com.example.jaspe.entity.ReimSpecialCostInfo;
import com.example.jaspe.entity.User;
import com.example.jaspe.utils.JasperReportUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @author sunhl
*/
@RestController
@RequestMapping("/api/report")
public class Test {
@GetMapping("/test")
public void exportFile(String format, HttpServletResponse response) throws Exception {
HashMap<String, Object> parameters = new HashMap<String, Object>();
List<ReimSpecialCostInfo> dataReim = this.getDataReim(3);
parameters.put("id", "ZX23100901");
parameters.put("time", "2023年10月12日");
parameters.put("department", "开发部");
parameters.put("project_id", "XMQR23042801");
parameters.put("persion_name", "孙xx");
parameters.put("persion_blank", "沈阳南湖科技开发区支行");
parameters.put("persion_account", "6214856704789696");
parameters.put("reim_num", "258");
parameters.put("reim_string", "报销金额: 拾 万 / 仟 贰 佰 伍 拾 捌 元 角 分");
parameters.put("company_leader", "李xx");
parameters.put("department_leader", "李xx");
parameters.put("department_leader_img", "http://10.10.10.31:80/api/minio/test//aa50254ba99940a894e8d3ea0f722f6c.jpg");
parameters.put("applicant", "孙xx");
parameters.put("accounting", "祁x");
String jasperPath = JasperReportUtil.getJasperFileDir("test");
if (format.equals("pdf")) {
JasperReportUtil.exportToPdf(jasperPath, parameters, dataReim, response);
} else if (format.equals("html")) {
JasperReportUtil.exportToHtml(jasperPath, parameters, dataReim, response);
}
}
private List<User> getData(int num) {
List<User> ansMap = new ArrayList<>();
for (int i = 0; i < num; i++) {
User user = new User();
user.setUserName("userName_00" + i);
user.setAge(i);
user.setId(i);
ansMap.add(user);
}
return ansMap;
}
private List<ReimSpecialCostInfo> getDataReim(int num) {
List<ReimSpecialCostInfo> ansMap = new ArrayList<>();
num = Math.min(num, 4);
for (int i = 0; i < num; i++) {
ReimSpecialCostInfo info = new ReimSpecialCostInfo();
info.setTask_name("任务名称_" + i);
info.setReim_content("报销内容说明_" + i);
info.setCost_type("费用类别_" + i);
info.setBill_type("发票类型_" + i);
info.setUntaxed_amount("未税金额_" + i);
info.setTaxes("税金_" + i);
info.setAmount_includ_tax("含税金额_" + i);
ansMap.add(info);
}
ansMap.add(new ReimSpecialCostInfo());
ansMap.add(new ReimSpecialCostInfo());
return ansMap;
}
}
总结
先写到这里,后续遇到问题在补充。有问题评论叫我