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

2023-10-28 15:59

本文主要是介绍Springboot 集成 Ureport2 导出Excel报表、生成PDF文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1.ureport 介绍:

    文档视频教程地址:

2. 如何在springboot 项目中实现各种报表导出、PDF文件导出

2.1使用IDEA创建maven工程 

 2.2添加yml配置信息

application.yml:

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

2.4添加property文件

2.5新建webapp目录,新建WEB-INF

2.7 创建启动类 

 2.8 新建 ureport 数据源类

再次 重启项目

 2.9 配置ureport数据源

2.9.1新建数据源

 2.9.2 添加数据集

2.9.3设计模板

3.0 新建导出excel、pdf公共类

3.1 新建测试类


1.ureport 介绍:

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

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

    文档视频教程地址:

  1. BSDN WIKI: http://wiki.bsdn.org/display/UR/ureport2+Home
  2. w3cschool: https://www.w3cschool.cn/ureport

2. 如何在springboot 项目中实现各种报表导出、PDF文件导出

2.1使用IDEA创建maven工程 

打开idea,New Project选择Maven Archetype ,创建web工程

右键新建Module 

 

    工程是这样的:   

 设置一下工程:

 pom.xml变灰了,需要设置一下,把这个勾选去掉,点击“应用”

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

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>SpringBootDemo</artifactId><groupId>org.springboot.demo</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ureport</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot</artifactId><version>2.1.6.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.1.6.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>com.bstek.ureport</groupId><artifactId>ureport2-console</artifactId><version>2.2.9</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.20</version></dependency><dependency><groupId>commons-configuration</groupId><artifactId>commons-configuration</artifactId><version>1.8</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.1.6.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.21</version></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.3</version></dependency><!--slf4j-Log4j驱动包  会自动添加4个对应包 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.6</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>1.6.6</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.22</version></dependency></dependencies><pluginRepositories><pluginRepository><id>spring-releases</id><name>Spring Releases</name><url>https://repo.spring.io/libs-release</url></pluginRepository></pluginRepositories><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>

 2.2添加yml配置信息

application.yml:

server:port: 8090servlet:context-path:
spring:http:encoding:force: trueenabled: truecharset: UTF-8datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true&nullNamePatternMatchesAll=trueusername: rootpassword: 123456type: com.alibaba.druid.pool.DruidDataSourceresources:static-locations: classpath:/,classpath:/static/

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

放在resources

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- 创建context.xml是第六步,引入ureport2报表xml配置文件 --><import resource="classpath:ureport-console-context.xml" /><bean id="propertyConfigurer" parent="ureport.props"><property name="location"><value>classpath:config.properties</value></property></bean>
</beans>

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@Beanpublic 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<Map<String, Object>> usrInfo(String dsName, String datasetName, Map<String, Object> parameters) {List<Map<String, Object>> 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<String, Object> 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<String, Object> 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 {@Autowiredprivate ExportManager exportManager;@SneakyThrows@RequestMapping("/generatePDF")public Map<String, Object> generatePDF(){Map<String,Object> 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<String,Object> result = new HashMap<>();result.put("result","Success");result.put("data",dataMap);return result;}@SneakyThrows@RequestMapping("/generateExcel")public Map<String, Object> generateExcel(){Map<String,Object> 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<String,Object> result = new HashMap<>();result.put("result","Success");result.put("data",dataMap);return result;}}

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

 生成PDF文件

 

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

 生成表格

这个就是最简单的入门教程,希望大家喜欢!有问题欢迎指出! 

下一篇 :JAVA 给PDF添加水印

这篇关于Springboot 集成 Ureport2 导出Excel报表、生成PDF文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/294343

相关文章

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect