Springboot 集成 Ureport2 导出Excel报表、生成PDF文件

作者: adm 分类: java 发布时间: 2023-10-13

1.ureport 介绍:
UReport2是一款高性能的架构在Spring之上纯Java报表引擎,通过迭代单元格可以实现任意复杂的中国式报表。
在UReport2中,提供了全新的基于网页的报表设计器,可以在Chrome、Firefox、Edge等各种主流浏览器运行(IE浏览器除外),打开浏览器即可完成各种复杂报表的设计制作。

UReport2是第一款基于Apache-2.0协议开源的中式报表引擎。

文档视频教程地址:
BSDN WIKI: http://wiki.bsdn.org/display/UR/ureport2+Home
w3cschool: https://www.w3cschool.cn/ureport
2. 如何在springboot 项目中实现各种报表导出、PDF文件导出

添加 springboot、ureport、数据库相关的依赖 pom.xml

SpringBootDemo
org.springboot.demo
1.0-SNAPSHOT
4.0.0
ureport


org.springframework.boot
spring-boot
2.1.6.RELEASE


org.springframework.boot
spring-boot-autoconfigure
2.1.6.RELEASE


junit
junit
4.11
test


com.bstek.ureport
ureport2-console
2.2.9


org.springframework
spring-context
5.3.20


commons-configuration
commons-configuration
1.8


org.springframework.boot
spring-boot-starter-web
2.1.6.RELEASE


org.mybatis
mybatis
3.5.6


org.mybatis
mybatis-spring
2.0.6



mysql
mysql-connector-java
8.0.21



com.alibaba
druid
1.2.3



org.slf4j
slf4j-log4j12
1.6.6


log4j
log4j
1.2.16


org.slf4j
jcl-over-slf4j
1.6.6


org.projectlombok
lombok
1.16.22

spring-releases
Spring Releases
https://repo.spring.io/libs-release
8
8

2.2添加yml配置信息
application.yml:
server:
port: 8090
servlet:
context-path:
spring:
http:
encoding:
force: true
enabled: true
charset: UTF-8
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true&nullNamePatternMatchesAll=true
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource

resources:
static-locations: classpath:/,classpath:/static/

2.3添加引用UReport2的Spring配置文件context.xml
放在resources




classpath:config.properties

2.4添加property文件
config.properties

#配置文件系统对应的报表文件地址
ureport.fileStoreDir=D:/work/ForTest/SpringBootDemo/ureport/src/main/webapp/WEB-INF/ureportfiles
# 是否禁用
ureport.disableFileProvider=false
ureport.debug=true
ureport.disableHttpSessionReportCache=false
# 配置ureport根路径
ureport.contextPath=/ureport
2.5新建webapp目录,新建WEB-INF
在WEB-INF下新建ureportfiles目录存放报表模板文件

2.7 创建启动类
Application.java
package org.springboot.demo;

import com.bstek.ureport.console.UReportServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(“classpath:context.xml”)
public class Application {
public static void main(String[] args) {

try {
SpringApplication.run(Application.class, args);
}catch (Exception e){
System.out.println(e.getMessage());
}
}
// ureport2使用到servlet
@Bean
public ServletRegistrationBean buildUReportServlet(){
return new ServletRegistrationBean(new UReportServlet(),”/ureport/*”);
}
}

执行启动 点击Application.java 右键启动项目

浏览器输入:http://localhost:8090/ureport/designer

就可以访问到ureport 模板设计页面了

2.8 新建 ureport 数据源类
新建 ureportSource.java
package org.springboot.demo.util;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component(“ureportSource”)
public class ureportSource {
/**
*
* @param dsName
* @param datasetName
* @param parameters
* @return
*/
public List> usrInfo(String dsName, String datasetName, Map parameters) {
List> mapList = new ArrayList<>();
mapList.add(parameters);
return mapList;
}
}

新建实体类

user.java

package org.springboot.demo.entity;

import lombok.Data;
import java.io.Serializable;
@Data
public class user implements Serializable{
private static final long serialVersionUID = -1518203115685933730L;
private String userName;
private String birthDate;
private String sex;
private String school;
private String grade;
private Integer age;
private String address;
}
再次 重启项目
记得一定要重启,或者热部署

2.9 配置ureport数据源
ureport数据源 有三种:直连数据源、SpringBean连接、内置数据源

这个优先使用SpringBean连接,安全性高,使用简单

这里配置SpringBean连接、点击中间这个按钮

2.9.1新建数据源

Bean ID是我们建的那个java类的类名,这个需要对应上

2.9.2 添加数据集

确定后,点击userInfo 刷新,就可以看到实体类的属性了

2.9.3设计模板

分别给每个表格赋值

属性名称,使用“普通文本” 类型

属性值,使用“数据集” 类型

所有属性和值设置好后,点击保存

保存后,我们可以看到工程下生成了一个报表模板文件

3.0 新建导出excel、pdf公共类
ExportUtils.java

package org.springboot.demo.util;

import com.bstek.ureport.export.ExportConfigureImpl;
import com.bstek.ureport.export.ExportManager;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Objects;
/**
导出excel、pdf
*/
public class ExportUtils {
public static void exportPdf(ExportManager exportManager, String sourcePath, String targetPath, Map param) throws Exception {
try {
OutputStream fos = new FileOutputStream(targetPath);
try {
ExportConfigureImpl exportConfigure = new ExportConfigureImpl(sourcePath, param, fos);
exportManager.exportPdf(exportConfigure);
} catch (Exception e) {
throw new Exception(“exportPdf error”, e);
} finally {
if (fos != null) {
try {
fos.close();
}catch(Exception e) {
throw new Exception(“exportPdf error”, e);
}
}
}
} catch (Exception e) {
throw new Exception(“exportPdf error”, e);
}
}
public static void exportExcel(ExportManager exportManager, String sourcePath, String targetPath, Map param) throws Exception {
try {
OutputStream fos = new FileOutputStream(targetPath);
try {
String ext = targetPath.substring(targetPath.indexOf(“.”) + 1);
ExportConfigureImpl exportConfigure = new ExportConfigureImpl(sourcePath, param, fos);
if (Objects.equals(ext, “xls”)) {
exportManager.exportExcel97(exportConfigure);
} else {
if (!Objects.equals(ext, “xlsx”)) {
throw new Exception(“File name is not support!”);
}
exportManager.exportExcel(exportConfigure);
}
} catch (Exception e) {
throw new Exception(“exportExcel error”, e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
throw new Exception(“exportExcel error”, e);
}
}
}
} catch (Exception e) {
throw new Exception(“exportExcel error”, e);
}
}

}

3.1 新建测试类
urePortController.java
package org.springboot.demo.controller;

import com.bstek.ureport.export.ExportManager;

import lombok.SneakyThrows;

import org.springboot.demo.util.ExportUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(“/user”)
public class urePortController {
@Autowired
private ExportManager exportManager;

@SneakyThrows
@RequestMapping(“/generatePDF”)
public Map generatePDF(){
Map dataMap = new HashMap<>();
dataMap.put(“userName”,”张三”);
dataMap.put(“age”,”20″);
dataMap.put(“sex”,”男”);
dataMap.put(“grade”,”大三”);
dataMap.put(“birthDate”,”2003-01-01″);
dataMap.put(“address”,”广州市天河区”);
dataMap.put(“school”,”广东工业大学”);
String filePath=”D:/userInfo.pdf”;
ExportUtils.exportPdf(exportManager,”file:userInfo.ureport.xml”,filePath,dataMap);
//返回页面信息
Map result = new HashMap<>();
result.put(“result”,”Success”);
result.put(“data”,dataMap);
return result;
}
@SneakyThrows
@RequestMapping(“/generateExcel”)
public Map generateExcel(){
Map dataMap = new HashMap<>();
dataMap.put(“userName”,”张三”);
dataMap.put(“age”,”20″);
dataMap.put(“sex”,”男”);
dataMap.put(“grade”,”大三”);
dataMap.put(“birthDate”,”2003-01-01″);
dataMap.put(“address”,”广州市天河区”);
dataMap.put(“school”,”广东工业大学”);
String filePath=”D:/userInfo.xls”;
ExportUtils.exportExcel(exportManager,”file:userInfo.ureport.xml”,filePath,dataMap);
//返回页面信息
Map result = new HashMap<>();
result.put(“result”,”Success”);
result.put(“data”,dataMap);
return result;
}

}

重启项目,浏览器输入 http://localhost:8090/user/generatePDF

生成PDF文件

浏览器输入 http://localhost:8090/user/generateExcel

生成表格

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!