一、创建需要导入的数据实体类,并给要导入的数据加上@Excel注解
在这里插入图片描述
二、在service层写导入方法
在这里插入图片描述
三、实现类方法,因为我的业务问题并没有往数据库插入数据,而是在前端回显数据,如果需要往数据库里面插入数据直接调用你的插入方法即可。

@Override
    public String importDrill(List<DrillingInformation> listDrill, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(listDrill) || listDrill.size() == 0)
        {
            throw new ServiceException("导入钻孔数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (DrillingInformation ldi : listDrill)
        {
            try
            {
                if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, ldi);
                    ldi.setCreateBy(operName);
                    successNum++;
                }
                else
                {
                    failureNum++;
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = " 导入失败:";
                failureMsg.append(msg + e.getMessage());
            }
            TaskDetailInfo taskDetailInfo = new TaskDetailInfo();
            BeanUtils.copyProperties(ldi,taskDetailInfo);

        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        }
        return successMsg.toString();
    }

四、在Controller层我去除了对数据库的操作
在这里插入图片描述

五,前端页面也需要进行修改
导入按钮代码

<el-col :span="1.5">
              <el-button
                type="info"
                plain
                icon="el-icon-upload2"
                size="mini"
                @click="handleImport"
                v-hasPermi="['']"
              >导入</el-button>
            </el-col>

六、导入方法代码
把这四个方法加进去就可以了

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "钻孔信息导入"; // todo
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('drill/taskPlan/importTemplate', {
      }, `drill_template_${new Date().getTime()}.xlsx`)  // todo
    },
// 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
// 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      debugger
      this.taskDetailInfoList= response.data
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },

这样导入操作就做完了,点击下载模板按钮可以下载你规定的格式
在这里插入图片描述

Logo

快速构建 Web 应用程序

更多推荐