In this article I will explain with an example, how to convert Excel (XLS and XLSX) file data to JSON using jQuery.
Once File is selected in FileUpload control, it is read as Binary data and then the Binary data is read using the xlsx Excel plugin which returns the Excel file data in JSON array format.
Finally, JSON array is displayed as HTML Table using jQuery.
 
 
HTML Markup
The HTML Markup consists of a FileUpload control (HTML File Input) and a HTML Button.
<input type="file" id="fileUpload" />
<input type="button" id="upload" value="Upload" />
<hr />
<div id="dvExcel"></div>
 
 
Convert Excel to JSON and displaying data in HTML Table using jQuery
The Excel file is selected in FileUpload control (HTML File Input) and Upload button is clicked.
Inside the jQuery Button Click event handler, first a check is performed to verify whether the file is a valid Excel file i.e. file with extension XLS or XLSX. Then a check is performed to make sure whether the browser supports HTML5 FileReader API.
The Excel file is read as Binary data using HTML5 FileReader and then the Binary data is read using the xlsx Excel plugin which returns rows from Excel in JSON Array format.
Finally, a loop is executed over the JSON Array and the HTML Table is populated.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/xlsx.full.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/jszip.js"></script>
<script type="text/javascript">
    $("body").on("click", "#upload", function () {
        //Reference the FileUpload element.
        var fileUpload = $("#fileUpload")[0];
 
        //Validate whether File is valid Excel file.
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xls|.xlsx)$/;
        if (regex.test(fileUpload.value.toLowerCase())) {
            if (typeof (FileReader) != "undefined") {
                var reader = new FileReader();
 
                //For Browsers other than IE.
                if (reader.readAsBinaryString) {
                    reader.onload = function (e) {
                        ProcessExcel(e.target.result);
                    };
                    reader.readAsBinaryString(fileUpload.files[0]);
                } else {
                    //For IE Browser.
                    reader.onload = function (e) {
                        var data = "";
                        var bytes = new Uint8Array(e.target.result);
                        for (var i = 0; i < bytes.byteLength; i++) {
                            data += String.fromCharCode(bytes[i]);
                        }
                        ProcessExcel(data);
                    };
                    reader.readAsArrayBuffer(fileUpload.files[0]);
                }
            } else {
                alert("This browser does not support HTML5.");
            }
        } else {
            alert("Please upload a valid Excel file.");
        }
    });
    function ProcessExcel(data) {
        //Read the Excel File data.
        var workbook = XLSX.read(data, {
            type: 'binary'
        });
 
        //Fetch the name of First Sheet.
        var firstSheet = workbook.SheetNames[0];
 
        //Read all rows from First Sheet into an JSON array.
        var excelRows = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[firstSheet]);
 
        //Create a HTML Table element.
        var table = $("<table />");
        table[0].border = "1";
 
        //Add the header row.
        var row = $(table[0].insertRow(-1));
 
       //Add the header cells.
        var headerCell = $("<th />");
        headerCell.html("Id");
        row.append(headerCell);
 
        var headerCell = $("<th />");
        headerCell.html("Name");
        row.append(headerCell);
 
        var headerCell = $("<th />");
        headerCell.html("Country");
        row.append(headerCell);
 
        //Add the data rows from Excel file.
        for (var i = 0; i < excelRows.length; i++) {
            //Add the data row.
            var row = $(table[0].insertRow(-1));
 
            //Add the data cells.
            var cell = $("<td />");
            cell.html(excelRows[i].Id);
            row.append(cell);
 
            cell = $("<td />");
            cell.html(excelRows[i].Name);
            row.append(cell);
 
            cell = $("<td />");
            cell.html(excelRows[i].Country);
            row.append(cell);
        }
 
        var dvExcel = $("#dvExcel");
        dvExcel.html("");
        dvExcel.append(table);
    };
</script>
 
 
Screenshots
Contents of the Excel file placed on user’s folder
Convert Excel to JSON using jQuery
 
The Excel file being displayed in browser as HTML Table
Convert Excel to JSON using jQuery
 
 
Downloads