<!--easyexcel-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.1</version>
        </dependency>
package com.tudb.cbpc;
import org.junit.Test;
import java.util.List;

/**
 * @Author Easyexcel 读取本地文件demo
 * @description
 * @Date 2023/2/23 19:34
 * @Version 1.0
 */
public class EasyexcelDemo {
    
    @Test
    public void test()throws Exception{

        //读数据
        String path = "D://01导入模板.xlsx";
        String paths = "D://02导入模板.xlsx";
        String patht = "D://03导入.xlsx";
        SheetData data = ExcelTool.read(patht);
        List<String> header = data.getHeader();
        List<List<String>> datas = data.getDatas();
        System.out.println(data);
        
    }

}
package com.tudb.cbpc;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @Author 封装工具类直接对SheetData进行读写
 * @description
 * @Date 2023/2/23 20:21
 * @Version 1.0
 */
public class ExcelTool {
    public static SheetData read(InputStream is, int sheetIndex){
        ArrayDataListener arrayDataListener = new ArrayDataListener();
        EasyExcel.read(is, arrayDataListener).sheet(sheetIndex).doRead();
        return arrayDataListener.getData();
    }

    public static SheetData read(String path) throws FileNotFoundException {
        return read(path, 0);
    }

    public static SheetData read(String path, int sheetIndex) throws FileNotFoundException {
        FileInputStream fis = null;
        SheetData data = null;
        try{
            fis = new FileInputStream(path);
            data = read(fis, sheetIndex);
        } finally {
            //.close(fis);
        }

        return data;
    }

    public static void write(String dist, SheetData data){
        write(dist, data, 0);
    }

    public static void write(String dist, SheetData data, int sheetIndex){
        ExcelWriterBuilder excelWriter = EasyExcel.write(dist);
        excelWriter.head(data.toExcelHeader());
        excelWriter.sheet(sheetIndex).doWrite(data.getDatas());
    }
}
package com.tudb.cbpc;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tudb.cbpc.utils.StringUtils;
import lombok.Data;
import com.alibaba.excel.metadata.data.ReadCellData;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author 实现AnalysisEventListener接口处理表头和每一行的数据
 * @description
 * @Date 2023/2/23 20:18
 * @Version 1.0
 */
@Data
public class ArrayDataListener extends AnalysisEventListener<Map<Integer,String>> {

    private SheetData data = new SheetData();

    @Override
    public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
        super.invokeHead(headMap, context);

        for(Integer key : headMap.keySet()){
            ReadCellData cellData = headMap.get(key);
            String value = cellData.getStringValue();
            if(StringUtils.isEmpty(value)){
                value = "none";
            }
            data.getHeader().add(value);
        }
    }


    @Override
    public void invoke(Map<Integer, String> dataMap, AnalysisContext analysisContext) {
        List<String> line = new ArrayList<>();
        for(Integer key : dataMap.keySet()){
            String value = dataMap.get(key);
            if(StringUtils.isEmpty(value)){
                value = "none";
            }
            line.add(value);
        }

        data.getDatas().add(line);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }
}
package com.tudb.cbpc;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 通用的实体
 * @description
 * @Date 2023/2/23 20:18
 * @Version 1.0
 */
@Data
public class SheetData {

    private List<String> header = new ArrayList<>();
    private List<List<String>> datas = new ArrayList<>();

    public List<List<String>> toExcelHeader(){
        List<List<String>> headList = new ArrayList<List<String>>();
        for(String row : header){
            List<String> h = new ArrayList<>();
            h.add(row);
            headList.add(h);
        }
        return headList;
    }


}

GitHub 加速计划 / ea / easyexcel
31.64 K
7.47 K
下载
快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:3 个月前 )
c42183df Bugfix 3 个月前
efa7dff6 * 重新加回 `commons-io` 3 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐