Skip to content

Instantly share code, notes, and snippets.

@andyj
Created September 16, 2011 04:13
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save andyj/1221180 to your computer and use it in GitHub Desktop.
Save andyj/1221180 to your computer and use it in GitHub Desktop.
Paste Excel in to HTML to create at <table>
<html>
<head>
<style>
*{
font-family: arial;
font-size: 11px;
}
table{
border-collapse: collapse;
border: 1px solid silver;
}
tr{
border-bottom: 1px solid silver;
}
/*This will style the header row!*/
tr:first-child td {
background-color: #EEE;
border: 1px solid silver;
font-weight: bold;
padding: 2px;
text-align: center;
}
</style>
<script language="javascript">
function createTable() {
// Get the data
var excelData = document.getElementById('csv').value;
// split into rows
excelRow = excelData.split(String.fromCharCode(10));
// split rows into columns
for (i=0; i<excelRow.length; i++) {
excelRow[i] = excelRow[i].split(String.fromCharCode(9));
}
// start to create the HTML table
var myTable = document.createElement("table");
var myTbody = document.createElement("tbody");
// Loop over the rows
for (i=0; i<excelRow.length - 1; i++) {
// create a row in the HTML table
var myRow = document.createElement("tr");
// Loop over the columns and add TD to the TR
for (j=0; j<excelRow[i].length; j++) {
// Loop over the row columns
if (excelRow[i][j].length != 0) {
var myCell = document.createElement("td");
myCell.innerHTML = excelRow[i][j];
}
myRow.appendChild(myCell);
}
myTbody.appendChild(myRow);
}
myTable.appendChild(myTbody);
document.body.appendChild(myTable);
// console.log(myTable)
}
</script>
</head>
<body>
<p>
On your XLSX document "Select All" then "copy" the data, come to this page, put focus on the &lt;textarea> box and "paste" the text in. In some instance (large data sets) this might take a couple of seconds.
Once there simply click the button to product the table.
</p>
<textarea id="csv" placeholder="Paste XLSX content here" style="width: 300px; height: 100px;"></textarea><br/>
<input type="button" value="Create HTML Table from XLSX content" onclick="createTable()" >
</body>
</html>
@mikeres0
Copy link

Although this was created 4 years ago it's still very useful! Thanks 👍

@levicus
Copy link

levicus commented Mar 22, 2019

awesome, 8 years strong!

@tg21
Copy link

tg21 commented Jan 22, 2021

for anyone having issue of blank Excel cells being skipped in HTML Table.
remove if (excelRow[i][j].length != 0) { condition from line 53.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment