通过教程,我尝试创建一个从HTML表格到excel的导出选项,并使用自定义文件名。
现在我已经有了一个漂亮的HTML表格。我使用了很多选项,我喜欢让这个选项起作用:
http://jsfiddle.net/RpKr8/这是我的tableToExcel.js:
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}
var blob = new Blob([format(template, ctx)]);
var blobURL = window.URL.createObjectURL(blob);
return blobURL;
}
})()
$("#btnExport").click(function () {
var todaysDate = moment().format('DD-MM-YYYY');
var blobURL = tableToExcel('account_table', 'test_table');
$(this).attr('download',todaysDate+'.xls')
$(this).attr('href',blobURL);
});这是HTML表:
<table class="table table-striped" id="account_table">
<tr>
<th>
header 1
</th>
<th>
header 2
</th>
</tr>
<tr>
<td>
Testing Export
</td>
<td>
Saved to todays date
</td>
</tr>
</table>
<div>
<a class="btn btn-success" id="btnExport">Export</a>
</div>我的问题是,当我按下导出按钮时,什么也没有发生?我只是不明白,在每一把小提琴上,它都是有效的。我在一个iframe中工作,尽管当我不是的时候,这种情况也会发生。
我该如何解决这个问题呢?还是有别的办法呢?
发布于 2015-11-05 06:29:54
<script type='text/javascript' src='//code.jquery.com/jquery-1.11.0.js'></script>
<script type='text/javascript' src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.7.0/moment.min.js"></script>添加了这些行,它就起作用了。
还将javscript代码分离为:
<script type='text/javascript'>//<![CDATA[
$(function(){
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}
var blob = new Blob([format(template, ctx)]);
var blobURL = window.URL.createObjectURL(blob);
return blobURL;
}
})()
$("#btnExport").click(function () {
var todaysDate = moment().format('DD-MM-YYYY');
var blobURL = tableToExcel('account_table', 'test_table');
$(this).attr('download',todaysDate+'.xls')
$(this).attr('href',blobURL);
});
});//]]>
</script>https://stackoverflow.com/questions/33532674
复制相似问题