Easyexcel 读取任意格式,无需实体的excel文件。亲测可用

   
        
            com.alibaba
            easyexcel
            3.1.1
        
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 header = data.getHeader();
        List> 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> {

    private SheetData data = new SheetData();

    @Override
    public void invokeHead(Map> 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 dataMap, AnalysisContext analysisContext) {
        List 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 header = new ArrayList<>();
    private List> datas = new ArrayList<>();

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


}

你可能感兴趣的:(excel,java,junit)