Table2Excel
Pure Javascript plugin that converts html table to excel format exporter
by Imad Hadi
HTML
<script src="https://raw.githubusercontent.com/masifi/table2excel/master/table2excel.min.js"></script>
<button onclick="excelit()">Export to Excel</button>
<table id="tbl">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
<tr>
<th colspan="3" align="center" class="text-center">
This table is taken from
<a href="https://www.w3schools.com/html/html_tables.asp">W3Schooles</a>
</th>
</tr>
</table>
CSS
button {
padding: 5px 15px;
border: 1px solid #090;
color: #090;
margin-bottom: 10px;
border-radius: 3px;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) { background-color: #fafbfc; }
.text-center{ text-align:center; }
JavaScript
class Table2Excel{constructor(e,t={}){this.element=document.querySelectorAll(e),this.settings={},this.settings.exclude=void 0!==t.exclude&&""!==t.exclude?t.exclude:".skip",this.settings.name=void 0!==t.name&&""!==t.name?t.name:"table2excel",this.settings.filename=void 0!==t.filename&&""!==t.filename?t.filename:"table2excel"}export(){this.template={head:'<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><head>\x3c!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>',sheet:{head:"<x:ExcelWorksheet><x:Name>",tail:"</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"},mid:"</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--\x3e</head><body>",table:{head:"<table>",tail:"</table>"},foot:"</body></html>"},this.tableRows=[],this.element.forEach((e,t)=>{var s="";e.querySelectorAll("tr:not("+this.settings.exclude+")").forEach((e,t)=>{s+="<tr>"+e.innerHTML+"</tr>"}),this.tableRows.push(s)}),this.tableToExcel(this.tableRows,this.settings.name,this.settings.sheetName)}tableToExcel(e,t,s){var i,a,l,o="";if(this.uri="data:application/vnd.ms-excel;base64,",this.base64=function(e){return window.btoa(unescape(encodeURIComponent(e)))},this.format=function(e,t){return e.replace(/{(\w+)}/g,function(e,s){return t[s]})},s=void 0===s?"Sheet":s,this.ctx={worksheet:t||"Worksheet",table:e,sheetName:s},o=this.template.head,Array.isArray(e))for(i in e)o+=this.template.sheet.head+s+i+this.template.sheet.tail;if(o+=this.template.mid,Array.isArray(e))for(i in e)o+=this.template.table.head+"{table"+i+"}"+this.template.table.tail;for(i in o+=this.template.foot,e)this.ctx["table"+i]=e[i];if(delete this.ctx.table,"undefined"!=typeof msie&&msie>0||navigator.userAgent.match(/Trident.*rv\:11\./))if("undefined"!=typeof Blob){o=[o];var n=new...