前言:做項(xiàng)目中俱饿,一需求為上傳excel文件,上傳前判斷文件中的表頭字段為固定字段不能變塌忽,固需要解析excel文件拍埠。項(xiàng)目采用的是angular開(kāi)發(fā),經(jīng)查閱資料可使用xlsx插件解決土居。
在使用xlsx插件前枣购,就遇到下載xlsx插件報(bào)錯(cuò)問(wèn)題嬉探,查閱資料,總結(jié)方法如下棉圈。
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我們能想到權(quán)限問(wèn)題涩堤,以管理員身份運(yùn)行cmd(針對(duì)本次無(wú)用,大家可以下去試試有用沒(méi))
2.npm版本問(wèn)題分瘾,npm最新版本在安裝包時(shí)有些缺少依賴包胎围,這種情況就需要我們安裝低一點(diǎn)的npm版本,命令npm i -g npm@6.3.0(可用)
現(xiàn)在進(jìn)入正題德召,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 為上傳文件之前的鉤子,參數(shù)為上傳的文件上岗,若返回 false 則停止上傳福荸。
import * as XLSX from 'xlsx';
beforeUpload = (file): boolean => {
if (file) {
const fileName = file.name;//獲取文件名
const reader: FileReader = new FileReader();//FileReader 對(duì)象允許Web應(yīng)用程序異步讀取存儲(chǔ)在用戶計(jì)算機(jī)上的文件
//當(dāng)讀取操作成功完成時(shí)調(diào)用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 });//解析出文件數(shù)據(jù),可以進(jìn)行后面操作
this.importUserHeader = this.importUserList[0];//獲得表頭字段
this.fileList = this.fileList.concat(file);
};
reader.readAsBinaryString( file );
return false;
}
}