2016-08-10 91 views
0

我試圖使用XLSX庫來讀取excelsheet數據,但我得到這個: -當使用XLSX解析excelsheet - 它是扔_fs是不確定的

ERROR: _fs is undefined at xlsx.js (line 11388, col 59)

這裏是我的代碼: -

<html> 
    <head> 
    <title>Read Excel</title> 
    <meta meta http-equiv="Content-Type" content="text/html;" charset="UTF-8"/> 
    </head> 
    <body> 
    <script src="xlsx.js"></script> 
    <script> 
    function actionbegins(){ 
     console.log("Inside the function action begins !!"); 
     if(typeof XLSX === 'undefined' && typeof require !== 'undefined') 
      XLSX = require('xlsx'); 
     var workbook = XLSX.readFile("Invoice.xlsx", {type: 'base64'}); 
     var first_sheet_name = workbook.SheetNames[0]; 
     var address_of_cell = 'A2'; 
     var worksheet = workbook.Sheets[first_sheet_name]; 
     var desired_cell = worksheet[address_of_cell]; 
     var desired_value = desired_cell.v; 
     console.log("we got the value as --> "+desired_value); 
    } 
    </script> 
    <button id="btnDoIt" onclick="actionbegins()" name="btnDoIt" class="btn btn-primary">do It !!</button> 
    </body> 
</html> 

我試圖尋找一個合適的答案,但找不到任何。請建議。

+0

你正確安裝'xlsx' - 包?它似乎無法找到所需的依賴關係。 –

+0

我簡單地從github下載了這個庫,並在我的HTML中使用它。任何我缺少的東西? –

+0

您是否按照github上的建議安裝了'bower'或'npm'? –

回答

0

它不工作,因爲該文件沒有完全加載,它開始處理它。

這裏是工作的精絕代碼:

<html> 
    <head> 
    <title>Read Excel</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <script lang="javascript" src="dist/xlsx.core.min.js"></script> 
    </head> 
    <body> 
    <script> 
    function letsdoit(){ 
      var url = "Invoice.xlsx"; 
      var oReq = new XMLHttpRequest(); 
      oReq.open("GET", url, 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(""); 
      var workbook = XLSX.read(bstr, {type:"binary"}); 

      var first_sheet_name = workbook.SheetNames[0]; 
      var address_of_cell = 'A1'; 
      var worksheet = workbook.Sheets[first_sheet_name]; 
      var desired_cell = worksheet[address_of_cell]; 

      var desired_value = desired_cell.v; 
      alert("value is -- "+desired_value); 
     } 

     oReq.send(); 
    } 
    </script> 
    <input type="file" name="file" id="selectfile" onchange="letsdoit()" /> 
    </body> 
</html> 
相關問題