Angular2 / Javascript表格导出到Excel

嗨,我们似乎有出口的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)) } } 

以下是输出的屏幕截图: Excel文件

正如你可以看到在parsing中包含某些html元素,我如何正确格式化?

我们计算出来不妨回答这个问题,如果有人需要一个参考,我们只是将“decodeURI”改为“decodeURIComponent”,而且格式正常。