Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (13.1k points)

I am reading an Excel file using 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 into JSON.

How can I read xls file row by row?

1 Answer

0 votes
by (7k points)

The code below will convert the excel sheet data into JSON.

<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>

Want to be a full stack developer? Check out the full stack developer course from Intellipaat.

Related questions

0 votes
1 answer
asked Feb 25, 2021 in Web Technology by Jake (7k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...