Spring?boot集成easy?excel實現(xiàn)導入導出功能
Spring boot集成easy excel實現(xiàn)導入導出操作
一 查看官網(wǎng)
easyexcel官方網(wǎng)站地址為easyexcel官網(wǎng),官網(wǎng)的信息比較齊全,可以查看官網(wǎng)使用easyexcel的功能。
二 引入依賴
使用easyexcel,首先要引入easyexcel的maven依賴,具體的版本根據(jù)你的需求去設置。
<!--easyexcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>三 實現(xiàn)簡單導入
首先定義實體類
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Device {
@ExcelIgnore
private Integer id;
@ExcelProperty("設備名稱")
private String name;
@ExcelProperty("設備編號")
private String no;
@ExcelProperty("設備描述")
private String description;
@ExcelProperty("設備類型")
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>準備工作完成之后,就可以寫一個簡單的導入了。如下,我在controller中寫了導入方法,通過EasyExcel的read方法把excel中的數(shù)據(jù)解析成對應的列表,然后就可以直接調(diào)用service導入了。
@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)簡單導出
在controller寫了簡單的導出方法,拿到service得到的數(shù)據(jù),就可以直接調(diào)用EasyExcel的write方法導出了。
@GetMapping("export")
public void export(Dto dto,HttpServletResponse response) throws IOException {
// 這里注意 有同學反應使用swagger 會導致各種問題,請直接用瀏覽器或者用postman
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 這里URLEncoder.encode可以防止中文亂碼
String fileName = URLEncoder.encode("設備數(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);
}五 批量導出功能
總結
使用easyexcel實現(xiàn)導入和導出確實是非常方便的,同時,easyexcel還支持批量導入和批量導出,確實非常nice。
到此這篇關于Spring boot集成easy excel實現(xiàn)導入導出操作的文章就介紹到這了,更多相關Spring boot集成easy excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot EasyPoi動態(tài)導入導出的兩種方式實現(xiàn)方法詳解
- SpringBoot+Vue實現(xiàn)EasyPOI導入導出的方法詳解
- 使用SpringBoot+EasyExcel+Vue實現(xiàn)excel表格的導入和導出詳解
- Spring?Boot?+?EasyExcel實現(xiàn)數(shù)據(jù)導入導出
- 使用VUE+SpringBoot+EasyExcel?整合導入導出數(shù)據(jù)的教程詳解
- SpringBoot整合EasyExcel實現(xiàn)文件導入導出
- SpringBoot中EasyExcel實現(xiàn)Excel文件的導入導出
相關文章
基于Java的Socket多客戶端Client-Server聊天程序的實現(xiàn)
這篇文章主要介紹了基于Java的Socket多客戶端Client-Server聊天程序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
Java調(diào)用Oss JDk實現(xiàn)刪除指定目錄下的所有文件
這篇文章主要為大家詳細介紹了Java如何調(diào)用Oss JDk實現(xiàn)刪除指定目錄下的所有文件功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2025-03-03
Spring原生Rpc六種的正確打開方式實現(xiàn)示例
這篇文章主要為大家展示了Spring原生Rpc六種的正確打開方式實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進步早日升職加薪2022-02-02

