2017-08-08 52 views
1

我想分析XLSX文件,並將其轉換成JSON在角2 我使用下面的代碼閱讀和分析此文件 READFILE(){Excel以JSON轉換角2

var testUrl= "../assets/US175939.xlsx"; 
var oReq = new XMLHttpRequest(); 
oReq.open("GET", testUrl, true); 
oReq.responseType = "arraybuffer"; 

oReq.onload = function(e) { 
    var arraybuffer = oReq.response; 

    /* convert data to binary string */ 
    var data = new Uint8Array(arraybuffer); 
    var arr = new Array(); 
    for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]); 
    var bstr = arr.join(""); 
    console.log("Data"+bstr); 

    /* Call XLS */ 
    var workbook = XLSX.read(bstr, {type:"binary"}); 


    /* DO SOMETHING WITH workbook HERE */ 
} 

oReq.send(); 
} 

錯誤是 - 「找不到名稱XLSX」。

請建議我一些解決方案,在角2

+2

可能的複製[如何導出JSON到CSV或Excel - 角2](https://stackoverflow.com/questions/39177183/how-to-export-json-to-csv-or-excel-angular-2) – theFunkyEngineer

+0

我想將XLSX導出爲JSON。您已經參考的數據是將JSON導出到EXCEL。他們是不同的 – Akanksha

回答

0

的Excel轉換成JSON我已經找到了解決方案。希望這將幫助所有:

//讀取文件 READFILE(){

var testUrl= "../assets/US175939.xlsx"; 
var oReq = new XMLHttpRequest(); 
oReq.open("GET", testUrl, true); 
oReq.responseType = "arraybuffer"; 

oReq.onload = function(e) { 
    var arraybuffer = oReq.response; 

    /* convert data to binary string */ 
    var data = new Uint8Array(arraybuffer); 
    var arr = new Array(); 
    for(var i = 0; i != data.length; ++i){ arr[i] = String.fromCharCode(data[i]); 
    // console.log("Data"+data[i]); 
    } 
    var bstr = arr.join(""); 
    var workbook = XLSX.read(bstr, {type:"binary"}); 
//console.log("Data"+bstr); 
var first_sheet_name = workbook.SheetNames[0]; 
    /* Get worksheet */ 
    var worksheet = workbook.Sheets[first_sheet_name]; 
    var json = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], {header:1, raw:true}); 
    var jsonOut = JSON.stringify(json); 
    console.log("test"+jsonOut); 
} 

oReq.send(); 

}