Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 9, 2023 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/9ef243174d9db0826bb1ca93570e2431 to your computer and use it in GitHub Desktop.
Save code-boxx/9ef243174d9db0826bb1ca93570e2431 to your computer and use it in GitHub Desktop.
NodeJS Show Excel In HTML Table

NODEJS SHOW EXCEL IN HTML TABLE

https://code-boxx.com/excel-to-html-table-nodejs/

NOTES

  1. GIST does not allow Excel files. Convert 1-dummy.csv to 1-dummy.xlsx on your own.
  2. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Create a views folder, move 3-demo.ejs inside.
    • Install required modules - npm i express ejs xlsx
    • Start the Express and Peer server - 2-server.js.
  3. Access http://localhost/ in your browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Name Email
Joa Doe joa@doe.com
Job Doe job@doe.com
Joe Doe joe@doe.com
Jon Doe jon@doe.com
Joy Doe joy@doe.com
// (A) LOAD MODULES
const xlsx = require("xlsx"),
express = require("express");
// (B) EXPRESS SERVER & EJS
const app = express();
app.set("view engine", "ejs");
// (C) SERVE DEMO PAGE
app.get("/", (req, res) => {
// (C1) OPEN EXCEL FILE - USE FIRST WORKSHEET
var workbook = xlsx.readFile("1-dummy.xlsx"),
worksheet = workbook.Sheets[workbook.SheetNames[0]],
range = xlsx.utils.decode_range(worksheet["!ref"]);
// (C2) READ EXCEL INTO OBJECT
var data = [];
for (let row=range.s.r; row<=range.e.r; row++) {
data[row] = [];
for (let col=range.s.c; col<=range.e.c; col++) {
let cell = worksheet[xlsx.utils.encode_cell({r:row, c:col})];
data[row].push(cell.v);
}
}
// (C3) RENDER HTML TEMPLATE
res.render("3-demo", { data : data });
});
// (D) START!
app.listen(80, () => console.log(`Server running at port 80`));
<!DOCTYPE html>
<html>
<head>
<title>NodeJS Excel To HTML</title>
<style>
* { font-family: Arial, Helvetica, sans-serif; box-sizing: border-box; }
table { border-collapse: collapse; }
td { padding: 10px; }
tr:nth-child(odd) { background: #f2f2f2; }
</style>
</head>
<body>
<table>
<% data.forEach(row => { %>
<tr>
<% row.forEach(cell => { %>
<td><%= cell %></td>
<% }); %>
</tr>
<% }); %>
</table>
</body>
</html>
md views
move 3-demo.ejs views
call npm i express ejs xlsx
node 2-server.js
mkdir -m 777 views
mv ./3-demo.ejs ./views
source "npm i express ejs xlsx"
node ./2-server.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment