【问题标题】:Html tables within div export to exceldiv中的html表导出到excel
【发布时间】: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


【解决方案1】:

在 IE 中,需要将动态创建的锚标记添加到 DOM 以执行其点击事件。此外,IE 不支持下载属性:

Download attribute on A tag not working in IE

编辑:

最近我发布了很多解决这个问题的答案,这里有两个:

image does not download with it's own extension

JS Base64 string to downloadable pdf - Edge

基本上你必须在 IE 中使用 msSaveOrOpenBlob():

var tF = 'Whatever.xls';
var tB = new Blob(..);

if(window.top.navigator.msSaveOrOpenBlob){
    //Store Blob in IE
    window.top.navigator.msSaveOrOpenBlob(tB, tF)
}
else{
    //Store Blob in others
    var tA = document.body.appendChild(document.createElement('a'));
    tA.href = URL.createObjectURL(tB);
    tA.download = tF;
    tA.style.display = 'none';
    tA.click();
    tA.parentNode.removeChild(tA)
}

在上述情况下:

var tT = new XMLSerializer().serializeToString(document.querySelector('table')); //Serialised table
var tF = 'Whatever.xls'; //Filename
var tB = new Blob([tT]); //Blob

if(window.top.navigator.msSaveOrOpenBlob){
    //Store Blob in IE
    window.top.navigator.msSaveOrOpenBlob(tB, tF)
}
else{
    //Store Blob in others
    var tA = document.body.appendChild(document.createElement('a'));
    tA.href = URL.createObjectURL(tB);
    tA.download = tF;
    tA.style.display = 'none';
    tA.click();
    tA.parentNode.removeChild(tA)
}

https://jsfiddle.net/23ao1v0s/1/

【讨论】:

  • 你能帮我看看如何在 IE 中实现同样的效果吗?
  • 同样累但没有运气
  • @jinish shah: 有什么办法可以看看尝试过什么没用吗?
  • 其实漏掉了什么。现在工作正常。非常感谢
  • @jinish shah:好的。如果答案提供了所需内容,请记住接受/标记答案。
【解决方案2】:

请检查下面给出的链接。我想你会得到你的问题的解决方案 https://github.com/rainabba/jquery-table2excel

【讨论】:

  • 在我的情况下,我将在一个 div 中有多个表。对于上面的插件,我必须为表指定 id,每个表也会生成一个单独的 excel 文件。我想要单个 excel 文件中的所有表格。
猜你喜欢
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多