EXCEL导入

1. EXCEL导入后台

EXCEL导入:是根据EXCEL模板读取传入的EXCEL文件,并存入到List对象中,以供各业务系统使用。首先需要配置EXCEL导入的一些配置信息。

下面是EXCEL导入配置信息说明:

1.1 EXCEL导入配置信息

EXCEL导入配置信息可在数据库中进行配置维护,也可以直接使用代码将配置信息写入,下面介绍使用数据库配置方式来进行配置。

EXCEL导入配置表:
IMPORT_CONFIG
内容如下:

IMPORT_CONFIG:

列名 字段说明
id 主键
template EXCEL模板(二进制数据存储)
class_name 读取excel转化成的实体对象的类名(全限定名)
dictionary_url 获取数据字典controller的URL地址
deal_service 结果处理类
method_name 结果处理类方法名

1.2 EXCEL导入后台配置

EXCEL导入后台提供一个访问接口(ImportController类[/import/excel]),一个处理EXCEL导入的Service服务类(ImportSupportService)
其中:ImportSupportService类已经在spring-excel-service.xml文件中定义好了,只需在ApplicationContext.xml引入就OK了)

<import resource="classpath*:spring-excel-service.xml"/>

ImportController类在spring-excel-controller.xml文件中定义好了,需在dispatcher-servlet.xml中引入

<import resource="classpath*:spring-excel-controller.xml"/>

1.3 编写EXCEL导入服务类

a、各个业务系统需根据自己的需求,实现ImportService接口

  1. 接口ImportConfig getImportConfig(String key) 根据key参数从数据库或其他配置文件中获取导入的配置信息。

以下给个示例实现:

package com.gillion.sebusiness.demo.service.importexcel;

    import com.gillion.excel.in.entity.ImportConfig;
    import com.gillion.excel.in.service.ImportService;
    import com.gillion.sebusiness.excel.mapper.ImportConfigMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    @Service("importServiceImpl")
    public class ImportServiceImpl implements ImportService{

    @Autowired
    private ImportConfigMapper importConfigMapper;
    /**
    * 获取配置信息
    * @param fileName
    * @param ou
    */
    public ImportConfig getImportConfig(String key) {
    com.gillion.sebusiness.excel.model.ImportConfig config=importConfigMapper.selectByPrimaryKey(key);
        ImportConfig importConfig = new ImportConfig();
        importConfig.setTemplate(config.getTemplate());
        importConfig.setDictionaryUrl(config.getDictionaryUrl());
        importConfig.setClassName(config.getClassName());
        importConfig.setKey(config.getKey());
        importConfig.setDealService(config.getDealService());
        importConfig.setMethodName(config.getMethodName());
        return importConfig;
    }
    public ImportConfigMapper getImportConfigMapper( ) {
        return importConfigMapper;
    }

    public void setImportConfigMapper(ImportConfigMapper importConfigMapper) {
        this.importConfigMapper = importConfigMapper;
    }
}

b、各个业务系统需根据自己的需求,编写处理转化好了的EXCEL数据的服务:

以下给个示例实现:

package com.gillion.sebusiness.demo.service.importexcel.impl;

    import com.gfa4j.batch.domain.BatchExecutor;
    import com.gfa4j.batch.domain.BatchExecutorFactory;
    import com.gfa4j.utils.ContextHolder;
    import com.gillion.sebusiness.demo.export.test.StudentScore;
    import com.gillion.sebusiness.demo.service.importexcel.ImportDealService;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.context.expression.BeanFactoryResolver;
    import org.springframework.expression.spel.support.StandardEvaluationContext;
    import org.springframework.stereotype.Service;
    import java.util.List;

    @Service("importDealServiceImpl")
    public class ImportDealServiceImpl implements ImportDealService,ApplicationContextAware,InitializingBean{

    private StandardEvaluationContext context;
    private ApplicationContext applicationContext;
    /**
    * 把excel数据(List)存入数据库中
    * @param excelData Excel中的数据
    */
    @Override
    public void dealExcleList(List<StudentScore> excelData) {
        BatchExecutorFactory batchExecutorFactory = ContextHolder.getBean(BatchExecutorFactory.class);
        BatchExecutor batchExecutor = batchExecutorFactory.getBatchExecutor(StudentScore.class);
        batchExecutor.saveOrUpdate(excelData);
    }

    @Override
    public void afterPropertiesSet() throws Exception  {
        public void afterPropertiesSet() throws Exception {
        context = new StandardEvaluationContext();
        context.setBeanResolver(new BeanFactoryResolver(applicationContext));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)throws BeansException  {
        this.applicationContext = applicationContext;
    }

}
    

c、各个业务系统需根据自己的需求,编写导入时需要的数据字典Controller类:

以下给个示例实现:

package com.gillion.sebusiness.demo.controller.dictionary;
    import  org.springframework.stereotype.Controller;
    import  org.springframework.web.bind.annotation.RequestMapping;
    import  org.springframework.web.bind.annotation.RequestMethod;
    import  org.springframework.web.bind.annotation.ResponseBody;

    @Controller
    @RequestMapping("/dictionary")
    public class DictionaryController{

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Page returnDictionary( ) {
        String result = "{\"name\":{\"小华\":\"华哥\"}}";
        return result;
    }
}

2. EXCEL导入前端

EXCEL导入前端,为一个g-import-excel指令控件

属性说明如下

属性说明

参数名 含义 是否必须 说明
key 查询配置信息参数 Y 用于后台代码查询导入配置信息的查询条件
import_url 处理导入的controller地址 Y 后续会统一配置到前端配置文件中。目前地址为[ /项目名称/import/excel ]
display-express 按钮显示名称 Y

示例说明如下:

 <g-import-excel
       key="id1"
       import_url="/gillion-web/import/excel"
       display-express="导入EXCEL数据">
          </g-import-excel>