大家好,我们好像导出的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元素,如何正确格式化?
答案 0 :(得分:1)
我们认为如果有人需要参考我们刚刚更改了&#34; decodeURI&#34; to&#34; decodeURIComponent&#34;格式结果很好。
答案 1 :(得分:0)
我建议您使用ExcellentExport.js https://github.com/jmaister/excellentexport将表格转换为Excel。
注意:我是图书馆的开发者。