EasyExcel


EasyExcel

https://www.yuque.com/easyexcel/doc/easyexcel

  1. 实例1
package com.lhq.Main;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.Data;

import java.util.Map;

/**
 * @author admin
 */

@Data
class Person{
    private String id ;
    private String name;
    private Integer age;
}


public class Main {
    public static void main(String[] args) throws Exception{
        ExcelReaderBuilder read = EasyExcel.read();
        read.file("C:\\Users\\admin\\Desktop\\test.xlsx");
        read.sheet("one");
        read.excelType(ExcelTypeEnum.XLSX);
        read.autoCloseStream(true);

        read.registerReadListener(new AnalysisEventListener<Map<Integer,String>>() {


            @Override
            public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
                System.out.println(integerStringMap);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("读取完毕");
            }
        });

        ExcelReader build = read.build();
        build.readAll();
        build.finish();
    }
}


  1. 示例二
package com.lhq.Main;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.Data;

import java.util.LinkedList;
import java.util.Map;

/**
 * @author admin
 */

@Data
class Person{
    private String id ;
    private String name;
    private Integer age;
}


public class Main {
    public static void main(String[] args) throws Exception{
        LinkedList<Map<Integer, String>> list = new LinkedList<>();
        ExcelReaderBuilder read = EasyExcel.read();
        read.file("C:\\Users\\admin\\Desktop\\test.xlsx");
        read.sheet("one");
        read.excelType(ExcelTypeEnum.XLSX);
        read.autoCloseStream(true);
        read.registerReadListener(new AnalysisEventListener<Map<Integer,String>>() {


            @Override
            public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
                list.add(integerStringMap);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("读取完毕");
            }
        });

        ExcelReader build = read.build();
        build.readAll();
        build.finish();

        System.out.println(list);
    }
}


  1. 实例三
@Data
public
class Person{
    @ExcelProperty("id")
    private String id ;
    @ExcelProperty("name")
    private String name;
    @ExcelProperty("age")
    private Integer age;
}


public class Main {
    public static void main(String[] args) throws Exception{
        EasyExcel.read("C:\\Users\\admin\\Desktop\\test.xlsx").sheet("one")
                .head(Person.class)
            .registerReadListener(new AnalysisEventListener<Person>() {

                @Override
                public void invoke(Person person, AnalysisContext analysisContext) {
                    System.out.println(person);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    System.out.println("完成");
                }
            }).doRead();
    }
}
  1. 写入
 package com.lhq.Main;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.lhq.pojo.Person;
import lombok.Data;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 */




public class Main {
    public static void main(String[] args) throws Exception{
        List<Person> data = getData();
        EasyExcel.write("C:\\Users\\admin\\Desktop\\test.xlsx").sheet("two")
                .head(Person.class)
                .doWrite(data);
    }

    public static List<Person> getData(){
        List<Person> list = new LinkedList<>();
        EasyExcel.read("C:\\Users\\admin\\Desktop\\test.xlsx").sheet("one")
                .head(Person.class)
                .registerReadListener(new AnalysisEventListener<Person>() {

                    @Override
                    public void invoke(Person person, AnalysisContext analysisContext) {
                        list.add(person);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        System.out.println("完成");
                    }
                }).doRead();
        return list;
    }
}



文章作者: anlen123
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 anlen123 !
 上一篇
pypi打包上传 pypi打包上传
第三方包用pip安装,上传到哪呢? 答案是上传到pypi.org里面去 详细教程: https://blog.konghy.cn/2018/04/29/setup-dot-py/ 1. 首先需要注册账号很简单不用说也知道 2. 准备工
2021-03-02 anlen123
下一篇 
LazyGit LazyGit
LazyGitgithub: https://github.com/jesseduffield/lazygit 简介: https://github.com/jesseduffield/lazygit/blob/master/README
2021-02-07 anlen123
  目录