1. 概述

UReport2 是一款高性能的架构在 Spring 之上纯 Java 报表引擎,通过迭代单元格可以实现任意复杂的中国式报表。

在 UReport2 中,提供了全新的基于网页的报表设计器,可以在 Chrome、Firefox、Edge 等各种主流浏览器运行(IE浏览器除外),打开浏览器即可完成各种复杂报表的设计制作。

2. 集成

2.1 Spring boot 引入 UReport

package com.redxun.ureport.config;

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

import javax.servlet.Servlet;

/**
 *  Ureport2 配置类
 * @author zhongwei
 */
@Configuration
@ImportResource("classpath:ureport-console-context.xml")
public class UreportConfig {

    @Bean
    public ServletRegistrationBean<Servlet> buildUreportServlet(){
        return new ServletRegistrationBean<Servlet>(new UReportServlet(), "/ureport/*");
    }

}

2.2 报表数据库的存储实现

说明:ureport默认是基于文件存储的,即在线设计报表时,其以文件方式存储于某个目录下,平台通过扩展实现,把在线的文件保存于数据库中,需要重新实现其文件存储的接口。

package com.redxun.ureport.provider;

import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import com.redxun.common.tool.BeanUtil;
import com.redxun.ureport.core.entity.UreportFile;
import com.redxun.ureport.core.service.UreportFileServiceImpl;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Mysql 报表存储
 * @author zhongwei
 *
 */
@Setter
@Component
@ConfigurationProperties(prefix = "ureport.db.provider")
public class DbSqlProvider implements ReportProvider {
    private static final String NAME = "db-provider";

    private String prefix = "db:";

    private boolean disabled;

    @Resource
    private UreportFileServiceImpl ureportFileService;

    @Override
    public InputStream loadReport(String file) {
        UreportFile ureportFile = ureportFileService.getByFileName(getCorrectName(file));
        if(BeanUtil.isEmpty(ureportFile)){
            return null;
        }
        byte[] content = ureportFile.getContent();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
        return inputStream;
    }

    @Override
    public void deleteReport(String file) {
        UreportFile ureportFile = ureportFileService.getByFileName(getCorrectName(file));
        if(BeanUtil.isNotEmpty(ureportFile)){
            ureportFileService.delete(ureportFile.getId());
        }
    }

    @Override
    public List<ReportFile> getReportFiles() {
        List<ReportFile> list = new ArrayList<>();

        return list    ;
    }

    @Override
    public void saveReport(String file, String content) {
        file = getCorrectName(file);
        UreportFile ureportFileEntity = ureportFileService.getByFileName(getCorrectName(file));
        Date currentDate = new Date();
        if(ureportFileEntity == null){
            ureportFileEntity = new UreportFile();
            ureportFileEntity.setName(file);
            ureportFileEntity.setContent(content.getBytes());
            ureportFileEntity.setCreateTime(currentDate);
            ureportFileEntity.setUpdateTime(currentDate);
            ureportFileService.insert(ureportFileEntity);
        }else{
            ureportFileEntity.setContent(content.getBytes());
               ureportFileEntity.setUpdateTime(currentDate);
            ureportFileService.update(ureportFileEntity);
        }
    }

    @Override
    public void saveReportByCategoryId(String file,String categoryId,String content) {
        file = getCorrectName(file);
        UreportFile ureportFileEntity = ureportFileService.getByFileName(getCorrectName(file));
        Date currentDate = new Date();
        if(ureportFileEntity == null){
            ureportFileEntity = new UreportFile();
            ureportFileEntity.setName(file);
            ureportFileEntity.setContent(content.getBytes());
            ureportFileEntity.setCategoryId(categoryId);
            ureportFileEntity.setCreateTime(currentDate);
            ureportFileEntity.setUpdateTime(currentDate);
            ureportFileService.insert(ureportFileEntity);
        }else{
            ureportFileEntity.setContent(content.getBytes());
            ureportFileEntity.setUpdateTime(currentDate);
            ureportFileService.update(ureportFileEntity);
        }
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public boolean disabled() {
        return disabled;
    }

    @Override
    public String getPrefix() {
        return prefix;
    }

    /**
     * 获取没有前缀的文件名
     * @param name
     * @return
     */
    private String getCorrectName(String name){
        if(name.startsWith(prefix)){
            name = name.substring(prefix.length(), name.length());
        }
        return name; 
    }
}

2.3 表结构


CREATE TABLE UREPORT_FILE(
  ID_ varchar(255)  NOT NULL COMMENT '主键',
  NAME_ varchar(255)  NULL DEFAULT NULL COMMENT '报表名称',
  CONTENT_ mediumblob NULL COMMENT '报表内容',
  CATEGORY_ID_ varchar(64)  NULL DEFAULT NULL COMMENT '分类ID',
  TENANT_ID_ varchar(64)  NULL DEFAULT NULL COMMENT '租用用户Id',
  CREATE_DEP_ID_ varchar(64)  NULL DEFAULT NULL COMMENT '创建部门ID',
  CREATE_BY_ varchar(64)  NULL DEFAULT NULL COMMENT '创建人ID',
  CREATE_TIME_ datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  UPDATE_BY_ varchar(64)  NULL DEFAULT NULL COMMENT '更新人ID',
  UPDATE_TIME_ datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (ID_) USING BTREE
);
ALTER TABLE UREPORT_FILE COMMENT 'ureport报表';

具体参考实现Jpaas-ureport

报表实现效果

文档更新时间: 2021-09-15 10:30   作者:zyg