亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot集成EasyExcel實現Excel導入的方法

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

第一次正式的寫文章進行分享,如果文章中有什么問題,歡迎大家在文末的群內反饋。

一、背景

為什么會用Easyexcel來做Excel上傳

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

阿里巴巴語雀團隊對EasyExcel是這樣介紹的

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

二、集成EasyExcel?

薩達

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

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

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

import com.alibaba.excel.annotation.ExcelProperty;

public class ExcelEntity {
 // ExcelProperty中的參數要對應Excel中的標題
 @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)聽類

  • 這個監(jiān)聽類里面每一個ExcelEntity對象代表一行數據
  • 在這個監(jiān)聽類里面可以對讀取到的每一行數據進行單獨操作
  • 這里的讀取的數據是按照Excel中每一條數據的順序進行讀取的
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 這個類一般是用來接受前臺傳過來的文件
 @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 有多個不同的read方法,適用于多種需求
    * 這里調用EasyExcel中通過InputStream流方式讀取Excel的Read方法
    * 他會返回一個ExcelReaderBuilder類型的返回值
    * ExcelReaderBuilder中有一個doReadAll方法,會讀取所有的Sheet
    */
   EasyExcel.read(in,ExcelEntity.class,new UploadExcelListener())
     .sheet("Sheet1")
     .doRead();

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

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

5、application.yml配置

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

6、測試

我們先搞一個簡單的Excel,用來測試

在這里插入圖片描述

然后通過Postman模擬發(fā)送請求

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

在這里插入圖片描述

返回值如下:

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

[
 {
  "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;
 }

所有的方法都在這兒了,其實如果看不懂到底應該調用哪一個read方法的話,可以以根據自己所能得到的參數來判斷。

四、擴展

讀取本地Excel

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

讀取本地的Excel和通過InPutStream流讀取的方式是一樣的,只是參數變了,原本傳的是InPutStream流,現在傳的是文件的絕對路徑。我這里監(jiān)聽類和映射實體類都沒有變,和上傳用的是同一個,大家也可以根據需求來設定自己的監(jiān)聽類與實體類

MultipartFile文檔

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

翻譯是通過Google Chrome自帶翻譯插件進行翻譯的,建議大家使用Google Chrome打開,自帶翻譯功能

在這里插入圖片描述

到此這篇關于SpringBoot集成EasyExcel實現Excel導入的方法的文章就介紹到這了,更多相關SpringBoot實現Excel導入內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot入坑筆記之spring-boot-starter-web 配置文件的使用

    SpringBoot入坑筆記之spring-boot-starter-web 配置文件的使用

    本篇向小伙伴介紹springboot配置文件的配置,已經全局配置參數如何使用的。需要的朋友跟隨腳本之家小編一起學習吧
    2018-01-01
  • java版簡單的猜數字游戲實例代碼

    java版簡單的猜數字游戲實例代碼

    猜數字游戲是一款經典的游戲,該游戲說簡單也很簡單,說不簡單確實也很難,那么下面這篇文章主要給大家介紹了java版簡單的猜數字游戲的相關資料,文中給出了詳細的實現分析和示例代碼供大家參考學習,需要的朋友們下面來一起看看吧。
    2017-05-05
  • Java實現的文本字符串操作工具類實例【數據替換,加密解密操作】

    Java實現的文本字符串操作工具類實例【數據替換,加密解密操作】

    這篇文章主要介紹了Java實現的文本字符串操作工具類,可實現數據替換、加密解密等操作,涉及java字符串遍歷、編碼轉換、替換等相關操作技巧,需要的朋友可以參考下
    2017-10-10
  • Java 使用 HttpClient 發(fā)送 GET請求和 POST請求

    Java 使用 HttpClient 發(fā)送 GET請求和 POST請求

    本文主要介紹了Java 使用 HttpClient 發(fā)送 GET請求和 POST請求,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Mybatis Plus整合PageHelper分頁的實現示例

    Mybatis Plus整合PageHelper分頁的實現示例

    這篇文章主要介紹了Mybatis Plus整合PageHelper分頁的實現示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 關于ReentrantLock的實現原理解讀

    關于ReentrantLock的實現原理解讀

    這篇文章主要介紹了關于ReentrantLock的實現原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • java swing中實現拖拽功能示例

    java swing中實現拖拽功能示例

    這篇文章主要介紹了java swing中實現拖拽功能示例,需要的朋友可以參考下
    2014-04-04
  • java 使用JDBC構建簡單的數據訪問層實例詳解

    java 使用JDBC構建簡單的數據訪問層實例詳解

    以下是如何使用JDBC構建一個數據訪問層,包括數據轉換(將從數據庫中查詢的數據封裝到對應的對象中……),數據庫的建立,以及如何連接到數據庫,需要的朋友可以參考下
    2016-11-11
  • java獲取服務器基本信息的方法

    java獲取服務器基本信息的方法

    這篇文章主要介紹了java獲取服務器基本信息的方法,涉及java獲取系統(tǒng)CPU、內存及操作系統(tǒng)等相關信息的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • 詳解JVM的內存對象介紹[創(chuàng)建和訪問]

    詳解JVM的內存對象介紹[創(chuàng)建和訪問]

    這篇文章主要介紹了JVM的內存對象介紹[創(chuàng)建和訪問],文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03

最新評論