UReport2是一款高性能的架构在Spring之上纯Java报表引擎,通过迭代单元格可以实现任意复杂的中国式报表。在UReport2中,提供了全新的基于网页的报表设计器,可以在Chrome、Firefox、Edge等各种主流浏览器运行(IE浏览器除外),打开浏览器即可完成各种复杂报表的设计制作。
UReport2是第一款基于Apache-2.0协议开源的中式报表引擎。
安装与配置
- 创建一个maven项目,在pom.xml中添加依赖
<dependency>
<groupId>com.bstek.ureport</groupId>
<artifactId>ureport2-console</artifactId>
<version>[version]</version>
</dependency>
- 配置ureport需要的servlet
@ImportResource("classpath:context.xml")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
/**
* "/ureport/*" 固定写法
* @return
*/
@Bean
public ServletRegistrationBean buildUreportServlet(){
return new ServletRegistrationBean(new UReportServlet(),"/ureport/*");
}
}
- 如果将UReport配置到已经存在的spring项目中,同时spring也要加载自己的properties文件,需要进行如下配置:
在src->main->resources 下创建一个context.properties文件,并且在application类加注解@ImportResource(“classpath:context.xml”)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="classpath:ureport-console-context.xml"/>
<!-- 引入配置文件 -->
<bean id="propertyConfigurer" parent="ureport.props">
<property name="location">
<!-- spring项目的配置文件 -->
<value>classpath:application.yml</value>
</property>
</bean>
</beans>
- 需要加载的自己的properties文件如下ureport.properties
#文件的保存路径 随便配置
ureport.fileStoreDir=E:/web/ureportfiles
ureport.disableFileProvider=false
ureport.debug=true
ureport.disableHttpSessionReportCache=false
到这里,我们就在一个标准的 Maven 项目中加入了 UReport2,运行项目,在浏览器中访问 URL:http://localhost:8080/ureport-demo/ureport/designer,就可以看到 UReport2 的报表设计器界面。
报表保存以及数据源配置
- 数据库数据源
点测试连接 提示成功则保存,就可以选择数据库中的某个表把数据取出来。 - spring bean数据集
创建一个bean类
@Component
public class TestBean {
/**
* @param dsName 数据源名称
* @param datasetName 数据集名称
* @param parameters 外部传入的map
* @return
*/
public List<Map<String,Object>> loadReportData(String dsName,String datasetName,Map<String,Object> parameters){
return null;
}
public List<User> buildReport(String dsName,String datasetName,Map<String,Object> parameters){
return null;
}
}
添加数据集的时候选择方法就会弹出上面两个在代码中定义的方法名,返回对象如果指定返回对象需要使用全路径即可
- 内置数据源
@Component
public class TestInDataSource implements BuildinDatasource {
@Autowired
private DataSource dataSource;
@Override
public String name() {
return "cluster";
}
@Override
public Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
log.println("========"+throwables.getMessage());
}
return null;
}
}
如果报DataSource为空,在pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>