• 【esaypoi】esaypoi 使用


    需求是这样的: 我们线上有些数据存在不一致的情况,leader 给了我一张 excel 表格, 叫我对比表格,写出更新的 sql 。

    刚开始,我觉得也没有什么,打开表格就开始复制粘贴,查询起来。

    但是做到一半,猛然觉得身为程序员却做这么多重复的工作简直就是一种耻辱。

    于是乎,写了一个小demo,自动生成 sql 文件。

    第一步,maven 依赖

        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.3</version>
        </dependency>

    第二步,定义pojo

    比如我的表格样式是这样的

    我定义的dto就是下面这样的

    @Data
    public class PreTypeDTO {
        @Excel(name = "id", orderNum = "0")
        private String id;
        @Excel(name = "typeName", orderNum = "1")
        private String typeName;
        @Excel(name = "parentId", orderNum = "2")
        private String parentId;
        @Excel(name = "type", orderNum = "3")
        private String type;
        @Excel(name = "opStatus", orderNum = "4")
        private String opStatus;
    }

    第三步, 愉快地使用

    以将 excel 中的数据转化为 list 对象为例,

    具体的实现可以这样写

    public static <E> List<E> import2List(String path, Class<?> pojoClass,ImportParams importParams){
        List<E> result = null;
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(path);
        }catch (IOException e){
            log.error("read path error.");
            log.error(e.getMessage(), e);
            return new LinkedList<>();
        }
        
        try {
            result =  ExcelImportUtil.importExcel(inputStream, pojoClass, importParams);
        }catch (Exception e){
            log.error("importExcel error.");
            log.error(e.getMessage(), e);
            return new LinkedList<>();
        }
    
        try {
            if(Objects.nonNull(inputStream)){
                inputStream.close();
            }
        }catch (IOException e){
            log.error("close inputStream error.");
            log.error(e.getMessage(), e);
            return new LinkedList<>();
        }
    
        return result;
    }

    比如将 list 对象导出到 excel 中。

    public static <E> String list2Excel(List<E> list, Class<E> clazz, String path){
        ExportParams exportParams = new ExportParams();
        exportParams.setSheetName("sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, clazz, list);
        File file = FileUtils.createNewFile(path);
        FileOutputStream out = null;
        try{
            out = new FileOutputStream(file);
            workbook.write(out);
        }catch (IOException e){
            log.error(e.getMessage(), e);
        }
    
        try {
            if(null != out){
                out.close();
            }
        }catch (IOException e){
            log.error(e.getMessage(),e);
        }
        return path;
    }

  • 相关阅读:
    类的空间问题
    面向对象初识
    collections模块,shelve模块
    一段水印文字的练习
    jquery选择器中(:button)的含义
    关于通过jquery来理解position的relative及absolute
    [小明学算法]1.动态规划--最长递增子序列问题
    [小明学算法]2.贪心算法----
    [Unity的序列化]2.技能编辑器的实现
    [Unity的序列化]1.什么是序列化
  • 原文地址:https://www.cnblogs.com/catlkb/p/12530343.html
Copyright © 2020-2023  润新知