easyExcel:由阿里巴巴公司开发,由github托管

github上有详细使用文档

github地址:https://github.com/alibaba/easyexcel/blob/master/quickstart.md

 

 

导入

1、模型类:可以是实体类

主要是@ExcelProperty注解

value:对应字段

index:对应导入模板是第几列(从0开始)

@ExcelProperty(value = "name", index = 0)

 

推荐使用有模型,因为导入模板如果有变化,index可以随时改变

 

2、Controller接收导入文件

//批量导入(有模型)
    @RequestMapping("batchImport")
    public ModelAndView batchImport(@RequestParam(value = "file", required=true)MultipartFile file) {
        ModelAndView mv=new ModelAndView();
        mv.setView(Jackson2Util.jsonView());
        InputStream in = null;
        try {
            in = file.getInputStream();
            // 解析每行结果在listener中处理
            AnalysisEventListener listener = new ExcelListener(vehicleInfoService);
            ExcelReader excelReader = new ExcelReader(in, ExcelTypeEnum.XLSX, null, listener);
            //(第几个sheet,表头所在行数,表格对应实体类)
            excelReader.read(new Sheet(1, 1, ExcelCardIssueVehicleInfo.class));
            
            mv.addObject("res", "0");
            
        } catch(Exception e) {
            logger.error("批量导入失败!", e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return mv;
    }

 

 

3、解析每行数据并入库

public class ExcelListener extends AnalysisEventListener {
    private CardIssueVehicleInfoService cardIssueVehicleInfoService;
   
    public ExcelListener(CardIssueVehicleInfoService cardIssueVehicleInfoService) {
        super();
        this.cardIssueVehicleInfoService = cardIssueVehicleInfoService;
    }
    
    private List<CardIssueVehicleInfo> datas = new ArrayList<CardIssueVehicleInfo>();
    //每解析一行数据就走一遍invoke()方法
    public void invoke(Object object, AnalysisContext context) {
        //转为导入模型类
        ExcelCardIssueVehicleInfo excel = (ExcelCardIssueVehicleInfo)object;
        CardIssueVehicleInfo vehicleInfo =  new CardIssueVehicleInfo();
        try {
            //导入模型类转为对应数据库的实体类:可以是同一个实体类
            BeanUtils.copyProperties(vehicleInfo, excel);
            datas.add(vehicleInfo);    //数据存储到list,供批量导入处理,或后续自己业务逻辑处理。
            
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
    //解析完所有Excel数据后,走此方法
    public void doAfterAllAnalysed(AnalysisContext context) {
        try{
            //入库
            cardIssueVehicleInfoService.batchInsert(datas);
        } catch(Exception e) {
            logger.error("批量导入失败!", e);
        }
        datas.clear();//解析结束销毁不用的资源
    }
    

}

 

 

遇到问题及解决方案:

1、模型(实体类)支持String和int类型,不支持Short类型

 

2、在ExcelListener 类中,Spring注入Service层,会无法注入

解决:在Controller层,将已注入的Service,传入ExcelListener中

 AnalysisEventListener listener = new ExcelListener(vehicleInfoService);

 

并在ExcelListener的构造器中接收

public ExcelListener(CardIssueVehicleInfoService cardIssueVehicleInfoService) {
        super();
        this.cardIssueVehicleInfoService = cardIssueVehicleInfoService;
}

 

3、poi.jar和poi-ooxml.jar的版本一定要一致(easyExcel依赖poi),不然会报错


4、easyExcel基于POI的,遇到的报错,都可以按照POI的错误搜索解决办法

 

 posted on 2018-12-13 10:44  布鲁布鲁sky  阅读(9435)  评论(0编辑  收藏  举报