Click here to Skip to main content
15,125,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"/>
Posted
Updated 23-Sep-21 3:18am

1 solution

   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900