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!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist | Example on CodePen

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

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.

 

TUTORIAL VIDEO

 

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 first worksheet of 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).

 

 

EXTRA) READING MULTIPLE WORKSHEETS

2-excel-table.html
<!-- (C) EMPTY CONTAINER -->
<div id="demoB"></div>

Create an empty container instead of a table.

3-excel-table.js
// (B) ON FINISH LOADING
reader.addEventListener("loadend", evt => {
  // (B1) GET DIV CONTAINER
  var div = document.getElementById("demoB");
  div.innerHTML = "";
 
  // (B2) LOOP THROUGH ALL WORKSHEETS
  var workbook = XLSX.read(evt.target.result, {type: "binary"});
  for (let worksheet of Object.values(workbook.Sheets)) {
    // (B2-1) CREATE TABLE & APPEND TO DIV CONTAINER
    var table = document.createElement("table");
    div.appendChild(table);
 
    // (B2-2) READ EXCEL CELLS & INSERT ROWS/COLUMNS
    var range = XLSX.utils.decode_range(worksheet["!ref"]);
    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;
      }
    }
  }
});

Instead of reading the first worksheet only, loop through all the worksheets – Generate the HTML table, rows, and cells as usual.

 

EXTRAS

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!

4 thoughts on “Javascript Display Excel In HTML Table (Simple Example)”

  1. Lavoro eccellente. Mostro il mio problema: ho un archivio di dati in excel. Lo voglio pubblicare a pagine (con pagina next e previous). Ho già fatto il programma caricando il file con input type=”file”. Poiché il file da caricare è conosciuto, c’è un modo di evitare il display della finestra di dialogo di input file?

    Google Translate : Excellent work. I show my problem: I have a data archive in excel. I want to publish it in pages (with next and previous page). I already made the program loading the file with input type=”file”. Since the file to upload is known, is there a way to avoid the file input dialog display?

Comments are closed.