Introduction
In this article, we will learn how to read an Excel file from the client-side and display its contents in an HTML table by making use of the FileReader() API in HTML5 & jQuery.
The two important jQuery plugins we used here are "xlsx.core.min.js" and "xls.core.min.js" which are used to convert the data from Excel to a JSON array.
First, we will create a File Upload button, then an HTML table which is hidden first, and lastly ab Input button which upon clicking, calls the function to export Excel data to the HTML table.
<input type="file" id="excelfile" />
<input type="button" id="viewfile" value="Export To Table" onclick="ExportToTable()" />
<br />
<br />
<table id="exceltable"></table>
Running the page will look like below.

Now, we reference the jQuery plugin files "xlsx.core.min.js" and "xls.core.min.js" in the head section.
<script src="jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.7/xlsx.core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xls/0.7.4-a/xls.core.min.js"></script>
The JavaScript function ExportToTable() is given below.
function ExportToTable() {
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xlsx|.xls)$/;
/* Checks whether the file is a valid excel file */
if (regex.test($("#excelfile").val().toLowerCase())) {
var xlsxflag = false; /* Flag for checking whether excel is .xls format or .xlsx format */
if ($("#excelfile").val().toLowerCase().indexOf(".xlsx") > 0) {
xlsxflag = true;
}
/* Checks whether the browser supports HTML5 */
if (typeof (FileReader) != "undefined") {
var reader = new FileReader();
reader.onload = function (e) {
var data = e.target.result;
/* Converts the excel data into object */
if (xlsxflag) {
var workbook = XLSX.read(data, { type: 'binary' });
}
else {
var workbook = XLS.read(data, { type: 'binary' });
}
/* Gets all the sheet names of excel into a variable */
var sheet_name_list = workbook.SheetNames;
var cnt = 0; /* This is used for restricting the script to consider only the first sheet of excel */
sheet_name_list.forEach(function (y) { /* Iterate through all sheets */
/* Convert the cell value to JSON */
if (xlsxflag) {
var exceljson = XLSX.utils.sheet_to_json(workbook.Sheets[y]);
}
else {
var exceljson = XLS.utils.sheet_to_row_object_array(workbook.Sheets[y]);
}
if (exceljson.length > 0 && cnt == 0) {
BindTable(exceljson, '#exceltable');
cnt++;
}
});
$('#exceltable').show();
}
if (xlsxflag) { /* If the excel file is .xlsx extension, then create an Array Buffer from excel */
reader.readAsArrayBuffer($("#excelfile")[0].files[0]);
}
else {
reader.readAsBinaryString($("#excelfile")[0].files[0]);
}
}
else {
alert("Sorry! Your browser does not support HTML5!");
}
}
else {
alert("Please upload a valid Excel file!");
}
}



Nicolas LOUISPosted Aug 5, 2024, 10:59 AM
This is a great article. Thx very much!
Shiza KhaliqPosted Nov 3, 2020, 4:29 AM
How do I get data from excel without formatting?
Rajesh TrasuPosted Jun 10, 2020, 6:11 PM
How do I select one sheet over the other in the same excel file?
Peter SharrockPosted Apr 16, 2020, 6:09 AM
Hi, it's great but it doesn't seem to work if there are apostrophes in the excel data.
Dharmendra Kumar PanditPosted Jan 13, 2020, 12:49 AM
Nice One article. I impressed.... Thank you
AYUSH CHAUDHARYPosted Jun 27, 2019, 5:17 AM
Date format change after read using jquery 02-05-2019 to 5/4/19. please reply if any solution I change many date format but doesn't work
yaser M.S.Posted Mar 7, 2019, 12:44 PM
How can add more column for read file excel in html?
Patel SoniyaPosted Mar 5, 2019, 1:57 AM
Very nice thank you so much.
Ismael BalanaPosted Dec 17, 2018, 2:41 AM
Sir very nice thank you sommuch.. what about drect updating withoout using upload.. that excel file drecitly uploaded into html page. is that possible
Pronati DebnathPosted Oct 25, 2018, 9:15 AM
Getting warning pop-up 'Please upload a valid Excel file!' still if it is valid .xls file:(
Muthu KumarPosted Sep 25, 2018, 10:15 PM
Very nice.
Tim TatumPosted Sep 13, 2018, 11:17 AM
Nice! Thank you Midhun.
ravi prajapatiPosted Sep 12, 2018, 6:30 AM
Very nice code
Rohith AttaluriPosted Feb 1, 2018, 6:22 AM
If we want to get only the header names with index of that headers what to do ?
Harsha SPosted Dec 15, 2017, 6:58 AM
Hello Midhun, Using jquery is it possible to write the content to excel sheet .
gaurav singhPosted Dec 2, 2017, 10:17 AM
Please let me know how to solve it
gaurav singhPosted Dec 2, 2017, 10:16 AM
Hello Midhun, if i have a column with header also has empty value then it will not show into html table.
Darshan KshirasagarPosted Nov 23, 2017, 1:50 AM
Yes i am talking about empty cell only. how to solve this
Darshan KshirasagarPosted Nov 22, 2017, 4:16 AM
HI Midhun, Your Code is super working amazing but one issue is if my excel column is empty then its not showing in the table y...? is it possible to show empty column also .
matt dPosted Sep 15, 2017, 6:12 PM
Hello, I tried this method but keep getting Uncaught type Error: t.charCodeAt is not a function at mE (xlsx.min.js:21) at Object.AE [as read] (xlsx.min.js:21) at FileReader.ExportToTable.reader.onload
Sundaram SubramanianPosted Aug 19, 2017, 1:59 PM
Thanks for sharing a good one. !!!!