Skip to content

Instantly share code, notes, and snippets.

@luckydevilru
Created August 6, 2021 09:38
Show Gist options
  • Select an option

  • Save luckydevilru/8708fce75aa26db59a6ada4a5c637676 to your computer and use it in GitHub Desktop.

Select an option

Save luckydevilru/8708fce75aa26db59a6ada4a5c637676 to your computer and use it in GitHub Desktop.
экспорт html таблицы в excel xls xlsx
<script src="https://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
var tableToExcel = (function () {
// Define your style class template.
var style = "<style>.title{font-size:18px;}.smetaTable{box-sizing: border-box;width: 100%;}.green{background-color: #31869B; color: #ffffff;}.brown{background-color:#FABF8F;font-weight:bold;font-size:13px;}.leftimg{position: absolute;margin-top:-20px}"
+".rightimg{position: absolute;}"
+".relative{position: relative;height:40px;}</style>";
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<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"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->' + style + '</head><body><table>{table}</table></body></html>'
, base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)))
}
, format = function (s, c) {
return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; })
}
return function (tableID, sheetName, fileName) {
if (!tableID.nodeType) tableID = document.getElementById(tableID)
var ctx = { worksheet: sheetName || 'Worksheet', tableID: tableID.innerHTML }
// window.location.href = uri + base64(format(template, ctx))
var a = document.createElement("a");
a.href = uri + base64(format(template, ctx)), a.download = fileName + ".xls", a.click();
}
})()
</script>
<input type="button" onclick="tableToExcel('smetaTable', 'List1', 'SmetaCrownPlaza')" class="crm-entity-widget-content-block-inner-pay-button ui-btn ui-btn-sm ui-btn-primary" value="Export в Excel" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment