0
点赞
收藏
分享

微信扫一扫

java用easypoi完成数据导入(结合layui)

Sophia的玲珑阁 2022-05-02 阅读 47
java

第一步:在pom文件中导入如下依赖

    <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>

第二步在实体需要导入的字段上面添加如下注解

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
-------导包

@Excel(name = "配置项值")        ----excel表头名
@TableField("PRIMARILY")        ----数据库中的字段名

第三步代码

public JSONObject uploadExcel(MultipartFile multipartFile, HttpServletResponse response) {
        try {
            ImportParams params = new ImportParams();
            params.setHeadRows(1);
            List<grantConfig> result = ExcelImportUtil.importExcel(multipartFile.getInputStream(), grantConfig.class, params);
            //创建一个集合用于存放map数据
            List<HashMap<String, Object>> hashMaps = new ArrayList<>();
            HashMap<String, Object> map;
            for (grantConfig grantConfig : result) {
                map = new HashMap<>();
                map.put("primarily", grantConfig.getPrimarily());
                map.put("critical", grantConfig.getCritical());
                map.put("key", grantConfig.getKey());
                map.put("remarks", grantConfig.getRemarks());
                hashMaps.add(map);
            }
            insert(hashMaps);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("state", "success");
           return

按钮

<button type="button" class="layui-btn" id="importFile" style="margin-top: 1%; margin-right: 1%; float: right">
    <i class="layui-icon">&#xe681;导入</i>
</button>

实现

//上传文件
        upload.render({
            elem: '#importFile',
            url: '/grantConfig/uploadGrantFile',
            accept: 'file',
            done: function (res) {
                if (res.state == 'success') {
                    layer.msg("导入成功", {icon: 1})
                } else {
                    layer.msg("导入失败", {icon: 2})
                }
            }
        });

------此段代码要放在layui.use
举报

相关推荐

0 条评论