这个问题解决的关键在于前端获取数据,
首先,先复制表单
1,按钮
<el-button
type="info"
icon="el-icon-upload2"
size="mini"
@click="handleImport"
>导入</el-button>
导入的对话框,通用的
<!-- 用户导入对话框 -->
<el-dialog :title="upload.title" :visible.sync="upload.open" 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";
前端方法相应的参数
upload: {
// 是否显示弹出层(用户导入)
open: false,
// 弹出层标题(用户导入)
title: "",
// 是否禁用上传
isUploading: false,
// 是否更新已经存在的用户数据
updateSupport: 0,
// 设置上传的请求头部
headers: { Authorization: "Bearer " + getToken() },
// 上传的地址
url: process.env.VUE_APP_BASE_API + "/system/article/importData" // 这里要改你自己访问的URL
},
下面则是js响应的操作
/** 导入按钮操作 */
handleImport() {
this.upload.title = "用户导入";
this.upload.open = true;
},
/** 下载模板操作 */
importTemplate() {
importTemplate().then(response => {
this.download(response.msg);
});
},
// 文件上传中处理
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();
}
这样就可以访问到后台接口了
接下来是设置实体类,确保能被识别到
/** 主键 */
private Long id;
/** 标题 */
@Excel(name = "标题")
private String titles;
/** 描述 */
@Excel(name = "描述")
private String describes;
/** 作者 */
@Excel(name = "作者")
private String author;
/** 来源 */
@Excel(name = "来源")
private String source;
接下来就是查看导入是否有读到
@Log(title = "导入文章", businessType = BusinessType.IMPORT)
@PostMapping("/importData")
public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
{
ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
List<Article> ArticleList = util.importExcel(file.getInputStream());
// LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
System.out.println(ArticleList);
// 写入数据
articleService.insertArticleBach(ArticleList);
return AjaxResult.success(ArticleList);
}
读到了,并且封装成对象后,其实可以直接插入操作了。
想了想,很多时候遇到问题,不要一股脑的想看小说一样看其他的文档,慢慢,静下心来看,也许能收获很多。