前端导入、导出Excel文件流处理

公共文件:util.js 

//导出
export function excelDownload(type, name, data) {
  const link = document.createElement("a");
  const blob = new Blob([data]);
  link.style.display = "none";
  link.href = URL.createObjectURL(blob);
  link.setAttribute("download", `${name}.` + type);
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

下载接口文件:extensionManagement.js

//下载模板
export const downloadTemplate = () =>
    request({
        url: `${baseUrl}/control/templateExport`,
        method: "get",
        responseType: 'blob',
    });

//批量导入
export const machineImport = (data) =>
    request({
        url: `${baseUrl}/control/uploadExcel`,
        method: "post",
        data: data,
        headers: {
            'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundarynl6gT1BKdPWIejNq'
        }
    });

下载方法(vue3语法)

//模板下载
function templateExport(){
    Modal.confirm({
        title: '确定要下载模板?',
        icon: createVNode(ExclamationCircleOutlined),
        onOk() {
            downloadTemplate().then(res=>{
                excelDownload("xls", "分机管理模板", res.data); 
            })
        },
        onCancel() {
            //  console.log('Cancel');
        },
    });
}

导入Excel方法
 

// 批量导入
function beforeUpload (file) {
    const formData = new FormData()
    formData.append('uploadExcel', file);
    machineImport(formData).then((res) => {
        if (res.data.code === 0) {
            message.success("导入成功")
            onLoad()
        } else {
            message.error(res.data.msg)
        }
    })
};

你可能感兴趣的:(前端,javascript,开发语言)