在 Javascript 中以正确的格式导出 HTML 表格 [英] Exporting HTML table with correct format in Javascript
本文介绍了在 Javascript 中以正确的格式导出 HTML 表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张桌子,我有一个金额栏.我想以正确的数字格式导出表格,因为当我导出表格时,我只得到 100 而不是 100.00.
I have a table and I have there a amount column. I want to export the table with the correct number format because when I do export my table, I only get 100 instead of 100.00.
我的桌子是这样的:
ID Code Amount Time
1 1 100.00 2014-09-22 18:59:25
1 1 100.60 2014-09-22 18:59:25
1 1 100.00 2014-09-22 18:59:25
1 1 12.50 2014-09-22 18:59:25
而Excel输出是这样的:
And the Excel output is like this:
ID Code Amount Time
1 1 100 2014-09-22 18:59:25
1 1 100.60 2014-09-22 18:59:25
1 1 100 2014-09-22 18:59:25
1 1 12.5 2014-09-22 18:59:25
这是我的代码:
<script>
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, filename) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {
worksheet: name || 'Worksheet',
table: table.innerHTML
}
document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = filename;
document.getElementById("dlink").click();
}
})()
function download(){
$(document).find('tfoot').remove();
var name = document.getElementById("name").innerHTML;
tableToExcel('table2', 'Sheet 1', name+'.xls')
setTimeout("window.location.reload()",0.0000001);
}
var btn = document.getElementById("btn");
btn.addEventListener("click",download);
</script>
有什么办法可以做到这一点吗?我希望我的 Excel 文件看起来与表格中的数据完全一样.
Is there any way to achieve this? I want my Excel file to look exactly like the data in my table.
推荐答案
尝试
<td style='mso-number-format:"#,##0.00"'>100.00</td>
在表格 HTML 中.
in the table HTML.
见小提琴:http://jsfiddle.net/ad3xda1z/1/
问候
阿克塞尔
这篇关于在 Javascript 中以正确的格式导出 HTML 表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文