如何编写读取xls文件的javascript? [英] How to write javascript that reads xls file?

查看:79
本文介绍了如何编写读取xls文件的javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究并尝试了许多方法来读取Xls文件,但它不起作用。

一旦Javascript读取Xls文件,它就应该能够显示为Json格式。



请帮我编辑代码所以它能够读取文件并显示为Json。



我尝试过:



I have researched and tried many methods to read Xls file but it does not work.
Once the Javascript reads the Xls file then it should be able to display as Json format.

Please help me to edit the code so that it is able to read the file and display as Json.

What I have tried:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.7/xlsx.core.min.js"></script>

 var workbook = XLSX.read(data, { type: 'binary' });
        var sheet_name_list = workbook.SheetNames;
 sheet_name_list.forEach(function (y) { /* iterate through sheets */
          //Convert the cell value to Json
          var roa = XLSX.utils.sheet_to_json(workbook.Sheets[y]);
          if (roa.length > 0) {
              result = roa;
          }
      });
        <script>
   function handleFile(e) {
     //Get the files from Upload control
        var files = e.target.files;
        var i, f;
     //Loop through files
        for (i = 0, f = files[i]; i != files.length; ++i) {
            var reader = new FileReader();
            var name = f.name;
            reader.onload = function (e) {
                var data = e.target.result;

                var result;
                var workbook = XLSX.read(data, { type: 'binary' });
                
                var sheet_name_list = workbook.SheetNames;
                sheet_name_list.forEach(function (y) { /* iterate through sheets */
                    //Convert the cell value to Json
                    var roa = XLSX.utils.sheet_to_json(workbook.Sheets[y]);
                    if (roa.length > 0) {
                        result = roa;
                    }
                });
               //Get the first column first cell value
                alert(result[0].Column1);
            };
            reader.readAsArrayBuffer(f);
        }
    }

  //Change event to dropdownlist
  $(document).ready(function(){
    $('#files').change(handleFile);
  });
</script>

<input type="file" id="files" name="files"/>

推荐答案

(document).ready(function(){
(document).ready(function(){


('#files')。change(handleFile);
});
< / script>

< input type =fileid =filesname =files/>
('#files').change(handleFile); }); </script> <input type="file" id="files" name="files"/>


查看此StackOverflow主题:

json - 如何解析Excel Javascript / HTML5中的文件 - 堆栈溢出 [ ^ ]。
See this StackOverflow thread:
json - How to parse Excel file in Javascript/HTML5 - Stack Overflow[^].


这篇关于如何编写读取xls文件的javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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