In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to achieve springboot layui hutool Excel import, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will gain something after reading this springboot layui hutool Excel import article, let's take a look at it.
1. Import dependency (1) preparation of other environments
First of all, the springboot+ front-end framework is built according to the needs, which is based on the personal project, I use springboot+layui. These are not the focus of the discussion here.
(2) hutool and Excel import cn.hutool hutool-all 5.3.8 org.apache.poi poi-ooxml 4.0.0
A wave must be recommended:
II. Core code
(I) Front button
The front-end code is not the core, it's just for an idea.
/ / ① Button Import / / ② / / Import layui upload plug-ins layui.use (["element", "laypage", "layer", "upload"], function () {debugger; var element = layui.element; var laypage = layui.laypage; var layer = layui.layer; var upload = layui.upload / / mainly this layui.upload.render ({elem: "# importData", / / Import 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 ()})
(2) backend code
Controller interface
@ PostMapping (value = "/ importData") @ ResponseBodypublic CommonResult importData (@ RequestParam MultipartFile file) {/ / call the service method, where you can receive the contents of the uploaded Excel file through the MultipartFile parameter: empService.importTemplate (file); return CommonResult.success ("uploaded successfully");}
Service implementation class code
Note: after we receive the file here, we use the ExcelUtil tool in the hutool tool to help us parse the file. When we wait for a line of data, we only need to map to our entity class. Here I just write two fields in an EmpDO, which can be referred to if the business is complex.
@ Overridepublic void importTemplate (MultipartFile file) {InputStream inputStream = null; try {inputStream = file.getInputStream ();} catch (Exception e) {logger.info ("file exception");} / / specify input stream and sheet ExcelReader reader = ExcelUtil.getReader (inputStream, 0); / / read data from the second line to the last line List read = reader.read (1, reader.getRowCount (); List empDOS = new ArrayList () For (List objects: read) {EmpDO empDO = new EmpDO (); / / read a row's first column data Object sampleNo = objects.get (0); / / read a row's second column data Object sampleName = objects.get (1); / / employee id empDO.setEmpId (Integer.parseInt (sampleNo.toString () / / employee name empDO.setName (sampleName.toString ()); empDOS.add (empDO); / / No data is inserted into the database. I use mybatisplus System.out.println (empDO);}} third, test
(1) document preparation:
(2) Select the import file
(3) enter into business processing
This is the end of the article on "how to implement springboot layui hutool Excel Import". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve springboot layui hutool Excel import". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.