Javascript Display Excel In HTML Table (Simple Example)

Welcome to a tutorial on how to display an Excel file in an HTML table in Javascript. Need to open an Excel file and “convert” it into an HTML table? Yes, that is possible with modern Javascript. No server-side scripts, no uploading involved. Read on for the example!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

Download & Notes Excel To Table Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

  • Download and unzip into your HTTP folder.
  • Acces 2-excel-table.html in the browser. Captain Obvious, use http://, not file://.
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

EXCEL TO HTML TABLE

All right, let us now get into the example of reading an Excel file in Javascript, and generating an HTML table with it.

 

PART 1) DUMMY EXCEL FILE

NAME EMAIL
Jo Doe jo@doe.com
Job Doe job@doe.com
Joe Doe joe@doe.com
Jog Doe jog@doe.com
Joh Doe joh@doe.com
Joi Doe joi@doe.com
Jol Doe jol@doe.com
Jon Doe jon@doe.com
Jou Doe jou@doe.com
Joy Doe joy@doe.com

First, let us start with the Excel file… There is nothing “special” here, this is just a list of dummy users.

 

PART 2) HTML PAGE

2-excel-table.html
<!-- (A) JAVASCRIPT -->
<!-- https://sheetjs.com/ -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
<script defer src="3-excel-table.js"></script>
 
<!-- (B) FILE PICKER -->
<input type="file" id="demoA" accept=".xls,.xlsx"/>
 
<!-- (C) EMPTY TABLE -->
<table id="demoB"></table>

Next, the HTML page itself should be pretty self-explanatory too.

  1. Javascript does not have native functions to read Excel files, so we using a library called “SheetJS” here. Links to their full documentation are in the extras section below.
  2. Just a normal <input type="file"> file input field, restricted to accept Excel files only. But take note, SheetJS can actually open CSV and ODT files too.
  3. An empty <table> to display the Excel file.

 

 

PART 3) EXCEL TO HTML TABLE

3-excel-table.js
document.getElementById("demoA").onchange = (evt) => {
  // (A) NEW FILE READER
  var reader = new FileReader();
 
  // (B) ON FINISH LOADING
  reader.addEventListener("loadend", (evt) => {
    // (B1) GET HTML TABLE
    var table = document.getElementById("demoB");
    table.innerHTML = "";
 
    // (B2) GET THE FIRST WORKSHEET
    var workbook = XLSX.read(evt.target.result, {type: "binary"}),
        worksheet = workbook.Sheets[workbook.SheetNames[0]],
        range = XLSX.utils.decode_range(worksheet["!ref"]);
 
    // (B3) READ EXCEL CELLS & INSERT ROWS/COLUMNS
    for (let row=range.s.r; row<=range.e.r; row++) {
      let r = table.insertRow();
      for (let col=range.s.c; col<=range.e.c; col++) {
        let c = r.insertCell(),
        xcell = worksheet[XLSX.utils.encode_cell({r:row, c:col})];
        c.innerHTML = xcell.v;
      }
    }
  });
 
  // (C) START - READ SELECTED EXCEL FILE
  reader.readAsArrayBuffer(evt.target.files[0]);
};

Lastly, not going to explain this line-by-line. But when the user picks a file with <input type="file">:

  1. We create a new FileReader() to read the selected Excel file.
  2. Use SheetJS to loop through the rows and columns, and generate the HTML table.
  3. Read the selected Excel file as an array buffer. This effectively starts (B).

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

COMPATIBILITY CHECKS

This example works on all modern “Grade A” browsers.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.