Home> Java> javaTutorial> body text

How to implement springboot layui hutool Excel import

WBOY
Release: 2023-05-14 17:04:17
forward
1503 people have browsed it

    1. Import dependencies

    (1) Other environment preparation

    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.

    (2) hutool and Excel import

      cn.hutool hutool-all 5.3.8   org.apache.poi poi-ooxml 4.0.0 
    Copy after login

    Must recommend:

    2. Core code

    (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() });
    Copy after login

    (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("上传成功"); }
    Copy after login

    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 objects : read) { EmpDO empDO = new EmpDO(); //读取某行第一列数据 Object sampleNo = objects.get(0); //读取某行第二列数据 Object sampleName = objects.get(1); //员工id empDO.setEmpId(Integer.parseInt(sampleNo.toString())); //员工姓名 empDO.setName(sampleName.toString()); empDOS.add(empDO); //这里没有做数据插入到数据库的操作,我用的是mybatisplus System.out.println(empDO); } }
            
    Copy after login

    3. Test

    (1) File preparation:

    How to implement springboot layui hutool Excel import

    ## (2) Select the import file

    How to implement springboot layui hutool Excel import

    (3) Enter business processing

    How to implement springboot layui hutool Excel import

    The 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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!