亲宝软件园·资讯

展开

SpringBoot集成EasyExcel SpringBoot集成EasyExcel的步骤

爱鼓捣的程序猿 人气:2
想了解SpringBoot集成EasyExcel的步骤的相关内容吗,爱鼓捣的程序猿在本文为您仔细讲解SpringBoot集成EasyExcel的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot,EasyExcel,SpringBoot集成EasyExcel,下面大家一起来学习吧。

一 、EasyExcel简介

EasyExcel优势

二、常用注解

三、依赖

  <!-- easyexcel 主要依赖  这一个基本上就够了-->
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>easyexcel</artifactId>
   <version>2.1.4</version>
</dependency>
    <!-- servlet-api -->
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>4.0.1</version>
   <scope>provided</scope>
</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.47</version>
</dependency>

四、监听

/**
 * EasyExcel 导入监听
 */
public class ExcelListener extends AnalysisEventListener {
    //可以通过实例获取该值
    private List<Object> datas = new ArrayList<Object>();

    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        datas.add(o);//数据存储到list,供批量处理,或后续自己业务逻辑处理。
        doSomething(o);//根据自己业务做处理
    }

    private void doSomething(Object object) {
        //1、入库调用接口
    }

    public List<Object> getDatas() {
        return datas;
    }

    public void setDatas(List<Object> datas) {
        this.datas = datas;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // datas.clear();//解析结束销毁不用的资源
    }
}

五、接口导入Excel

try {
            //获取文件名
            String filename = file.getOriginalFilename();
            //获取文件流
            InputStream inputStream = file.getInputStream();
            //实例化实现了AnalysisEventListener接口的类
            ExcelListener listener = new ExcelListener();
            //传入参数
            ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
            //读取信息
            excelReader.read(new Sheet(1, 0, Test.class));
            //获取数据
            List<Object> list = listener.getDatas();
            if (list.size() > 1) {
                for (int i = 0; i < list.size(); i++) {
                    Testobj = (Test) list.get(i);
                    JSONObject jo = new JSONObject();
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

六、接口 导出Excel (HttpServletResponse response, HttpServletRequest request)

try {
    String filenames = "111111";
    String userAgent = request.getHeader("User-Agent");
    if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
        filenames = URLEncoder.encode(filenames, "UTF-8");
    } else {
        filenames = new String(filenames.getBytes("UTF-8"), "ISO-8859-1");
    }
    response.setContentType("application/vnd.ms-exce");
    response.setCharacterEncoding("utf-8");
    response.addHeader("Content-Disposition", "filename=" + filenames + ".xlsx");
    EasyExcel.write(response.getOutputStream(), Test.class).sheet("sheet").doWrite(testList);
} catch (Exception e) {
}

七、本地导入、本地导出

List<Test> testList = new ArrayList<>();
try {
    String strUrl = "C:\\Users\\Administrator\\Desktop\\json.xlsx";
    File multipartFile = new File(strUrl);
    InputStream inputStream = new FileInputStream(multipartFile);
    //实例化实现了AnalysisEventListener接口的类
    ExcelListener listener = new ExcelListener();
    //传入参数
    ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
    //读取信息
    excelReader.read(new Sheet(1, 0, Test.class));
    //获取数据
    List<Object> list = listener.getDatas();
    if (list.size() > 1) {
        for (int i = 0; i < list.size(); i++) {
            Testobj = (Test) list.get(i);
        }
    }
} catch (Exception e) {
    System.out.println(e.getMessage());
}
try {
    String strUrl = "C:\\Users\\Administrator\\Desktop\\json"+System.currentTimeMillis()+".xlsx";
    EasyExcel.write(strUrl,Test.class).sheet("sheet").doWrite(testList);
} catch (Exception e) {
}

  以上就是EasyExcel的基础使用过程,欢迎点赞关注交流。

加载全部内容

相关教程
猜你喜欢
用户评论