Home  >  Article  >  Java  >  How to implement springboot layui hutool Excel import

How to implement springboot layui hutool Excel import

WBOY
WBOYforward
2023-05-14 17:04:171547browse

    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

    <!-- 基本依赖包 -->
    <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>

    Must recommend:

    2. Core code

    (1) Front-end button

    The front-end code is not the core, just for an idea

    //①按钮
    <button id="importData" class="layui-btn">导入</button>
    //②//导入 用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: &#39;3072&#39;,
                accept: "file",
                exts: &#39;xls|xlsx|xlsm|xlt|xltx|xltm&#39;,
                done: function (result) {
                    if (result.status == 0) {
                        parent.layui.table.reload(&#39;LAY-app-emp-list&#39;);
                    }
                    if (result.message != null) {
                        parent.layui.table.reload(&#39;LAY-app-emp-list&#39;);
                        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<List<Object>> read = reader.read(1, reader.getRowCount());
        List<EmpDO> empDOS = new ArrayList<>();
        for (List<Object> 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);
        }
    }

    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!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete