How to parse Excel file in Javascript HTML5

0 votes
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.

How to read xls file row by row?
Jun 19, 2020 in Java-Script by kartik
• 37,510 points
13,700 views

1 answer to this question.

0 votes

Hello @kartik,

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>

Hope this is helpful!!

Thank You!!

answered Jun 19, 2020 by Niroj
• 82,880 points
How will i call this method with file..

Hello @Nik,

Can you be more specific about what you want? or Do you want know about calling function from one javascript file from another?

Related Questions In Java-Script

0 votes
1 answer

How to get the browser to navigate to URL in JavaScript?

Hii, This works in all browsers: window.location.href = '...'; If ...READ MORE

answered May 28, 2020 in Java-Script by Niroj
• 82,880 points
2,376 views
+1 vote
1 answer

How to Check synchronously if file/directory exists in Node.js?

Hello, You can use fs.existsSync(): const fs = require("fs"); // ...READ MORE

answered May 28, 2020 in Java-Script by Niroj
• 82,880 points
1,617 views
0 votes
1 answer

How to include csrf_token() in an external js file in Laravel?

Hello @kartik, To resolve this error you can ...READ MORE

answered Jun 11, 2020 in Java-Script by Niroj
• 82,880 points
3,403 views
0 votes
1 answer

How to Store PHP variable with HTML in JavaScript in Laravel Blade Template?

Hello @kartik, The double curly brackets {{ }} will always ...READ MORE

answered Jun 11, 2020 in Java-Script by Niroj
• 82,880 points
16,608 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,748 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,647 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,504 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,359 views
–1 vote
1 answer

How to read an external local JSON file in JavaScript?

Hello @kartik, For reading the external Local JSON ...READ MORE

answered Sep 3, 2020 in Java-Script by Niroj
• 82,880 points
182,463 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,880 points
12,233 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP