Vue 导入excel功能

页面:

1 <input type="file" @change="importf(this)" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/>

 

js代码:

 1     importf(obj) {
 2       let _this = this;  
 3       this.file = event.currentTarget.files[0];  
 4       var rABS = false; //是否将文档读取为二进制字符串  
 5       var file = this.file; 
 6 
 7       FileReader.prototype.readAsBinaryString = function(f) {  
 8         var binary = "";  
 9         var rABS = false; //是否将文档读取为二进制字符串  
10         var pt = this;  
11         var workbook; //读取完成的数据  
12         // var excelData;  
13         var reader = new FileReader(); 
14         reader.onprogress = function(e) { 
15           let total = file.size;
16           _this.progress = (e.loaded/total)*100;
17           console.log( _this.progress);
18         };   
19         reader.onload = function(e) {
20           try {
21             var bytes = new Uint8Array(reader.result);  
22             var length = bytes.byteLength;  
23             for(var i = 0; i < length; i++) {  
24               binary += String.fromCharCode(bytes[i]);  
25             }  
26             if(rABS) {  
27               workbook = XLSX.read(btoa(fixdata(binary)), { //手动转化  
28                 type: 'base64'  
29               });  
30             }else {  
31               workbook = XLSX.read(binary, {  
32                 type: 'binary'  
33               });  
34             } 
35             // excelData = []; 
36           } catch(e) {
37             console.log('文档类型不正确');
38             return;
39           }
40           var fromTo = '';
41           for (var sheet in workbook.Sheets) {
42             if (workbook.Sheets.hasOwnProperty(sheet)) {
43               fromTo = workbook.Sheets[sheet]['!ref'];
44               _this.excelData = _this.excelData.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
45             }
46           }
47           console.log(_this.excelData);
48         };
49 
50         reader.readAsArrayBuffer(f);  
51 
52       } 
53  
54       var reader = new FileReader();
55       if(rABS) {  
56         reader.readAsArrayBuffer(file);  
57       }else {  
58         reader.readAsBinaryString(file);  
59       } 
60 
61       
62     },

 参考网址:

https://www.jianshu.com/p/74d405940305

关键词:var reader file workbook sheet binary exceldata excel rabs sheets

相关推荐:

js读取excel文档,绘制echarts图形---数据处理

vue导出excel数据表格功能

React读取Excel——js-xlsx 插件的使用

How do I import from Excel to a DataSet using Microsoft&period;Office&period;Interop&period...

如何使用JavaScript实现纯前端读取和导出excel文档

vue中使用excel导入导出

csv和excel读取和下载

上传excel文档并读取数据

【第三周读书笔记】浅谈node.js中的异步回调和用js-xlsx操作Excel表格

上传excel文档并绘制成图表