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

第一步:在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',       //绑定按钮的id
            url: '/grantConfig/uploadGrantFile',      //请求后台路径
            accept: 'file',
            done: function (res) {
                if (res.state == 'success') {
                    layer.msg("导入成功", {icon: 1})
                } else {
                    layer.msg("导入失败", {icon: 2})
                }
            }
        });

------此段代码要放在layui.use中
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值