栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > .Net

Easy操作Excel导入

.Net 更新时间:发布时间: 百科书网 趣学号

Controller

public GbmResult batchReadFile(@RequestParam(required = false,name ="file") MultipartFile file) throws IOException {
    String fileName = file.getOriginalFilename();
    String suffixName = fileName.substring(fileName.lastIndexOf("."));
    if (!(suffixName.equals(".xlsx"))) {
      System.out.println("请上传xlsx格式文件");
    }
    Listener listener = new Listener (TestService);    EasyExcel.read(file.getInputStream(),MgbsupplierBatchDto.class,listener).sheet().doRead();
    return Listener .getResult();
}

MgbsupplierBatchDto.class 导入Excel的类

监视器

@Slf4j
public class Listener extends AnalysisEventListener {
    List list = new ArrayList();
   //这是需要做业务的service
    private TestService testService;
    private Map map = new HashMap(){
        {
            put("success",0);
            put("message","");           
        }
    };
    public Map getResult(){
        return this.map;
    }
    public Listener(TestService testService){
        this.testService= testService;
    }
    @Override
    public void invoke(MgbsupplierBatchDto mgbsupplierBatchDto, AnalysisContext analysisContext) {
             //有什么判断都可以在这里做
            list.add(mgbsupplierBatchDto); 
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
     //读取完excel,最后执行这里
    this.map=testService.readBatch(list,map);
    }
}

//过来excel的空行

private static boolean isAllNull(MgbsupplierBatchDto t) {
    for (Method method : methods) {
        if (method.getName().contains("get") && method.getParameterCount() == 0) {
            if(!StringUtils.contains(method.getName(),"getCellStyleMap")&&!StringUtils.contains(method.getName(),"getClass") ){
                Object invoke = null;
                try {
                    invoke = method.invoke(t);
                } catch (IllegalAccessException e) {
                    log.error(e.toString(),e);
                } catch (InvocationTargetException e) {
                    log.error(e.toString(),e);
                }
                if (ToolUtil.isNotEmpty(invoke)) {
                    return false;
                }
            }

        }
    }
    return true;
}
转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/985152.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号