如何在Javascript/HTML5中解析Excel(XLS)文件 [英] How to parse Excel (XLS) file in Javascript/HTML5

查看:217
本文介绍了如何在Javascript/HTML5中解析Excel(XLS)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过FileReader读取Excel文件,但是它可以输出文本以及奇怪的字符.我需要逐行读取xls文件,读取每一列中的数据并将其转换为JSON.

I am able to read Excel file via FileReader but it outputs text as well as weird characters with it. I need to read xls file row-wise, read data in every column and convert it to JSON.

如何逐行读取xls文件?

How to read xls file row by row?

推荐答案

以下功能将Excel工作表(XLSX格式)数据转换为JSON.您可以向函数添加promise.

Below Function converts the Excel sheet (XLSX format) data to JSON. you can add promise to the function.

<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.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>

下面的帖子是否具有XLS格式的代码 Excel到JSON JavaScript代码?

Below post has the code for XLS format Excel to JSON javascript code?

这篇关于如何在Javascript/HTML5中解析Excel(XLS)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆