目錄
1.ureport 介紹:
? ? 文檔視頻教程地址:
2. 如何在springboot 項(xiàng)目中實(shí)現(xiàn)各種報(bào)表導(dǎo)出、PDF文件導(dǎo)出
2.1使用IDEA創(chuàng)建maven工程?
?2.2添加yml配置信息
application.yml:
2.3添加引用UReport2的Spring配置文件context.xml?
2.4添加property文件
2.5新建webapp目錄,新建WEB-INF
2.7 創(chuàng)建啟動(dòng)類?
?2.8 新建 ureport 數(shù)據(jù)源類
再次 重啟項(xiàng)目
?2.9 配置ureport數(shù)據(jù)源
2.9.1新建數(shù)據(jù)源
?2.9.2 添加數(shù)據(jù)集
2.9.3設(shè)計(jì)模板
3.0 新建導(dǎo)出excel、pdf公共類
3.1 新建測(cè)試類
1.ureport 介紹:
? ? ? ?UReport2是一款高性能的架構(gòu)在Spring之上純Java報(bào)表引擎,通過迭代單元格可以實(shí)現(xiàn)任意復(fù)雜的中國式報(bào)表。
? ? ? ?在UReport2中,提供了全新的基于網(wǎng)頁的報(bào)表設(shè)計(jì)器,可以在Chrome、Firefox、Edge等各種主流瀏覽器運(yùn)行(IE瀏覽器除外),打開瀏覽器即可完成各種復(fù)雜報(bào)表的設(shè)計(jì)制作。
? ? ? UReport2是第一款基于Apache-2.0協(xié)議開源的中式報(bào)表引擎。
? ? 文檔視頻教程地址:
- BSDN WIKI:?http://wiki.bsdn.org/display/UR/ureport2+Home
- w3cschool:?https://www.w3cschool.cn/ureport
2. 如何在springboot 項(xiàng)目中實(shí)現(xiàn)各種報(bào)表導(dǎo)出、PDF文件導(dǎo)出
2.1使用IDEA創(chuàng)建maven工程?
打開idea,New Project選擇Maven Archetype ,創(chuàng)建web工程
右鍵新建Module?
?
? ? 工程是這樣的:? ?
?設(shè)置一下工程:
?pom.xml變灰了,需要設(shè)置一下,把這個(gè)勾選去掉,點(diǎn)擊“應(yīng)用”
?添加 springboot、ureport、數(shù)據(jù)庫相關(guān)的依賴 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驅(qū)動(dòng)包 會(huì)自動(dòng)添加4個(gè)對(duì)應(yīng)包 -->
<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: 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
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 創(chuàng)建context.xml是第六步,引入ureport2報(bào)表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
#配置文件系統(tǒng)對(duì)應(yīng)的報(bào)表文件地址
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目錄存放報(bào)表模板文件
2.7 創(chuàng)建啟動(dòng)類?
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/*");
}
}
?執(zhí)行啟動(dòng) 點(diǎn)擊Application.java 右鍵啟動(dòng)項(xiàng)目
?瀏覽器輸入:http://localhost:8090/ureport/designer
就可以訪問到ureport 模板設(shè)計(jì)頁面了
?2.8 新建 ureport 數(shù)據(jù)源類
新建 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;
}
}
新建實(shí)體類
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;
}
再次 重啟項(xiàng)目
記得一定要重啟,或者熱部署
?2.9 配置ureport數(shù)據(jù)源
ureport數(shù)據(jù)源 有三種:直連數(shù)據(jù)源、SpringBean連接、內(nèi)置數(shù)據(jù)源
這個(gè)優(yōu)先使用SpringBean連接,安全性高,使用簡(jiǎn)單
這里配置SpringBean連接、點(diǎn)擊中間這個(gè)按鈕
2.9.1新建數(shù)據(jù)源
Bean ID是我們建的那個(gè)java類的類名,這個(gè)需要對(duì)應(yīng)上
?2.9.2 添加數(shù)據(jù)集
?
確定后,點(diǎn)擊userInfo 刷新,就可以看到實(shí)體類的屬性了
2.9.3設(shè)計(jì)模板
分別給每個(gè)表格賦值?
屬性名稱,使用“普通文本”?類型
屬性值,使用“數(shù)據(jù)集” 類型
所有屬性和值設(shè)置好后,點(diǎn)擊保存
保存后,我們可以看到工程下生成了一個(gè)報(bào)表模板文件
3.0 新建導(dǎo)出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;
/**
導(dǎo)出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 新建測(cè)試類
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<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","廣州市天河區(qū)");
dataMap.put("school","廣東工業(yè)大學(xué)");
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","廣州市天河區(qū)");
dataMap.put("school","廣東工業(yè)大學(xué)");
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;
}
}
?重啟項(xiàng)目,瀏覽器輸入?http://localhost:8090/user/generatePDF
?生成PDF文件
?
瀏覽器輸入?http://localhost:8090/user/generateExcel
?生成表格
這個(gè)就是最簡(jiǎn)單的入門教程,希望大家喜歡!有問題歡迎指出!?文章來源:http://www.zghlxwxcb.cn/news/detail-719600.html
下一篇 :JAVA 給PDF添加水印文章來源地址http://www.zghlxwxcb.cn/news/detail-719600.html
到了這里,關(guān)于Springboot 集成 Ureport2 導(dǎo)出Excel報(bào)表、生成PDF文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!