javascript - 使用 Angular 13 使用 js-xlsx 读取本地 Excel 文件?

标签 javascript excel angular file js-xlsx

我的 Angular-CLI 前端开发服务器在本地运行 localhost:4200

I need to get a local Excel file stored on my PC, read its content and make some calls to an API from the client side.


我正在尝试使用 js-xlsx,安装了 npm install xlsx但我找不到如何获取文件并读取其内容。
如何在 Angular 13 中使用 js-xlsx 导入本地 excel 文件?

最佳答案

这是工作Example

onFileChange(ev) {
    let workBook = null;
    let jsonData = null;
    const reader = new FileReader();
    const file = ev.target.files[0];
    reader.onload = (event) => {
      const data = reader.result;
      workBook = XLSX.read(data, { type: 'binary' });
      jsonData = workBook.SheetNames.reduce((initial, name) => {
        const sheet = workBook.Sheets[name];
        initial[name] = XLSX.utils.sheet_to_json(sheet);
        return initial;
      }, {});
      const dataString = JSON.stringify(jsonData);
      document.getElementById('output').innerHTML = dataString.slice(0, 300).concat("...");
      this.setDownload(dataString);
    }
    reader.readAsBinaryString(file);
  }

关于javascript - 使用 Angular 13 使用 js-xlsx 读取本地 Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57818105/

相关文章:

javascript - 如何使用react-i18next的<Trans/>进行替代图像翻译?

Javascript 属性说未定义

vba - Excel VBA : How to create an autofilter with an exception to the filter?

c# - 使用 openxml 删除 x 命名空间

javascript - 从 Firebase 存储获取的 URL 未显示在 Ionic 3 中的 <ion-img [src]=...> 上

javascript - npm jshint 安装以及 Grunt 和 Nodejs 的工作

javascript - JQuery id 测试失败,带有单独 div 的动画系统

excel - Power Query - 将所有数字替换为 null

结构指令中的 Angular 输入绑定(bind)

css - 使用 dialogflow 的 ionic 3 聊天 UI