EasyExcel读写Excel

  1. 引入依赖
  2. 工具类封装
  3. 具体测试用例
  4. 运行结果
  5. 下载excel文件到浏览器
  6. 联系作者
    1. 微信公众号
    2. QQ群

EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。

我们来看下通过EasyExcel来读写Excel有多方便。

首先我们创建两个方便测试得excel文件,放在桌面或哪里都可以,test_export.xlsx和test_import.xlsx

引入依赖

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>2.2.10</version>
    </dependency>    

工具类封装

package com.template.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import lombok.var;

/**
 * Excel工具类
 */
public class ExcelUtil {
    /**
     * 模板类
     */
    public interface Template {
    }

    /**
     * 根据模板类型,导出Excel
     * @param list   数据列表
     * @param $class 描述Excel结构信息的模板类型
     */
    public static <T extends Template> InputStream Export(List<?> list, Class<T> $class) {
        var stream = new ByteArrayOutputStream();
        var writer = EasyExcel.write(stream);
        writer.head($class);
        var sheet = writer.sheet("Sheet1");
        sheet.doWrite(list);
        return new ByteArrayInputStream(stream.toByteArray());
    }

    /**
     * 根据模板类型,导入Excel
     * @param stream 文件流
     * @param $class 描述Excel结构信息的模板类型
     */
    public static <T extends Template> List<T> Import(InputStream stream, Class<T> $class) {
        var reader = EasyExcel.read(stream);
        reader.head($class);
        return reader.doReadAllSync();
    }
}

具体测试用例

package com.template.service.dailycode;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.template.utils.ExcelUtil;
import lombok.Data;
import lombok.var;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExportImportExcelService {

    public static void Export(){
        String fileName = "C:\\Users\\38427\\Desktop\\test_export.xlsx";
        List<TemplateObject> list = new ArrayList<>();

        TemplateObject row1 = new TemplateObject();
        row1.setCELL_1("1");
        row1.setCELL_2("1");
        row1.setCELL_3("1");
        row1.setCELL_4("1");
        row1.setCELL_5("1");
        TemplateObject row2 = new TemplateObject();
        row2.setCELL_1("2");
        row2.setCELL_2("2");
        row2.setCELL_3("2");
        row2.setCELL_4("2");
        row2.setCELL_5("2");

        list.add(row1);
        list.add(row2);

        EasyExcel.write(fileName, TemplateObject.class).sheet("Sheet1").doWrite(list);
    }

    public static void Import(){
        try {
            InputStream stream = new FileInputStream("C:\\Users\\38427\\Desktop\\test_import.xlsx");
            var list = ExcelUtil.Import(stream, TemplateObject.class);
            for(TemplateObject templateObject: list){
                System.out.println(templateObject);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /** 导入导出模板 */
    @Data
    public static class TemplateObject implements ExcelUtil.Template{
        @ExcelProperty("列名1")
        public String CELL_1;
        @ExcelProperty("列名2")
        public String CELL_2;
        @ExcelProperty("列名3")
        public String CELL_3;
        @ExcelProperty("列名4")
        public String CELL_4;
        @ExcelProperty("列名5")
        public String CELL_5;
    }

    public static void main(String[] args) {
        Import();
        Export();
    }

}

运行结果

ExportImportExcelService.TemplateObject(CELL_1=1, CELL_2=1, CELL_3=1, CELL_4=1, CELL_5=1)
ExportImportExcelService.TemplateObject(CELL_1=2, CELL_2=2, CELL_3=2, CELL_4=2, CELL_5=2)
ExportImportExcelService.TemplateObject(CELL_1=3, CELL_2=3, CELL_3=3, CELL_4=3, CELL_5=3)
ExportImportExcelService.TemplateObject(CELL_1=4, CELL_2=4, CELL_3=4, CELL_4=4, CELL_5=4)
ExportImportExcelService.TemplateObject(CELL_1=5, CELL_2=5, CELL_3=5, CELL_4=5, CELL_5=5)

可以看到已经读取出test_import.xlsx文件中得具体内容了,
同时,两条造得数据也已经写入test_export.xlsx文件中。

当然,如果是需要下载到浏览器得话,可以用如下方式实现:

下载excel文件到浏览器

package com.template.controller;

import com.alibaba.excel.EasyExcel;
import com.template.service.dailycode.ExportImportExcelService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
public class EasyExcelController {

    @GetMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''"+fileName+".xlsx");

        List<ExportImportExcelService.TemplateObject> list = new ArrayList<>();

        ExportImportExcelService.TemplateObject row1 = new ExportImportExcelService.TemplateObject();
        row1.setCELL_1("1");
        row1.setCELL_2("1");
        row1.setCELL_3("1");
        row1.setCELL_4("1");
        row1.setCELL_5("1");
        ExportImportExcelService.TemplateObject row2 = new ExportImportExcelService.TemplateObject();
        row2.setCELL_1("2");
        row2.setCELL_2("2");
        row2.setCELL_3("2");
        row2.setCELL_4("2");
        row2.setCELL_5("2");

        list.add(row1);
        list.add(row2);

        EasyExcel.write(response.getOutputStream(), ExportImportExcelService.TemplateObject.class).sheet("Sheet1").doWrite(list);
    }
}

此时,我们起好服务,同时浏览器访问http://localhost:8080/exportExcel

我们就能看到浏览器里,下载了一个名为“测试.xlsx”,打开发现,造得两条测试数据也在文件中。

至此,说明我们使用easyexcel就轻松得完成了对excel得读写及下载操作。

联系作者

微信公众号

xiaomingxiaola
(BossLiu)

QQ群

58726094


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 384276224@qq.com

×

喜欢就点赞,疼爱就打赏

日记本 网文世界