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

时间:2022-12-07 22:17:08

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. 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." If someone could point me in the right direction I would be most appreciative. I need this to work cross-browser as well.

我正在尝试使用javascript或jquery的任何方法从Excel电子表格中提取数据以放置到我的html页面。我搜索了各种各样的东西,发现某些方法仅适用于IE而其他方法已经过时,但我找不到任何明确的例子,我可以用来应用简单的“基于行和列中的选择查找单元格” “。如果有人能指出我正确的方向,我会非常感激。我也需要这个以跨浏览器工作。

1 个解决方案

#1


0  

The following function will convert an xlsx file to JSON. Once it's in JSON, it is trivial to manipulate.

以下函数将xlsx文件转换为JSON。一旦它在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

对于可能锁定或崩溃浏览器的非常大的ex​​cel文件,请查看HTML5 Webworkers

#1


0  

The following function will convert an xlsx file to JSON. Once it's in JSON, it is trivial to manipulate.

以下函数将xlsx文件转换为JSON。一旦它在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

对于可能锁定或崩溃浏览器的非常大的ex​​cel文件,请查看HTML5 Webworkers