1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| var download_excel = function(content, filename) { var eleLink = document.createElement("a"); eleLink.download = filename; eleLink.style.display = "none"; var blob = new Blob([content]); eleLink.href = URL.createObjectURL(blob); document.body.appendChild(eleLink); eleLink.click(); document.body.removeChild(eleLink); };
var format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }); };
export const table2excel = function(tableid, sheetName) { var 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 type="text/css">' + "table td {" + "border: 1px solid #999999;" + "min-width: 200px;" + " text-align: center;" + "background-color: #f3f3f3;" + "color: #333333;" + " }" + "</style>" + '</head><body ><table class="excelTable">{table}</table></body></html>'; if (!tableid.nodeType) tableid = document.getElementById(tableid); var ctx = { worksheet: sheetName || "Worksheet", table: tableid.innerHTML }; download_excel(format(template, ctx), sheetName); };
|