依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.74</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
工具类
package com.cyz.easyexcel.utils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
实体类
-
@ExcelProperty("文章标题") 导出显示列名
package com.cyz.easyexcel.domain;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
服务类
实体监听类
-
继承 AnalysisEventListener
-
用于导入Excel的时候 映射字段
package com.cyz.easyexcel.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.cyz.easyexcel.dao.ArticleDao;
import com.cyz.easyexcel.domain.Article;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
接口
package com.cyz.easyexcel.controller;
import com.alibaba.excel.EasyExcel;
import com.cyz.easyexcel.dao.ArticleDao;
import com.cyz.easyexcel.domain.Article;
import com.cyz.easyexcel.listener.ArticleReadListener;
import com.cyz.easyexcel.utils.EasyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;