前端
<el-upload ref="performanceUpload" accept=".xlsx, .xls" :headers="performanceUpload.headers"
:action="performanceUpload.url + '?updateSupport=' + performanceUpload.updateSupport"
:disabled="performanceUpload.isUploading" :on-progress="handleFileUploadProgressPerformance"
:on-success="handleFileSuccessPerformance" :auto-upload="false" :multiple="true" 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="performanceUpload.updateSupport"/>
是否更新已经存在的用户数据
</div>
<span>仅允许导入xls、xlsx格式文件。</span>
</div>
</el-upload>
js
<el-upload
ref="upload"
accept=".docx, .doc"
:headers="upload.headers"
:action="upload.url"
:http-request="HttpUploadFile"
:disabled="upload.isUploading"
:data="getfileData()"
:on-change="handleChange"
:on-remove="handleRemove"
:on-progress="handleFileUploadProgress"
:on-success="handleFileSuccess"
:auto-upload="false"
:multiple="true" drag>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip text-center" slot="tip">
<span>仅允许导入.docx, .doc格式文件。</span>
</div>
</el-upload>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitFileForm">确 定</el-button>
<el-button @click="handleCancel">取 消</el-button>
</div>
upload: {
open: false,
title: '',
isUploading: false,
updateSupport: 0,
headers: { Authorization: 'Bearer ' + getToken() },
url: process.env.VUE_APP_BASE_API + '/duty/report/importData',
fileList: []
},
submitFileForm() {
if (this.upload.fileList.length === 0) {
return this.$message.warning('请选取文件后再上传')
}
if (this.upload.fileList.length > 0) {
this.fileData = new FormData()
this.$refs.upload.submit();
this.fileData.append("data", JSON.stringify(this.form));
this.fileData.append("headers", { Authorization: "Bearer " + getToken() });
this.fileData.append("withCredentials",false)
this.fileData.append("filename", "file");
var i = this.upload.fileList.length
if (i !== 0) {
exportFiles(this.fileData).then((res) =>{
if(res.code === 200) {
this.upload.isUploading = false;
this.upload.fileList = [];
this.upload.open = false;
this.$refs.upload.clearFiles();
this.$modal.msgSuccess(res.msg);
this.getReportDutyList(this.selectVal);
this.getPersonnelInfo(this.selectVal);
} else {
this.$message({
message: "失败",
type: 'error'
})
}
})
} else {
}
}
},
后端 controller
@PostMapping("/importDataFiles")
public AjaxResult importDataFiles(MultipartFile[] files) throws Exception {
if (CollectionUtils.isEmpty(Arrays.asList(files))) {
return AjaxResult.error("上传文件为空!");
} else {
for (MultipartFile file : files) {
InputStream inputStream = file.getInputStream();
List<WordTableVO> wordTableVOS = new ArrayList<>();
if (WordUtils.isDocOrDocx(file.getOriginalFilename())) {
wordTableVOS = WordUtils.getTableFromDoc(inputStream);
} else {
wordTableVOS = WordUtils.getTableFromDocx(inputStream);
}
ReportDutyExportEntity result = WordUtils.processWordData(wordTableVOS);
String userName = getUsername();
int insertResult = scoreService.insertScore(result, userName);
if (insertResult == 0) {
return AjaxResult.error("导入失败!");
}
}
return AjaxResult.success("导入成功!");
}
}