//数据导出 生成excel链接
const handleExport = async () => {
// getTankExportApi().then((res: any) => {
// const blob = new Blob([res.data], {
// type: "application/vnd.ms-excel;charset=utf-8",
// });
// const blobUrl = window.URL.createObjectURL(blob);
// const a = document.createElement("a");
// a.href = blobUrl;
// a.setAttribute("download", window.decodeURI(res.headers.filename));
// a.click();
// });
const exportURL =
"http://10.52.40.23:8080/apps/suppipe-lngtmrws/lngtmrws/storageTank/tagExport";
const res = await fetch(exportURL, {
method: "GET",
});
// 处理响应,这里假设服务器返回的是Excel文件
const blob = await res.blob();
// 创建一个链接元素用于下载
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "罐容表模板.xlsx"; // 指定下载的文件名
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
// 清理URL对象
window.URL.revokeObjectURL(url);
};
//数据导入
const handleFileChange = (val: any) => {
// console.log(selectRowData, "asd");
if (Object.keys(selectRowData).length === 0) {
message.warning("请选择一个储罐");
return;
}
setSpinning(true); //加载状态
const formData = new FormData();
formData.append("file", val.file);
service
.post(
`/lngtmrws/storageTank/importData?storageTankId=${selectRowData.id}`,
formData,
{
// headers: {
// "Content-Type": "x-www-form-urlencoded",
// },
}
)
.then((res) => {
if (res.data.code === 0) {
message.success("上传成功");
fetchTankVolumn(); //导入成功之后更新一下罐容表
} else {
message.error(res.data.msg);
}
})
.catch((error) => {
setSpinning(false);
message.error("上传失败");
})
.finally(() => {
setSpinning(false);
});
};
const beforeUpload = () => {
return false;
};
<div className="btns">
<Button
type="primary"
style={{ marginRight: "20px" }}
onClick={() => handleAddOrEdit("add")}
>
新增
</Button>
<Button
type="primary"
onClick={handleExport}
style={{ marginRight: "20px" }}
>
导出模板
</Button>
<Upload
showUploadList={false}
onChange={handleFileChange}
beforeUpload={beforeUpload}
>
<Button
type="primary"
disabled={selectedRowKeys.length == 0 ? true : false}
icon={<UploadOutlined />}
title="请至少勾选一个储罐"
>
上传罐容表
</Button>
</Upload>
</div>