【发布时间】:2016-12-16 00:50:33
【问题描述】:
<script type="text/javascript">
$(document).ready(function () {
//getting values of current time for generating the file name
$(".toExcelButton").click(function(){
var dt = new Date();
var day = dt.getDate();
var month = dt.getMonth() + 1;
var year = dt.getFullYear();
var hour = dt.getHours();
var mins = dt.getMinutes();
var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
//creating a temporary HTML link element (they support setting file names)
var a = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('dvData');
var table_html = table_div.outerHTML.replace(/ /g, '%20');
a.href = data_type + ', ' + table_html;
//setting the file name
a.download = 'exported_table_' + postfix + '.xls';
//triggering the function
a.click();
//just in case, prevent default behaviour
e.preventDefault();
})
});
</script>
需要将 div 表导出到 excel。上面的代码在 Chrome 中运行良好,但在 IE 中无法运行。任何人都可以帮助我。
【问题讨论】:
-
你有什么错误吗?另外,在哪个版本的 IE 上不能“工作”?
-
@lonut。不,没有任何错误。 IE10+
-
那我建议你使用
$(document).on('click', '.toExcelButton', function(){ // }) -
@lonut。也试过了,但没用。
-
您也可以发布带有表格值的 HTML 吗?至少让它成为一个最小的工作示例。
标签: javascript jquery excel export