V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
gaobing
V2EX  ›  Java

在现有实体类的基础上, 4 行代码快速生成 Excel

  •  
  •   gaobing ·
    gaoice · 2019-05-21 21:32:53 +08:00 · 1814 次点击
    这是一个创建于 1793 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在现有实体类的基础上构建最简单的 Excel,代码只需 4 行。

    实体类:

    Student {name, idcard, sex, ...}

    生成 XSSFWorkbook:

    String sheetName = "sheet name";

    String[] columnNames = {"姓名", "身份证号", "性别", ...};

    String[] classFieldNames = {"name", "idcard", "sex", ...};

    XSSFWorkbook workbook = ExcelBuilder.createWorkbook(new SheetInfo(sheetName, columnNames, classFieldNames, studentList));

    这个是对 poi 的包装,主要是业务上方便调用,快速生成 excel。

    github: https://github.com/gaoice/easyexcel

    ❤❤❤

    2 条回复    2019-05-22 10:16:11 +08:00
    gaobing
        2
    gaobing  
    OP
       2019-05-22 10:16:11 +08:00
    @mushishi 哈,我也是写完上传到 github 的时候发现 ali 开源库也有一个的。这个封装方便业务上快速调用,没有性能优化。之前设计有注解,但没办法动态调整导出字段内容,所以没用注解用了字段名字,而且支持字段对应 Lambda 表达式,感觉使用上还是要方便点哈。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5335 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 09:01 · PVG 17:01 · LAX 02:01 · JFK 05:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.