JS 实现下载Blod文件

实现代码:

//下载Blod文件
const downLoadBlobFile = (filename, res) => {
  if (!res) return;
  let a = document.createElement('a');
  let blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' });
  let blobUrl = window.URL.createObjectURL(blob);
  a.style.display = 'none';
  a.download = filename;
  a.href = blobUrl;
  document.body.appendChild(a);

  // 兼容触发clicka
  var evt = document.createEvent("MouseEvents");
  evt.initEvent("click", true, true);
  a.dispatchEvent(evt);

  document.body.removeChild(a);
  window.URL.revokeObjectURL(blobUrl);
}

调用:

  // 导出表格
  const customerUersExport = (payload) => {
    
    if (companyCode) payload.companyCode = companyCode;
      if (reportRanges.length != 0){
        payload.departmentIds = JSON.stringify(reportRanges.map(item => JSON.parse(item.key)))
      }
    dispatch({
      type: "customerModel/getCustomerUersExport",
      payload: payload
    }).then(response => {
      console.log('-------------', response)
      downLoadBlobFile('成员客户统计列表.xls', response);
    })
  }

 

你可能感兴趣的:(Web前端,react,JS,下载Blod,Blod,前端下载表格,下载,JS下载xls)