Spring?boot集成easy?excel實現(xiàn)導(dǎo)入導(dǎo)出功能
Spring boot集成easy excel實現(xiàn)導(dǎo)入導(dǎo)出操作
一 查看官網(wǎng)
easyexcel官方網(wǎng)站地址為easyexcel官網(wǎng),官網(wǎng)的信息比較齊全,可以查看官網(wǎng)使用easyexcel的功能。
二 引入依賴
使用easyexcel,首先要引入easyexcel的maven依賴,具體的版本根據(jù)你的需求去設(shè)置。
<!--easyexcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>三 實現(xiàn)簡單導(dǎo)入
首先定義實體類
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Device {
@ExcelIgnore
private Integer id;
@ExcelProperty("設(shè)備名稱")
private String name;
@ExcelProperty("設(shè)備編號")
private String no;
@ExcelProperty("設(shè)備描述")
private String description;
@ExcelProperty("設(shè)備類型")
private Integer type;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ExcelIgnore
private LocalDateTime createTime;
@ExcelIgnore
private Integer status;
}在定義實體類的時候,使用到了lombok,需要提前引入lombok的依賴
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>準備工作完成之后,就可以寫一個簡單的導(dǎo)入了。如下,我在controller中寫了導(dǎo)入方法,通過EasyExcel的read方法把excel中的數(shù)據(jù)解析成對應(yīng)的列表,然后就可以直接調(diào)用service導(dǎo)入了。
@RequestMapping("save")
public String save(MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
List<Device> list = EasyExcel.read(file.getInputStream()).head(Device.class).sheet().doReadSync();
deviceService.batchSave(list);
return "redirect:/device/lists";
}四 實現(xiàn)簡單導(dǎo)出
在controller寫了簡單的導(dǎo)出方法,拿到service得到的數(shù)據(jù),就可以直接調(diào)用EasyExcel的write方法導(dǎo)出了。
@GetMapping("export")
public void export(Dto dto,HttpServletResponse response) throws IOException {
// 這里注意 有同學(xué)反應(yīng)使用swagger 會導(dǎo)致各種問題,請直接用瀏覽器或者用postman
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 這里URLEncoder.encode可以防止中文亂碼
String fileName = URLEncoder.encode("設(shè)備數(shù)據(jù)", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
List<Device> deviceList = deviceService.getDeviceList(dto);
EasyExcel.write(response.getOutputStream(), Device.class).sheet("數(shù)據(jù)").doWrite(deviceList);
}五 批量導(dǎo)出功能
請參考easyexcel實現(xiàn)批量導(dǎo)出功能
總結(jié)
使用easyexcel實現(xiàn)導(dǎo)入和導(dǎo)出確實是非常方便的,同時,easyexcel還支持批量導(dǎo)入和批量導(dǎo)出,確實非常nice。
到此這篇關(guān)于Spring boot集成easy excel實現(xiàn)導(dǎo)入導(dǎo)出操作的文章就介紹到這了,更多相關(guān)Spring boot集成easy excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot EasyPoi動態(tài)導(dǎo)入導(dǎo)出的兩種方式實現(xiàn)方法詳解
- SpringBoot+Vue實現(xiàn)EasyPOI導(dǎo)入導(dǎo)出的方法詳解
- 使用SpringBoot+EasyExcel+Vue實現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
- Spring?Boot?+?EasyExcel實現(xiàn)數(shù)據(jù)導(dǎo)入導(dǎo)出
- 使用VUE+SpringBoot+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的教程詳解
- SpringBoot整合EasyExcel實現(xiàn)文件導(dǎo)入導(dǎo)出
- SpringBoot中EasyExcel實現(xiàn)Excel文件的導(dǎo)入導(dǎo)出
相關(guān)文章
基于javamelody監(jiān)控springboot項目過程詳解
這篇文章主要介紹了基于javamelody監(jiān)控springboot項目過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
解決Mybatis中foreach嵌套使用if標簽對象取值的問題
這篇文章主要介紹了解決Mybatis中foreach嵌套使用if標簽對象取值的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringBoot報錯Invalid?bound?statement?(not?found)問題排查和解決方案
這篇文章主要介紹了SpringBoot報錯Invalid?bound?statement?(not?found)問題排查和解決方案,文中通過圖文結(jié)合的方式講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03
springsecurity第三方授權(quán)認證的項目實踐
Spring security 是一個強大的和高度可定制的身份驗證和訪問控制框架,本文主要介紹了springsecurity第三方授權(quán)認證的項目實踐,具有一定的參考價值,感興趣可以了解一下2023-08-08
Springboot+rabbitmq實現(xiàn)延時隊列的兩種方式
這篇文章主要介紹了Springboot+rabbitmq實現(xiàn)延時隊列的兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05

