0

我正在尝试使用 XLSX 库从 excelsheet 读取数据,但我得到了这个:-

错误:_fs 在 xlsx.js 中未定义(第 11388 行,第 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>

我尝试在网上搜索合适的答案,但找不到任何答案。请建议。

4

1 回答 1

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>
于 2016-08-11T16:05:16.713 回答