记一次EasyExcel简单使用

一. EasyExcel简单介绍

EasyExcel语雀官方文档
EasyExce是阿里巴巴开源的一款excel文档处理工具类, 可以很方便的对excel文档进行处理

二. 使用方式简介

pom文件

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>

读取excel文件示例

  • 文件内容
    在这里插入图片描述
  • 读取代码
/**
 *  EasyExcel 读取excel文件demo
 */
public class Demo1 {

    public static void main(String[] args) throws FileNotFoundException {
        // 文件的位置
        String filePath = "D:\\酒店设备SN.xlsx";
        File file = new File(filePath);

        // 使用EasyExcel读取文件流, 第一个参数是文件流 第二个参数是读取的sheet表参数(第几个sheet表[1下标开始], 从第几行开始读[0下标开始], 对应的实体类)
        List<Object> read = EasyExcelFactory.read(new FileInputStream(file), new Sheet(1, 1, ExcelModel.class));
        List<ExcelModel> list = null;
        if (CollectionUtils.isNotEmpty(read)) {
            list = read.stream().map(x -> {
                if (x instanceof ExcelModel) {
                    return (ExcelModel) x;
                }
                return null;
            }).collect(Collectors.toList());
        }
        System.out.println(JSON.toJSONString(list));
    }

    /**
     * [
     *     {
     *         "assetId": "efee3fgwe",
     *         "cellStyleMap": {},
     *         "clientId": "dqwdwqdqw",
     *         "deviceSn": "123454ww45",
     *         "productId": "sgsaerg",
     *         "rowHeight": 0,
     *         "uuid": "23234243242"
     *     },
     *     {
     *         "assetId": "fgwegwfg",
     *         "cellStyleMap": {},
     *         "clientId": "dqwqdq",
     *         "deviceSn": "4444ss321",
     *         "productId": "gaerga",
     *         "rowHeight": 0,
     *         "uuid": "42323252"
     *     },
     *     {
     *         "assetId": "fwewf",
     *         "cellStyleMap": {},
     *         "clientId": "dqwqwq",
     *         "deviceSn": "123323545sss7",
     *         "productId": "argearea",
     *         "rowHeight": 0,
     *         "uuid": "423525232"
     *     }
     * ]
     */

}
  • demo代码
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
// 需要继承BaseRowModel
public class ExcelModel extends BaseRowModel {

    /**
     * 使用注解设置下标,0开始
     */
    @ExcelProperty(value ="设备sn",index = 0)
    private String deviceSn;

    @ExcelProperty(value ="uuid",index = 1)
    private String uuid;

    @ExcelProperty(value ="assetId",index = 2)
    private String assetId;

    @ExcelProperty(value ="clientId",index = 3)
    private String clientId;

    @ExcelProperty(value ="productId",index = 4)
    private String productId;
}

生成excel文件示例

  • 生成的Excel文件内容
    在这里插入图片描述

  • 生成的代码

/**
 * 生成excel文件
 */
public class Demo2 {

    public static void main(String[] args) {
        // 需要写入的内容
        List<ExcelDemo2> excelDemoList = new ArrayList<>(Arrays.asList(
                ExcelDemo2.builder().name("胡琦").age("24").build(),
                ExcelDemo2.builder().name("yymm").age("23").build(),
                ExcelDemo2.builder().name("想听").age("25").build()
        ));
        FileOutputStream fileOutputStream = null;
        try {
            // 使用EasyExcel生成excel放到D盘
            File file = new File("D:\\测试easyExcel.xlsx");
            fileOutputStream = new FileOutputStream(file);

            ExcelWriter writer = new ExcelWriter(fileOutputStream, ExcelTypeEnum.XLSX, true);
            Sheet sheet = new Sheet(1, 0, ExcelDemo2.class);
            writer.write(excelDemoList, sheet);
            writer.finish();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("生成成功");


    }
}
  • model
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ExcelDemo2 extends BaseRowModel {

    @ExcelProperty(value = "姓名" ,index = 0)
    private String name;

    @ExcelProperty(value = "年龄" ,index = 1)
    private String age;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值