嗨,大家好,我們似乎都與導出excel文件在這裏的格式麻煩的是我們的代碼:Angular2/JavaScript的表格導出到Excel
import { Component,ViewContainerRef } from '@angular/core';
@Component({
selector: 'my-app',
template: `<input type="button" (click)="tableToExcel('testTable', 'W3C Example Table')" value="Export to Excel">
<table id="testTable" rules="groups" frame="hsides" border="2">
<tr>
<th>111111</th>
<th>222222</th>
</tr>
<tr>
<td>333334</td>
<td>444443x</td>
</tr>
</table>
`
})
export class AppComponent{
tableToExcel(table, name){
let 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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
, base64 = function(s) { return window.btoa(decodeURI(encodeURIComponent(s))) }
, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
}
正如你可以看到解析中包含某些html元素,我如何正確格式化它?