HTML 代码
<a-button @click="saveData('submit')" v-if="formStatus === '已保存'">提交</a-button>
<input type="file" @change="importExcel($event)" v-show="false" ref="file" />
<a-button v-if="formStatus === '已保存'" @click="handlickImport">导入</a-button>
<a-button v-if="id" @click="handleClickExport">导出</a-button>
<a-button v-if="formStatus === '已保存'" @click="handlickDownloadTemplate"
>下载模板</a-button
>
JS 代码
handlickImport() {
(this.$refs.file as any).click();
}
importExcel(event: any) {
const file = event.target.files[0];
let params = new FormData();
params.append("file", file);
const schemaCode: any = this.schemaCode;
params.append("schemaCode", schemaCode);
this.import(params).then((res: any) => {
if (res.errcode === 0) {
let externalClaimAccountChild = res.data.externalClaimAccountChild;
externalClaimAccountChild.map((item: any) => {
for (let key in item) {
for (let key in item) {
if (key.includes("time") || key.includes("Time")) {
item[key] = item[key] ? moment(item[key]) : null;
} else if (key.includes("Cost") || key.includes("cost")) {
item[key] = item[key] ? Number(Number(item[key]).toFixed(2)) : null;
}
}
}
});
this.externalChangeAccountChild = externalClaimAccountChild;
this.$message.success("导入成功", 1, () => {
event.target.value = "";
});
} else {
this.$message.success("导入失败", 1, () => {
event.target.value = "";
});
}
});
}
handlickDownloadTemplate() {
let params = new FormData();
const schemaCode: any = this.schemaCode;
params.append("schemaCode", schemaCode);
this.downLoad(params).then((res: any) => {
if (res && res.errcode && res.errcode !== 0) {
this.$message.error("下载模板失败");
} else if (res) {
const blob = new Blob([res], {
type: "application/vnd.ms-excel;charset=UTF-8"
});
const date = new Date();
const mounth = date.getMonth() > 8 ? date.getMonth() + 1 : `0${date.getMonth() + 1}`;
const days = date.getDate() > 9 ? date.getDate() : `0${date.getDate()}`;
const hours = date.getHours() > 9 ? date.getHours() : `0${date.getHours()}`;
const minutes = date.getMinutes() > 9 ? date.getMinutes() : `0${date.getMinutes()}`;
const seconds = date.getSeconds() > 9 ? date.getSeconds() : `0${date.getSeconds()}`;
const stamp = `${date.getFullYear()}${mounth}${days}${hours}${minutes}${seconds}`;
const fileName = `对外索赔台账模板下载${stamp}.xlsx`;
const a = document.createElement("a");
const url = URL.createObjectURL(blob);
a.download = fileName;
a.href = url;
a.click();
URL.revokeObjectURL(url);
this.$message.destroy();
}
});
}
handleClickExport() {
this.export({
objectId: this.id,
schemaCode: this.schemaCode
}).then((res: any) => {
if (res && res.errcode && res.errcode !== 0) {
this.$message.error("导出失败");
} else if (res) {
const blob = new Blob([res], {
type: "application/vnd.ms-excel;charset=UTF-8"
});
const date = new Date();
const mounth = date.getMonth() > 8 ? date.getMonth() + 1 : `0${date.getMonth() + 1}`;
const days = date.getDate() > 9 ? date.getDate() : `0${date.getDate()}`;
const hours = date.getHours() > 9 ? date.getHours() : `0${date.getHours()}`;
const minutes = date.getMinutes() > 9 ? date.getMinutes() : `0${date.getMinutes()}`;
const seconds = date.getSeconds() > 9 ? date.getSeconds() : `0${date.getSeconds()}`;
const stamp = `${date.getFullYear()}${mounth}${days}${hours}${minutes}${seconds}`;
const fileName = `对外索赔台账导出${stamp}.xlsx`;
const a = document.createElement("a");
const url = URL.createObjectURL(blob);
a.download = fileName;
a.href = url;
a.click();
URL.revokeObjectURL(url);
this.$message.destroy();
}
});
}
接口
downLoad(params): Promise<Common.Data> {
const url = "/api/swat/StandingBook/downLoad";
return axios.post(url, params, {
responseType: "arraybuffer",
headers: { "Content-Type": "application/octet-stream" }
});
}
export(params): Promise<Common.Data> {
const url = "/api/swat/StandingBook/mstandingBookExport";
return axios.post(url, params, { responseType: "arraybuffer" });
}
import(params: any): Promise<Common.Data> {
let url: any = "/api/sawt/StandingBook/mstandingBookImport";
return axios.post(url, params, {
headers: { "Content-Type": "multipart/form-data" }
});
}
表格导入导出(字节流)
最新推荐文章于 2024-10-18 16:05:34 发布