Skip to content

纯 JS 写的 Excel 解析库 SheetJS js-xlsx #4

@greycode

Description

@greycode

先上模板代码

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <title>Hello, Excel!</title>
</head>

<body>
  <h1>Hello, Excel!</h1>
  <div class="row">
    <div class="col-sm-12">
      <input type="file" id="file" />
      <label for="file">... or click here to select a file</label>

    </div>

  </div>
  <!-- https://sheetjs.com/sexql/index.html -->
  <!-- https://docs.sheetjs.com/#parsing-workbooks -->
  <!-- https://github.com/atlassian/jquery-handsontable -->

  <!-- Optional JavaScript -->
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
    crossorigin="anonymous"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.0/xlsx.full.min.js"></script>
  <script>
    var rABS = true; // true: readAsBinaryString ; false: readAsArrayBuffer 
    function handleFile(e) {

      var files = e.target.files,
        f = files[0];
      var reader = new FileReader();
      reader.onload = function (e) {
        var data = e.target.result;
        if (!rABS) data = new Uint8Array(data);
        var workbook = XLSX.read(data, {
          type: rABS ? 'binary' : 'array'
        });
        console.log(workbook)
        /* DO SOMETHING WITH workbook HERE */
      };
      if (rABS) reader.readAsBinaryString(f);
      else reader.readAsArrayBuffer(f);

    }
    var input_file = document.querySelector('#file');
    input_file.addEventListener('change', handleFile, false);

  </script>
</body>

</html>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions