简体   繁体   English

如何使用javascript / jquery从Excel电子表格中获取数据?

[英]How to get data from Excel spreadsheets using javascript/jquery?

I'm trying to find a way to extract data from an Excel spreadsheet using any methods with javascript or jquery to place onto my html page. 我试图找到一种方法,可以使用任何将JavaScript或jquery放置到我的html页面上的方法,从Excel电子表格中提取数据。 I've googled various things finding that certain methods only work on IE and others are out-dated, but I can't find any definitive examples that I can use to apply to the simple "find cell based on choices in from row and column." 我在各种事物上进行了谷歌搜索,发现某些方法仅适用于IE,而另一些则已过时,但是我找不到任何可用于基于行和列中的选择而找到的简单“查找单元格”的确定示例。 ”。 If someone could point me in the right direction I would be most appreciative. 如果有人能指出我正确的方向,我将非常感激。 I need this to work cross-browser as well. 我也需要这个才能跨浏览器工作。

The following function will convert an xlsx file to JSON. 以下函数会将xlsx文件转换为JSON。 Once it's in JSON, it is trivial to manipulate. 一旦使用JSON,就可以轻松进行操作。

<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script>
<script>
var ExcelToJSON = function() {

  this.parseExcel = function(file) {
    var reader = new FileReader();

    reader.onload = function(e) {
      var data = e.target.result;
      var workbook = XLSX.read(data, {
        type: 'binary'
      });

      workbook.SheetNames.forEach(function(sheetName) {
        // Here is your object
        var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
        var json_object = JSON.stringify(XL_row_object);
        console.log(json_object);

      })

    };

    reader.onerror = function(ex) {
      console.log(ex);
    };

    reader.readAsBinaryString(file);
  };
};
</script>

For very large excel files that might lock up or crash the browser look into HTML5 Webworkers 对于可能会锁定或导致浏览器崩溃的超大型excel文件,请查看HTML5 Webworkers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM