java利用easyexcel實現(xiàn)導入與導出功能
前言
poi的解析方式是dom解析,把結果一次都讀入內(nèi)存操作,這樣的操作平時是不會有問題的,但是并發(fā)量上來的時候就會出現(xiàn)OOM,EasyExcel,底層對象其實還是使用poi包的那一套。它只是將poi包的一部分抽了出來,摒棄掉了大部分業(yè)務相關的屬性。由于它關注的業(yè)務是導入導出這一塊,所以在處理大數(shù)據(jù)量的導入導出能夠通過本地緩存來避免OOM,在特定場景中,EasyExcel的表現(xiàn)能力還是可以的。
1先添加依賴
<!-- EasyExcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.0-beta2</version>
</dependency>2批量插入數(shù)據(jù)
再試下導入導出功能前,寫批量插入數(shù)據(jù)的接口和查詢數(shù)據(jù)的接口。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.demo.mapper.MedicnesMapper">
<insert id="saveMedicnesList" parameterType="cn.demo.entity.Medicnes">
insert into medices
(food,remark)
values
<foreach collection="list" item="rm" separator=",">
(#{rm.food},#{rm.remark})
</foreach>
</insert>
<select id="medicnesList" parameterType="cn.demo.entity.Medicnes" resultType="cn.demo.entity.Medicnes">
SELECT
food,
remark
FROM
medices
</select>
</mapper>3創(chuàng)建需要導出數(shù)據(jù)實體類
package cn.demo.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Medicnes {
@ExcelProperty("食物名稱")
private String food;
@ExcelProperty("食物產(chǎn)地")
private String remark;
}4創(chuàng)建一個類ExcelListener
package cn.demo.config;
import cn.demo.entity.Medicnes;
import cn.demo.service.EmpService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
/**
* 有個很重要的點 ExcelListener
* 不能被spring管理,要每次讀取excel都要new,然后里面用到spring可以構造方法傳進去
*/
public class ExcelListener extends AnalysisEventListener<Medicnes> {
private List<Medicnes> list = new ArrayList<>();
//每隔5條存儲數(shù)據(jù)庫,實際使用中可以3000條,然后清理list ,方便內(nèi)存回收
private static final int BATCH_COUNT = 5;
//假設這個是一個DAO,當然有業(yè)務邏輯這個也可以是一個service。當然如果不用存儲這個對象用
@Autowired
private EmpService empService;
/**
* 如果使用了spring,請使用這個構造方法。
* 每次創(chuàng)建Listener的時候需要把spring管理的類傳進來
*/
public ExcelListener(EmpService empService) {
this.empService = empService;
}
/**
* 這個每一條數(shù)據(jù)解析都會來調(diào)用
*/
@Override
public void invoke(Medicnes goods, AnalysisContext analysisContext) {
System.out.println("解析到一條數(shù)據(jù):========================"+goods.toString());
// 數(shù)據(jù)存儲到data,供批量處理或后續(xù)自己業(yè)務邏輯處理。
list.add(goods);
// 達到BATCH_COUNT了,需要去存儲一次數(shù)據(jù)庫,防止數(shù)據(jù)幾萬條數(shù)據(jù)在內(nèi)存,容易OOM
if(list.size() >= BATCH_COUNT){
saveData();
// 存儲完成清理data
list.clear();
}
}
/**
* 所有數(shù)據(jù)解析完成了 都會來調(diào)用
*/
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//確保所有數(shù)據(jù)都能入庫
saveData();
}
/**
* 加上存儲數(shù)據(jù)庫
*/
private void saveData() {
System.out.println("=============================="+list.size()+"條數(shù)據(jù),開始存儲到數(shù)據(jù)庫");
empService.saveMedicnesList(list);
}
}5實現(xiàn)下載excel
接下來編寫我們的工具類--幫助我們來實現(xiàn)下載excel
public class DownExcel {
public static void download(HttpServletResponse response, Class t, List list) throws IOException, IllegalAccessException,InstantiationException {
response.setContentType("application/vnd.ms-excel");// 設置文本內(nèi)省
response.setCharacterEncoding("utf-8");// 設置字符編碼
response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); // 設置響應頭
EasyExcel.write(response.getOutputStream(), t).sheet("模板").doWrite(list); //用io流來寫入數(shù)據(jù)
}
}
//導出為Excel
@RequestMapping("/downloadexcel.do")
public void getExcel(HttpServletResponse response) throws IllegalAccessException, IOException,
InstantiationException {
List<Medicnes> list = sysUserService.medicnesList();
DownExcel.download(response,Medicnes.class,list);
}6控制器添加我們的導入操作代碼
//導入Excel
@RequestMapping("/importexcel.do")
@ResponseBody
public String importexcel(@RequestParam(value = "excelFile") MultipartFile file) throws IOException{
EasyExcel.read(file.getInputStream(), Medicnes.class, new ExcelListener(sysUserService)).sheet().doRead();
return "success";
}7導出效果如圖

8導入直接調(diào)用

到此這篇關于java利用easyexcel實現(xiàn)導入與導出功能的文章就介紹到這了,更多相關java easyexce 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java整合mybatis實現(xiàn)過濾數(shù)據(jù)
這篇文章主要介紹了Java整合mybatis實現(xiàn)過濾數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01
java連接postgresql數(shù)據(jù)庫代碼及maven配置方式
這篇文章主要介紹了java連接postgresql數(shù)據(jù)庫代碼及maven配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
Intellij IDEA集成JProfiler性能分析工具
作為Java程序員,性能分析是我們必須掌握的技能之一,在性能分析中,JProfiler是一款非常強大的工具,本文就來介紹一下Intellij IDEA集成JProfiler性能分析工具,就有一定的參考價值,感興趣的可以了解一下2023-12-12
Spring Boot jar可執(zhí)行原理的徹底分析
這篇文章主要給大家介紹了關于Spring Boot jar可執(zhí)行原理的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-07-07
SpringBoot中基于AOP和Semaphore實現(xiàn)API限流
調(diào)用速率限制是 Web API 中的常見要求,旨在防止濫用并確保公平使用資源,借助Spring Boot 中的 AOP,我們可以通過攔截方法調(diào)用并限制在特定時間范圍內(nèi)允許的請求數(shù)量來實現(xiàn)速率限制,需要的朋友可以參考下2024-10-10

