如何从输入字段获取excel文件数据并需要转换为角度为2的JSON数据?

时间:2017-02-09 08:54:51

标签: angular typescript

我需要将excel文件数据上传到一个表,但我也没有获取文件数据。

这是我的代码

**Here is the form**
<form role="form" #uploadFileForm="ngForm" id="uploadFileForm"> 
  <div class="card card-block">                            
    <div class="form-group" style="padding: 10px;">
      <label for="fileupload">Upload File</label>                  
      <input type="file" class="form-control" (change)="fileUploadChange()" required>
    </div>                           
   </div>
 <button type="button" class="btn btn-primary"  (click)="saveUploadfile()">Submit</button>
 <button type="button" class="btn btn-secondary" (click)="cancel()">Cancel</button>
</form> 

这是component.ts

private fileUploadChange(event: any){
    var file = event.dataTransfer ? event.dataTransfer.files[0] : event.target.files[0];
        var pattern = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
        console.log('choosen file type is ' + file); 
        console.dir("dir is " + file);       
        if (!file.type.match(pattern)) {  
             this.showToast('ERROR', 'Error', 'Invalid file format');
            return;
        }        
         let wb: XLSX.IWorkBook = XLSX.readFile(file, {type: 'binary'});
         console.log(wb);
         let sheetName = wb.SheetNames[0];
         console.log("Selected file sheet Name "+ sheetName);   
}

0 个答案:

没有答案