Javascript 将HTML表格导出到excel时无法将数据加载到excel文件
我想将HTML表格导出到excel文件。问题是表中的数据并没有导出。表头已成功导出,但数据未导出。此外,我在编码方面也有问题。某些特殊字符显示不正确。有人知道问题出在哪里吗 这是我的密码: HTML表格:Javascript 将HTML表格导出到excel时无法将数据加载到excel文件,javascript,jquery,html,export-to-excel,Javascript,Jquery,Html,Export To Excel,我想将HTML表格导出到excel文件。问题是表中的数据并没有导出。表头已成功导出,但数据未导出。此外,我在编码方面也有问题。某些特殊字符显示不正确。有人知道问题出在哪里吗 这是我的密码: HTML表格: <table id"table2excel"> <tbody> <tr> <th>First Name</th><th>Last name</th><th>Score
<table id"table2excel">
<tbody>
<tr>
<th>First Name</th><th>Last name</th><th>Score</th>
</tr>
<tr>
<td>John</td><td>Doe</td><td>23124</td>
</tr>
<tr>
<td>Jane</td><td>Doe</td><td>35555</td>
</tr>
</tbody>
</table>
第一个名字最后一个名字
约翰多二三一二四
珍妮35555
JavaScript
<script type="text/javascript">
var tableToExcel = (function() {
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]--></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(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
var tableToExcel=(函数(){
var uri='data:application/vnd.ms excel;base64,'
,模板=“{table}”
,base64=函数{return window.btoa(unescape(encodeURIComponent))}
,format=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
返回函数(表、名称){
如果(!table.nodeType)table=document.getElementById(table)
var ctx={工作表:名称| |'工作表',表:table.innerHTML}
window.location.href=uri+base64(格式(模板,ctx))
}
})()
您的html和脚本中有错误
<table id"table2excel">
应该是
<table id="table2excel">
我创作了一把小提琴:
我喜欢你在里面放了一把小提琴。@Zee嘿,这很好用,但是如果我想在按钮上点击同样的东西,如何修改它?