欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

EasyExcel读写Excel文件

程序员文章站 2024-03-20 15:25:22
...

为什么使用EaysExcel:

简洁且不会造成内存泄漏

示例

    @Test
    public void test2(){
        //读取文件
        //创建ExcelReaderBuilder实例
        ExcelReaderBuilder readerBuilder = EasyExcel.read();
        //获取文件对象
        readerBuilder.file("D:\\desk\\test\\单位名称.xlsx");
        //指定sheet
        readerBuilder.sheet("Sheet0");
        //自动关闭输入流
        readerBuilder.autoCloseStream(true);
        //设置文件格式
        readerBuilder.excelType(ExcelTypeEnum.XLSX);
        //注册监听器进行数据的解析
        readerBuilder.registerReadListener(new AnalysisEventListener<Map<Integer,String>>() {
			//最原始的匿名内部类重写的方法的第一参数是Object,我们指定接收类型为Map<Integer,String>
			//下文会有针对映射类的实例重写的方法
			//invoke方法会一行一行的读取excel的内容
            @Override
            public void invoke(Map<Integer, String> map, AnalysisContext analysisContext) {
                Set<Integer> keySet = map.keySet();
                Iterator<Integer> iterator = keySet.iterator();
                while (iterator.hasNext()){
                    Integer key = iterator.next();
                    System.out.print(key + ":" + map.get(key) + ", ");
                }
                System.out.println("");
            }
			//读取完成之后会执行的回调方法
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                //通知文件读取完毕
                System.out.println("文件读取完毕!");
            }
        });
        //构建读取器
        ExcelReader reader = readerBuilder.build();
        //读取数据
        reader.readAll();;
        //读取完毕
        reader.finish();
    }

使用步骤

  1. 导入依赖
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>
  1. 编写读写映射实例VO:
package com.atwu.miao.vo;

import com.alibaba.excel.annotation.ExcelProperty;

public class ExcelTestVo {
    @ExcelProperty("id")
    private String id;
    @ExcelProperty("name")
    private String name;
    @ExcelProperty("base_name")
    private String baseName;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBaseName() {
        return baseName;
    }

    public void setBaseName(String baseName) {
        this.baseName = baseName;
    }

    @Override
    public String toString() {
        return "ExcelTestVo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", baseName='" + baseName + '\'' +
                '}';
    }
}

  1. 编写测试类,读取excel文件\导出excel文件
package com.atwu.miao.test;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.atwu.miao.vo.ExcelTestVo;
import org.junit.Test;

import java.util.*;

public class ExcelTest {

    @Test
    public void test1(){
        List<Map<Integer,String>> list = new LinkedList<>();
        EasyExcel.read("D:\\desk\\test\\单位名称.xlsx")
                .sheet()
                .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("数据读取完毕");
                    }
                }).doRead();
        for (Map<Integer, String> integerStringMap : list) {
            Set<Integer> keySet = integerStringMap.keySet();
            Iterator<Integer> iterator = keySet.iterator();
            while (iterator.hasNext()){
                Integer key = iterator.next();
                System.out.print(key + ":" + integerStringMap.get(key) + ", ");
            }
            System.out.println("");
        }
    }

    @Test
    public void test2(){
        List<ExcelTestVo> list = new LinkedList<>();
        EasyExcel.read("D:\\desk\\test\\单位名称.xlsx")
                .head(ExcelTestVo.class)
                .sheet()
                .registerReadListener(new AnalysisEventListener<ExcelTestVo>() {
                    @Override
                    public void invoke(ExcelTestVo excelTestVo, AnalysisContext analysisContext) {
                        list.add(excelTestVo);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        System.out.println("数据读取完毕");
                    }
                }).doRead();
        for (ExcelTestVo excelTestVo : list) {
            System.out.println(excelTestVo);
        }
    }

    @Test
    public void test3(){
        List<ExcelTestVo> list = new LinkedList<>();
        EasyExcel.read("D:\\desk\\test\\单位名称.xlsx")
                .head(ExcelTestVo.class)
                .sheet()
                .registerReadListener(new AnalysisEventListener<ExcelTestVo>() {
                    @Override
                    public void invoke(ExcelTestVo excelTestVo, AnalysisContext analysisContext) {
                        list.add(excelTestVo);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        System.out.println("数据读取完毕");
                    }
                }).doRead();
        //list写入Excel文件
        EasyExcel.write("D:\\desk\\test\\单位名称导出.xlsx")
                .head(ExcelTestVo.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet()
                .doWrite(list);
    }
}

以上就是初学EasyExcel的读写,EasyExcel就是这么简洁!

相关标签: java excel poi