How to read large excel data in javascript

21:27
we are reading large excel data by using javascript. please follow below process to achieve this functionality.

Include Jquery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Include XLSX Libraries:

The below files are used to handling excel data efficiently
<!-- xlsx -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.9.2/xlsx.core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.3/FileSaver.js"></script>
<!-- xlsx -->

Script:

<script>
var 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);
        jQuery("#output").append(json_object);
        console.log(json_object);

      })

    };

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

    reader.readAsBinaryString(file);
  };
//parse Excel 
  jQuery(document).ready(function(){
   
 jQuery("#my_file_input").change(function(){
      var file = this.files[0];
   if(file)
   {
   var filename = file.name;
   var ext = filename.split('.').pop();
   switch(ext){
    case 'xlsx': ParseExcel(file);
        break;
    
   }
   
   }
   
 });
   
   
  });
  
  
  //parse Excel
</script>
<input type="file" id="my_file_input" />
Output:
<div id="output">

</div>
 

No comments:

Powered by Blogger.