使用hutool工具進行導(dǎo)入導(dǎo)出excel表格
如何在后臺添加導(dǎo)入導(dǎo)出表格的功能呢,本期的文章將會帶領(lǐng)小伙伴們一起實現(xiàn)此功能
1.先引入hutool的相關(guān)依賴
<!--hutool--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.20</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>
2.導(dǎo)出
創(chuàng)建一個Controller進行測試
//表格導(dǎo)出接口 @GetMapping("/export") public void export(HttpServletResponse response) throws IOException { //查詢所有用戶 List<User> list= userService.list(); //在內(nèi)存操作,寫到瀏覽器 ExcelWriter writer= ExcelUtil.getWriter(true); //自定義標(biāo)題別名 writer.addHeaderAlias("username","用戶名"); writer.addHeaderAlias("password","密碼"); writer.addHeaderAlias("nickname","昵稱"); writer.addHeaderAlias("email","郵箱"); writer.addHeaderAlias("phone","電話"); writer.addHeaderAlias("address","地址"); writer.addHeaderAlias("createTime","創(chuàng)建時間"); //默認配置 writer.write(list,true); //設(shè)置content—type response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8"); //設(shè)置標(biāo)題 String fileName= URLEncoder.encode("用戶信息","UTF-8"); //Content-disposition是MIME協(xié)議的擴展,MIME協(xié)議指示MIME用戶代理如何顯示附加的文件。 response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx"); ServletOutputStream outputStream= response.getOutputStream(); //將Writer刷新到OutPut writer.flush(outputStream,true); outputStream.close(); writer.close(); }
測試結(jié)果如下
Vue前端使用這個代碼進行訪問即可
//點擊函數(shù)exp, 導(dǎo)出表格 exp(){ window.open("http://localhost:9090/user/export") }
3.導(dǎo)入
創(chuàng)建一個Controller進行測試
因為我前面導(dǎo)出設(shè)置了別名,所以這里我選擇用第二種方式,
接下來可以去postman驗證是否能實現(xiàn)功能
/** * 導(dǎo)入excel * @param file */ @PostMapping("/import") public void importExcel(MultipartFile file) throws IOException { //1.第一種 頭必須和實體(英文)一樣 //文件處理成io流 InputStream in = file.getInputStream(); // //io流給ExcelReader ExcelReader excelReader=ExcelUtil.getReader(in); // //讀取數(shù)據(jù)且轉(zhuǎn)化為list // List<User> list = excelReader.readAll(User.class); //2.第二種導(dǎo)入方式 //忽略第一行頭(第一行是中文的情況),直接讀取表的內(nèi)容 List<List<Object>> list = excelReader.read(1); List<User> listUser = CollUtil.newArrayList(); for (List<Object> row: list) { User user=new User(); user.setUsername(row.get(0).toString()); user.setPassword(row.get(1).toString()); user.setNickname(row.get(2).toString()); user.setNickname(row.get(3).toString()); user.setPhone(row.get(4).toString()); user.setAddress(row.get(5).toString()); listUser.add(user); // ****類似一一對應(yīng)**** } //批量注冊進數(shù)據(jù)庫 userService.saveBatch(listUser); }
前端的話我是用的elemen-ui
<el-upload action="http://localhost:9090/user/import" :show-file-list="false" accept="xlsx" :on-success="handleImportSuccess" style="display: inline-block;margin-right: 5px"> <el-button type="primary">導(dǎo)入 <i class="el-icon-bottom"></i></el-button> </el-upload>
on-success是一個鉤子函數(shù),彈了一個插入成功的信息跟刷新頁面的操作
//導(dǎo)入,鉤子函數(shù)進行頁面的提示跟成功后頁面的刷新 handleImportSuccess(){ this.$message.success("導(dǎo)入成功") this.load(); }
4.效果圖
以上就是使用hutool工具進行導(dǎo)入導(dǎo)出excel表格的詳細內(nèi)容,更多關(guān)于hutool導(dǎo)入導(dǎo)出excel表格的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在SpringBoot中使用YourKit進行性能調(diào)優(yōu)的教程詳解
在應(yīng)用程序的開發(fā)過程中,性能調(diào)優(yōu)是一個重要的環(huán)節(jié),在SpringBoot應(yīng)用程序中,我們可以使用YourKit來進行性能調(diào)優(yōu),YourKit是一款非常強大的Java性能調(diào)優(yōu)工具,在本文中,我們將介紹如何在 SpringBoot應(yīng)用程序中使用YourKit進行性能調(diào)優(yōu)2023-06-06深入了解JVM(Java虛擬機)內(nèi)存結(jié)構(gòu)
Java虛擬機(Java Virtual Machine,JVM)是Java程序的運行環(huán)境,它是一個抽象的計算機模型,通過解釋和執(zhí)行Java字節(jié)碼來運行Java程序,本將大家深入了解JVM(Java虛擬機)內(nèi)存結(jié)構(gòu),需要的朋友可以參考下2023-08-08深入淺析Mybatis與Hibernate的區(qū)別與用途
這篇文章主要介紹了Mybatis與Hibernate的區(qū)別與用途的相關(guān)資料,需要的朋友可以參考下2017-10-10Java中驗證 Mybatis 數(shù)據(jù)分片可以減輕GC壓力的操作方法
這篇文章主要介紹了Java中驗證 Mybatis 數(shù)據(jù)分片可以減輕GC壓力的操作方法,本文使用 Spock(可集成Spring Boot項目) 編寫測試用例,基于 Groovy (JVM語言),感興趣的朋友跟隨小編一起看看吧2024-12-12IDEA中創(chuàng)建maven項目webapp目錄無法識別即未被標(biāo)識的解決辦法
在學(xué)習(xí)SpringMVC課程中,基于IDEA新建maven項目模塊后,webapp目錄未被標(biāo)識,即沒有小藍點的圖標(biāo)顯示,所以本文給大家介紹了IDEA中創(chuàng)建maven項目webapp目錄無法識別即未被標(biāo)識的解決辦法,需要的朋友可以參考下2024-03-03