您的位置:首页 > 汽车 > 时评 > 四库虎紧急入口2022_成都疫情最新情况风险等级_安徽网站设计_网站推广外贸

四库虎紧急入口2022_成都疫情最新情况风险等级_安徽网站设计_网站推广外贸

2025/6/5 23:56:59 来源:https://blog.csdn.net/yah_529/article/details/146602225  浏览:    关键词:四库虎紧急入口2022_成都疫情最新情况风险等级_安徽网站设计_网站推广外贸
四库虎紧急入口2022_成都疫情最新情况风险等级_安徽网站设计_网站推广外贸

一.创建数据表

        在数据库中创建需要用到的数据表。

二.后端设置

官方文档中对导入功能有说明:功能组件 | RuoYihttps://doc.ruoyi.vip/ruoyi/document/gnzj.html#%E5%AF%BC%E5%85%A5%E5%8A%9F%E8%83%BD

并且系统有导入功能示例,可以参考 ruoyi-admin的SysUserController文件,如下

自定义实现

controller层:

// 导入方法
@PostMapping("/importData")
@ResponseBody
public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
{ExcelUtil<Xxxx> util = new ExcelUtil<Xxxx>(Xxxx.class);List<Xxxx> xxxxList = util.importExcel(file.getInputStream());String message = xxxxService.importXxxx(xxxxList, updateSupport);return AjaxResult.success(message);
}

MultipartFile file:传入的Excel文件;

boolean updateSupport:是否更新已经存在的用户数据。

service层:数据处理逻辑写在这里,以下为简单的框架。

/*** 导入数据** @param xxxxList 数据列表* @param updateSupport 是否更新支持,如果已存在,则进行更新数据* @return 结果*/@Overridepublic String importXxxx(List<Xxxx> xxxxList, boolean updateSupport){if (StringUtils.isNull(xxxxList) || xxxxList.size() == 0){throw new ServiceException("导入用户数据不能为空!");}int successNum = 0;int failureNum = 0;StringBuilder successMsg = new StringBuilder();StringBuilder failureMsg = new StringBuilder();for (Xxxx xxxx : xxxxList){try{if (!updateSupport){xxxxMapper.insertXxxx(xxxx);successNum++;successMsg.append("<br/>" + successNum + " 导入成功");}else {xxxxMapper.updateXxxx(xxxx);successNum++;successMsg.append("<br/>" + successNum  + " 更新成功");}}catch (Exception e){failureNum++;String msg = "<br/>" + failureNum  + " 导入失败:";failureMsg.append(msg + e.getMessage());}}if (failureNum > 0){failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");throw new ServiceException(failureMsg.toString());}else{successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");}return successMsg.toString();}

domain层:

   变量声明要加@Excel,如

 /** 姓名 */@Excel(name = "姓名")private String xm;

三.前端

 <el-col :span="1.5"><el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport">导入</el-button></el-col>
 <el-dialog :title="upload.titleImport" :visible.sync="upload.openImport" width="400px" append-to-body><el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers":action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading":on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag><i class="el-icon-upload"></i><div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div><div class="el-upload__tip text-center" slot="tip"><div class="el-upload__tip" slot="tip"><el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据</div><span>仅允许导入xls、xlsx格式文件。</span><el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"@click="importTemplate">下载模板</el-link></div></el-upload><div slot="footer" class="dialog-footer"><el-button type="primary" @click="submitFileForm">确 定</el-button><el-button @click="upload.open = false">取 消</el-button></div></el-dialog>
import {getToken} from "@/utils/auth";
data() {return {// 导入参数upload: {// 是否显示弹出层(导入弹窗)openImport: false,// 弹出层标题(自己填写)titleImport: "",// 是否禁用上传isUploading: false,// 是否更新已经存在的用户数据updateSupport: 0,// 设置上传的请求头部headers: {Authorization: "Bearer " + getToken()},// 上传的地址,即后端接口地址url: process.env.VUE_APP_BASE_API + "/xxx/xxx/importData"},};},
 /**导入excel表*/handleImport() {this.upload.titleImport = "导入xxxx表";this.upload.openImport = true;},/** 下载模板操作 */importTemplate() {this.download('xxx/xxx/export', {...this.queryParams}, `xxxx模板${new Date().getTime()}.xlsx`)},// 文件上传中处理handleFileUploadProgress(event, file, fileList) {this.upload.isUploading = true;},// 文件上传成功处理handleFileSuccess(response, file, fileList) {this.upload.open = false;this.upload.isUploading = false;this.$refs.upload.clearFiles();this.$alert(response.msg, "导入结果", {dangerouslyUseHTMLString: true});this.getList();},// 提交上传文件submitFileForm() {this.$refs.upload.submit();}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com