0

I am trying to use the XLSX library to read data from excelsheet but I am getting this:-

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

Here is my code :-

<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>

I tried searching the net for a suitable answer but could not find any. Please suggest.

1 Answers1

0

It was not working because the file wasn't loaded completely and it started processing it.

Here is the code that is working absolutely fine :

<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>