您好,登錄后才能下訂單哦!
這篇文章主要介紹了springboot layui hutool Excel導(dǎo)入如何實(shí)現(xiàn)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇springboot layui hutool Excel導(dǎo)入如何實(shí)現(xiàn)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
首先前期根據(jù)需求需要搭建springboot+前端的框架,這個(gè)根據(jù)個(gè)人項(xiàng)目來(lái),我是用的springboot+layui。這些不是這里討論的重點(diǎn)。
<!-- 基本依賴包 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.0</version> </dependency>
必須推薦一波:
(一)前端按鈕
前端代碼不是核心,只是為了一個(gè)思路
//①按鈕 <button id="importData" class="layui-btn">導(dǎo)入</button> //②//導(dǎo)入 用layui upload插件 layui.use([ "element", "laypage", "layer", "upload"], function() { debugger; var element = layui.element; var laypage = layui.laypage; var layer = layui.layer; var upload = layui.upload;//主要是這個(gè) layui.upload.render({ elem: "#importData",//導(dǎo)入id url: "/emp/importData", size: '3072', accept: "file", exts: 'xls|xlsx|xlsm|xlt|xltx|xltm', done: function (result) { if (result.status == 0) { parent.layui.table.reload('LAY-app-emp-list'); } if (result.message != null) { parent.layui.table.reload('LAY-app-emp-list'); layer.msg(result.message) } } }); // refreshTable() });
(二)后端代碼
controller接口
@PostMapping(value = "/importData") @ResponseBody public CommonResult importData(@RequestParam MultipartFile file) { //調(diào)用service方法,這個(gè)地方通過(guò)MultipartFile參數(shù)就可以接收到上傳的Excel文件內(nèi)容了 empService.importTemplate(file); return CommonResult.success("上傳成功"); }
service實(shí)現(xiàn)類代碼
說(shuō)明:這里我們接收到file之后,通過(guò)hutool工具中的ExcelUtil工具幫我們解析文件,等到一行一行的數(shù)據(jù),這個(gè)時(shí)候我們只需要映射到我們的實(shí)體類中。這里我只是寫了一個(gè)EmpDO中的兩個(gè)字段,如果業(yè)務(wù)復(fù)雜參考這兩個(gè)字段來(lái)就行了。
@Override public void importTemplate(MultipartFile file) { InputStream inputStream = null; try { inputStream = file.getInputStream(); }catch (Exception e){ logger.info("文件異常"); } //指定輸入流和sheet ExcelReader reader = ExcelUtil.getReader(inputStream, 0); // 讀取第二行到最后一行數(shù)據(jù) List<List<Object>> read = reader.read(1, reader.getRowCount()); List<EmpDO> empDOS = new ArrayList<>(); for (List<Object> objects : read) { EmpDO empDO = new EmpDO(); //讀取某行第一列數(shù)據(jù) Object sampleNo = objects.get(0); //讀取某行第二列數(shù)據(jù) Object sampleName = objects.get(1); //員工id empDO.setEmpId(Integer.parseInt(sampleNo.toString())); //員工姓名 empDO.setName(sampleName.toString()); empDOS.add(empDO); //這里沒(méi)有做數(shù)據(jù)插入到數(shù)據(jù)庫(kù)的操作,我用的是mybatisplus System.out.println(empDO); } }
(一)文件準(zhǔn)備:
(二)選擇導(dǎo)入文件
(三)進(jìn)入到業(yè)務(wù)處理
關(guān)于“springboot layui hutool Excel導(dǎo)入如何實(shí)現(xiàn)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“springboot layui hutool Excel導(dǎo)入如何實(shí)現(xiàn)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。