const getExcelFile = (file) => {
  console.log("file", file);
  //新建一个fileReader
  let reader = new FileReader();
  //执行读文件的函数,设置编码格式
//Excel,需要将文件转为blob或文件流
  reader.readAsArrayBuffer(file, "UTF-8");
  //读取文件中的内容
  reader.onload = function (e) {
    const content = e.target.result;
    console.log("content", content);
    downloadContentFile(file.name, content);
  };
};

在这里插入图片描述

const downloadExcelFile = (filename, text) => {
  let blob = new Blob([text], { type: "application/vnd.ms-excel" });
  const element = document.createElement("a");
 const href = URL.createObjectURL(blob);
  element.href = href;
  element.setAttribute("download", filename);
  element.style.display = "none";
  element.click();
  //调用这个方法来让浏览器知道不用在内存中继续保留对这个文件的引用了。
  URL.revokeObjectURL(href);

  element.remove();
};
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐