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

EasyExcel导入导出excel工具类

程序员文章站 2024-03-21 12:36:40
...

 easyexcel依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>1.1.2-beat1</version>
        </dependency>

 工具类

package com.example.demo.files;


import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.demo.timer.entity.Words;
import com.example.demo.timer.listner.ExcelListner;
import com.example.demo.timer.listner.ModelListner;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @describtion  easyexcel读文件生成简单的excel
 * @create-time 9:38 2019/11/25
 **/
public class ExcelUtil {
    private static Logger log = LoggerFactory.getLogger(ExcelUtil.class);
    private static Sheet initSheet;

    static {
        initSheet = new Sheet(1, 0);
        initSheet.setSheetName("sheet");
        //设置自适应宽度
        initSheet.setAutoWidth(Boolean.TRUE);
    }

    /*
     * @Description: 读excel
     * @param: [file]
     * @return: java.util.List<java.util.List<java.lang.String>>
     * @Date: 2019/12/6
     */
    public static List<List<String>> read(File file) throws FileNotFoundException {
        ExcelListner listner = new ExcelListner();
        InputStream in = new FileInputStream(file);
        ExcelTypeEnum typeEnum;
        if(file.getName().equals("xlsx")) {
            typeEnum = ExcelTypeEnum.XLSX;
        }else {
            typeEnum = ExcelTypeEnum.XLS;
        }
        ExcelReader reader = new ExcelReader(in, typeEnum,null,listner);
        reader.read();
        return listner.getData();
    }

    /*
     * @Description: 读excel
     * @param: [file]
     * @return: java.util.List<java.util.List<java.lang.String>>
     * @Date: 2019/12/6
     */
    public static List<Object> readByModel(InputStream inputStream, Class<? extends BaseRowModel> clazz,ExcelTypeEnum excelTypeEnum) {
        ModelListner listner = new ModelListner();
        ExcelReader reader = new ExcelReader(inputStream,excelTypeEnum,null,listner);
        reader.read(new Sheet(1,1,clazz));
        return listner.getDatas();
    }

    /**
     *
     * @param file 有文件时
     * @return
     * @throws FileNotFoundException
     */
    public static Object write(File file) throws FileNotFoundException {
        OutputStream out = new FileOutputStream(file);
        ExcelWriter writer = new ExcelWriter(out,ExcelTypeEnum.XLS);
        return null;
    }
    /////////////////////////////内部类实现Listener/////////////////////////////////////
    private static class ExcelListner extends AnalysisEventListener {
        private List<List<String>> data = new ArrayList<>();
        @Override
        public void invoke(Object object, AnalysisContext context) {
            List<String> list = (List<String>)object;
            data.add(list);
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {

        }

        public List<List<String>> getData() {
            return data;
        }
        public void setData(List<List<String>> datas) {
            this.data = datas;
        }

    }

    private static class ModelListner extends AnalysisEventListener {
        private List<Object> datas = new ArrayList<>();
        @Override
        public void invoke(Object object, AnalysisContext context) {
            datas.add(object);
        }
        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {
        }
        public List<Object> getDatas() {
            return datas;
        }
        public void setDatas(List<Object> datas) {
            this.datas = datas;
        }

    }
}

 

相关标签: excel工具类