angular-xlsx 解析excel文件

前言:做项目中,一需求为上传excel文件,上传前判断文件中的表头字段为固定字段不能变,固需要解析excel文件。项目采用的是angular开发,经查阅资料可使用xlsx插件解决。

在使用xlsx插件前,就遇到下载xlsx插件报错问题,查阅资料,总结方法如下。

npm install xlsx --save

npm ERR! path E:\project\myGo\src\git.changhong.com\matrix\matrix-admin\node_modules\.node-sass.DELETE\vendor\win32-x64-57\binding.node
npm ERR! code EPERM
npm ERR! errno -4048
npm ERR! syscall unlink
npm ERR! Error: EPERM: operation not permitted, unlink 'E:\project\myGo\src\git.changhong.com\matrix\matrix-admin\node_modules\.node-sass.DELETE\vendor\win32-x64-57\binding.node'
npm ERR!  { Error: EPERM: operation not permitted, unlink 'E:\project\myGo\src\git.changhong.com\matrix\matrix-admin\node_modules\.node-sass.DELETE\vendor\win32-x64-57\binding.node'
npm ERR!   cause:
npm ERR!    { Error: EPERM: operation not permitted, unlink 'E:\project\myGo\src\git.changhong.com\matrix\matrix-admin\node_modules\.node-sass.DELETE\vendor\win32-x64-57\binding.node'
npm ERR!      errno: -4048,
npm ERR!      code: 'EPERM',
npm ERR!      syscall: 'unlink',
npm ERR!      path: 'E:\\project\\myGo\\src\\git.changhong.com\\matrix\\matrix-admin\\node_modules\\.node-sass.DELETE\\vendor\\win32-x64-57\\binding.node' },
npm ERR!   stack: 'Error: EPERM: operation not permitted, unlink \'E:\\project\\myGo\\src\\git.changhong.com\\matrix\\matrix-admin\\node_modules\\.node-sass.DELETE\\vendor\\win32-x64-57\\binding.node\'',
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'unlink',
npm ERR!   path: 'E:\\project\\myGo\\src\\git.changhong.com\\matrix\\matrix-admin\\node_modules\\.node-sass.DELETE\\vendor\\win32-x64-57\\binding.node',
npm ERR!   parent: 'matrix-admin' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It's possible that the file was already in use (by a text editor or antivirus),
npm ERR! or that you lack permissions to access it.
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\asus\AppData\Roaming\npm-cache\_logs\2019-06-27T08_48_19_621Z-debug.log

解决办法:
1.看到operation not permitted我们能想到权限问题,以管理员身份运行cmd(针对本次无用,大家可以下去试试有用没)
2.npm版本问题,npm最新版本在安装包时有些缺少依赖包,这种情况就需要我们安装低一点的npm版本,命令npm i -g npm@6.3.0(可用)

现在进入正题,angular使用xlsx解析excel文件:
上传组件我使用的是ng.ant的upload

    <nz-upload [(nzFileList)]="fileList" [nzLimit]='1' [nzMultiple]="true" [nzBeforeUpload]="beforeUpload">
      <button nz-button>
        <i nz-icon nzType="upload"></i>
        <span>选择文件</span>
      </button>
    </nz-upload>

ts文件中引入import * as XLSX from 'xlsx';
beforeUpload 为上传文件之前的钩子,参数为上传的文件,若返回 false 则停止上传。

import * as XLSX from 'xlsx';
beforeUpload = (file): boolean => {
    if (file) {
      const fileName = file.name;//获取文件名
      const reader: FileReader = new FileReader();//FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件
      //当读取操作成功完成时调用FileReader.onload 
      reader.onload = (e: any) => {
        const bstr: string = e.target.result;
        const wb: XLSX.WorkBook = XLSX.read(bstr, { type: 'binary' });      
        const wsname: string = wb.SheetNames[0];
        const ws: XLSX.WorkSheet = wb.Sheets[wsname];   
        this.importUserList = XLSX.utils.sheet_to_json(ws, { header: 1 });//解析出文件数据,可以进行后面操作
        this.importUserHeader =  this.importUserList[0];//获得表头字段
        this.fileList = this.fileList.concat(file);
      };
      reader.readAsBinaryString( file );
      return false;
    }
  }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 13,945评论 0 15
  • JavaScript 模块化编程 网站越来越复杂,js代码、js文件也越来越多,会遇到什么问题? 命名冲突; 文件...
    magic_pill阅读 1,476评论 0 1
  • 总觉得课题这个词对于我来说是特别高大上的感觉,虽然自己也是一线教师,但是从来没有想过自己有一天要去做一个课题,总...
    Aliceaurora阅读 449评论 0 0
  • 在互联网圈中,我们会经常聊到一个词,那就是“体验经济”,在互联的下半场,体验经济已经充斥着各个赛道,成为了商家必须...
    语忆科技阅读 576评论 0 0
  • 我是一个爱较劲,爱搞笑的人.... 十年前我进入了宠物圈.... 我还记得“你做不了宠物行业,别踏进这个深坑......
    新宠秀阅读 388评论 0 0