Firstly, build the springboot front-end framework according to the needs in the early stage. This is based on For personal projects, I use springboot layui. These are not the focus of the discussion here.
cn.hutool hutool-all 5.3.8 org.apache.poi poi-ooxml 4.0.0
Must recommend:
(1) Front-end button
The front-end code is not the core, just for an idea
//①按钮 //②//导入 用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;//主要是这个 layui.upload.render({ elem: "#importData",//导入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) Back-end code
controller interface
@PostMapping(value = "/importData") @ResponseBody public CommonResult importData(@RequestParam MultipartFile file) { //调用service方法,这个地方通过MultipartFile参数就可以接收到上传的Excel文件内容了 empService.importTemplate(file); return CommonResult.success("上传成功"); }
service implementation class code
Description : After we receive the file here, we use the ExcelUtil tool in the hutool tool to help us parse the file, and wait until the data is line by line. At this time, we only need to map it to our entity class. Here I just wrote two fields in EmpDO. If the business is complicated, just refer to these two fields.
@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); // 读取第二行到最后一行数据 List> read = reader.read(1, reader.getRowCount()); List
empDOS = new ArrayList<>(); for (List
(1) File preparation:
## (2) Select the import file (3) Enter business processingThe above is the detailed content of How to implement springboot layui hutool Excel import. For more information, please follow other related articles on the PHP Chinese website!