最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成EasyExcel實(shí)現(xiàn)Excel導(dǎo)入的方法

 更新時(shí)間:2021年01月07日 11:10:54   作者:CodrBird  
這篇文章主要介紹了SpringBoot集成EasyExcel實(shí)現(xiàn)Excel導(dǎo)入的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

第一次正式的寫(xiě)文章進(jìn)行分享,如果文章中有什么問(wèn)題,歡迎大家在文末的群內(nèi)反饋。

一、背景

為什么會(huì)用Easyexcel來(lái)做Excel上傳

平時(shí)項(xiàng)目中經(jīng)常使用EasyExcel從本地讀取Excel中的數(shù)據(jù),還有一個(gè)前端頁(yè)面對(duì)需要處理的數(shù)據(jù)進(jìn)行一些配置(如:Excel所在的文件夾,Excel的文件名,以及Sheet列名、處理數(shù)據(jù)需要的某些參數(shù)),由于每次都是讀取的本地的文件,我就在想,如果某一天需要通過(guò)前端上傳excel給我,讓我來(lái)進(jìn)行處理我又應(yīng)該怎么辦呢?我怎么才能在盡量少修改代碼的前提下實(shí)現(xiàn)這個(gè)功能呢(由于公司經(jīng)常改需求,項(xiàng)目已經(jīng)重新寫(xiě)了3次了)?后來(lái)查了很多資料,發(fā)現(xiàn)Excel可以使用InPutStream流來(lái)讀取Excel,我就突然明白了什么。

阿里巴巴語(yǔ)雀團(tuán)隊(duì)對(duì)EasyExcel是這樣介紹的

Java解析、生成Excel比較有名的框架有Apache poi、jxl。但他們都存在一個(gè)嚴(yán)重的問(wèn)題就是非常的耗內(nèi)存,
poi有一套SAX模式的API可以一定程度的解決一些內(nèi)存溢出的問(wèn)題,但POI還是有一些缺陷,比如07版Excel解壓
縮以及解壓后存儲(chǔ)都是在內(nèi)存中完成的,內(nèi)存消耗依然很大。easyexcel重寫(xiě)了poi對(duì)07版Excel的解析,能夠原
本一個(gè)3M的excel用POI sax依然需要100M左右內(nèi)存降低到幾M,并且再大的excel不會(huì)出現(xiàn)內(nèi)存溢出,03版依賴
POI的sax模式。在上層做了模型轉(zhuǎn)換的封裝,讓使用者更加簡(jiǎn)單方便。
當(dāng)然還有急速模式能更快,但是內(nèi)存占用會(huì)在100M多一點(diǎn)。

二、集成EasyExcel?

薩達(dá)

1、 在pom.xml中添加EasyExcel依賴

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

2、創(chuàng)建EasyExcel映射實(shí)體類

import com.alibaba.excel.annotation.ExcelProperty;

public class ExcelEntity {
 // ExcelProperty中的參數(shù)要對(duì)應(yīng)Excel中的標(biāo)題
 @ExcelProperty("ID")
 private int ID;

 @ExcelProperty("NAME")
 private String name;

 @ExcelProperty("AGE")
 private int age;

 public ExcelEntity() {
 }

 public ExcelEntity(int ID, String name, int age) {
  this.ID = ID;
  this.name = name;
  this.age = age;
 }

 public int getID() {
  return ID;
 }

 public void setID(int ID) {
  this.ID = ID;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }
}

3、創(chuàng)建自定義Easyexcel的監(jiān)聽(tīng)類

  • 這個(gè)監(jiān)聽(tīng)類里面每一個(gè)ExcelEntity對(duì)象代表一行數(shù)據(jù)
  • 在這個(gè)監(jiān)聽(tīng)類里面可以對(duì)讀取到的每一行數(shù)據(jù)進(jìn)行單獨(dú)操作
  • 這里的讀取的數(shù)據(jù)是按照Excel中每一條數(shù)據(jù)的順序進(jìn)行讀取的
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;

public class UploadExcelListener extends AnalysisEventListener<ExcelEntity> {

 private static final Logger logger = LoggerFactory.getLogger(LoggerItemController.class);
 public static final List<ExcelEntity> list = new ArrayList<>();

 @Override
 public void invoke(ExcelEntity excelEntity, AnalysisContext context) {
  logger.info(String.valueOf(excelEntity.getID()));
  logger.info(excelEntity.getName());
  logger.info(String.valueOf(excelEntity.getAge()));
  list.add(excelEntity);
 }

4、創(chuàng)建controller

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/loggerItem")
public class LoggerItemController {


 // MultipartFile 這個(gè)類一般是用來(lái)接受前臺(tái)傳過(guò)來(lái)的文件
 @PostMapping("/upload")
 public List<ExcelEntity> upload(@RequestParam(value = "multipartFile") MultipartFile multipartFile){
  if (multipartFile == null){
   return null;
  }

  InputStream in = null;
  try {
   // 從multipartFile獲取InputStream流
   in = multipartFile.getInputStream();

   /*
    * EasyExcel 有多個(gè)不同的read方法,適用于多種需求
    * 這里調(diào)用EasyExcel中通過(guò)InputStream流方式讀取Excel的Read方法
    * 他會(huì)返回一個(gè)ExcelReaderBuilder類型的返回值
    * ExcelReaderBuilder中有一個(gè)doReadAll方法,會(huì)讀取所有的Sheet
    */
   EasyExcel.read(in,ExcelEntity.class,new UploadExcelListener())
     .sheet("Sheet1")
     .doRead();

   // 每次EasyExcel的read方法讀取完之后都會(huì)關(guān)閉流,我這里為了試驗(yàn)doReadAll方法,所以重新獲取了一次
   in = multipartFile.getInputStream();
   /*
    * ExcelReaderBuilder中的Sheet方法,需要添加讀取的Sheet名作為參數(shù)
    * 并且不要忘記在后面再調(diào)用一下doReadAll方法,否則不會(huì)進(jìn)行讀取操作
    */

   EasyExcel.read(in,ExcelEntity.class,new UploadExcelListener()).doReadAll();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return UploadExcelListener.list;
 }
}

5、application.yml配置

server:
 # 指定端口號(hào)
 port: 8080
spring:
 servlet:
 multipart:
  # 配置單個(gè)上傳文件大小
  file-size-threshold: 100M
  # 配置總上傳大小
  max-request-size: 300M

6、測(cè)試

我們先搞一個(gè)簡(jiǎn)單的Excel,用來(lái)測(cè)試

在這里插入圖片描述

然后通過(guò)Postman模擬發(fā)送請(qǐng)求

  • 選擇Post請(qǐng)求并輸入請(qǐng)求地址
  • 在下面選擇Body
  • Key的框中輸入controller中的請(qǐng)求的方法中的參數(shù),后面的下拉框中選擇File
  • VALUE框中有一個(gè)Select File ,點(diǎn)擊后選擇自己剛才創(chuàng)建的測(cè)試的Excel
  • 最后點(diǎn)擊Send發(fā)送請(qǐng)求

在這里插入圖片描述

返回值如下:

由于我讀了兩次都放在同一個(gè)List中返回,所以返回值中有8個(gè)對(duì)象。

[
 {
  "name": "小黑",
  "age": 25,
  "id": 1
 },
 {
  "name": "小白",
  "age": 22,
  "id": 2
 },
 {
  "name": "小黃",
  "age": 22,
  "id": 3
 },
 {
  "name": "小綠",
  "age": 23,
  "id": 4
 },
 {
  "name": "小黑",
  "age": 25,
  "id": 1
 },
 {
  "name": "小白",
  "age": 22,
  "id": 2
 },
 {
  "name": "小黃",
  "age": 22,
  "id": 3
 },
 {
  "name": "小綠",
  "age": 23,
  "id": 4
 }
]

三、EasyExcel中的Read方法匯總

/**
  * Build excel the read
  *
  * @return Excel reader builder.
  */
 public static ExcelReaderBuilder read() {
  return new ExcelReaderBuilder();
 }

 /**
  * Build excel the read
  *
  * @param file
  *   File to read.
  * @return Excel reader builder.
  */
 public static ExcelReaderBuilder read(File file) {
  return read(file, null, null);
 }

 /**
  * Build excel the read
  *
  * @param file
  *   File to read.
  * @param readListener
  *   Read listener.
  * @return Excel reader builder.
  */
 public static ExcelReaderBuilder read(File file, ReadListener readListener) {
  return read(file, null, readListener);
 }

 /**
  * Build excel the read
  *
  * @param file
  *   File to read.
  * @param head
  *   Annotate the class for configuration information.
  * @param readListener
  *   Read listener.
  * @return Excel reader builder.
  */
 public static ExcelReaderBuilder read(File file, Class head, ReadListener readListener) {
  ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder();
  excelReaderBuilder.file(file);
  if (head != null) {
   excelReaderBuilder.head(head);
  }
  if (readListener != null) {
   excelReaderBuilder.registerReadListener(readListener);
  }
  return excelReaderBuilder;
 }

 /**
  * Build excel the read
  *
  * @param pathName
  *   File path to read.
  * @return Excel reader builder.
  */
 public static ExcelReaderBuilder read(String pathName) {
  return read(pathName, null, null);
 }

 /**
  * Build excel the read
  *
  * @param pathName
  *   File path to read.
  * @param readListener
  *   Read listener.
  * @return Excel reader builder.
  */
 public static ExcelReaderBuilder read(String pathName, ReadListener readListener) {
  return read(pathName, null, readListener);
 }

 /**
  * Build excel the read
  *
  * @param pathName
  *   File path to read.
  * @param head
  *   Annotate the class for configuration information.
  * @param readListener
  *   Read listener.
  * @return Excel reader builder.
  */
 public static ExcelReaderBuilder read(String pathName, Class head, ReadListener readListener) {
  ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder();
  excelReaderBuilder.file(pathName);
  if (head != null) {
   excelReaderBuilder.head(head);
  }
  if (readListener != null) {
   excelReaderBuilder.registerReadListener(readListener);
  }
  return excelReaderBuilder;
 }

 /**
  * Build excel the read
  *
  * @param inputStream
  *   Input stream to read.
  * @return Excel reader builder.
  */
 public static ExcelReaderBuilder read(InputStream inputStream) {
  return read(inputStream, null, null);
 }

 /**
  * Build excel the read
  *
  * @param inputStream
  *   Input stream to read.
  * @param readListener
  *   Read listener.
  * @return Excel reader builder.
  */
 public static ExcelReaderBuilder read(InputStream inputStream, ReadListener readListener) {
  return read(inputStream, null, readListener);
 }

 /**
  * Build excel the read
  *
  * @param inputStream
  *   Input stream to read.
  * @param head
  *   Annotate the class for configuration information.
  * @param readListener
  *   Read listener.
  * @return Excel reader builder.
  */
 public static ExcelReaderBuilder read(InputStream inputStream, Class head, ReadListener readListener) {
  ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder();
  excelReaderBuilder.file(inputStream);
  if (head != null) {
   excelReaderBuilder.head(head);
  }
  if (readListener != null) {
   excelReaderBuilder.registerReadListener(readListener);
  }
  return excelReaderBuilder;
 }

所有的方法都在這兒了,其實(shí)如果看不懂到底應(yīng)該調(diào)用哪一個(gè)read方法的話,可以以根據(jù)自己所能得到的參數(shù)來(lái)判斷。

四、擴(kuò)展

讀取本地Excel

public static void main(String[] args) {
 EasyExcel.read("C:/Users/Lonely Programmer/Desktop/新建 Microsoft Excel 工作表.xlsx"
     ,ExcelEntity.class
     ,new UploadExcelListener())
  .doReadAll();
}

讀取本地的Excel和通過(guò)InPutStream流讀取的方式是一樣的,只是參數(shù)變了,原本傳的是InPutStream流,現(xiàn)在傳的是文件的絕對(duì)路徑。我這里監(jiān)聽(tīng)類和映射實(shí)體類都沒(méi)有變,和上傳用的是同一個(gè),大家也可以根據(jù)需求來(lái)設(shè)定自己的監(jiān)聽(tīng)類與實(shí)體類

MultipartFile文檔

MultipartFile文檔地址:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html

翻譯是通過(guò)Google Chrome自帶翻譯插件進(jìn)行翻譯的,建議大家使用Google Chrome打開(kāi),自帶翻譯功能

在這里插入圖片描述

到此這篇關(guān)于SpringBoot集成EasyExcel實(shí)現(xiàn)Excel導(dǎo)入的方法的文章就介紹到這了,更多相關(guān)SpringBoot實(shí)現(xiàn)Excel導(dǎo)入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

缙云县| 辉南县| 隆尧县| 香港| 吉水县| 吉木萨尔县| 霍林郭勒市| 手机| 南投市| 井冈山市| 民勤县| 黑水县| 长垣县| 河间市| 康乐县| 克什克腾旗| 肇庆市| 肥城市| 新民市| 蓝田县| 济南市| 滕州市| 肇源县| 城固县| 海安县| 邹平县| 留坝县| 乐清市| 太湖县| 佛冈县| 沽源县| 建阳市| 涪陵区| 丰宁| 德安县| 陇南市| 尼木县| 当雄县| 汕头市| 石泉县| 双峰县|