【问题标题】:Export HTML table data to Excel (JQuery) WITH Save As Dialog将 HTML 表数据导出到 Excel (JQuery) WITH Save As 对话框
【发布时间】:2016-07-11 17:06:55
【问题描述】:

这段代码对于我的 Web 应用程序来说几乎完美无缺。感谢sampopesthis thread 的回答。

function fnExcelReport()
{
    var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
    var textRange; var j=0;
    tab = document.getElementById('headerTable'); // id of table

    for(j = 0 ; j < tab.rows.length ; j++) 
    {     
        tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
        //tab_text=tab_text+"</tr>";
    }

    tab_text=tab_text+"</table>";
    tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
    tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
    tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE "); 

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
    {
        txtArea1.document.open("txt/html","replace");
        txtArea1.document.write(tab_text);
        txtArea1.document.close();
        txtArea1.focus(); 
        sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
    }  
    else                 //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

    return (sa);
}

基本上,我有一个 HTML 表(由把手和 mysql 使用数据库中的数据“填充”,但我们可以将其排除在等式之外,而是考虑使用静态表)。用户可以将表格保存在当前版本中,但文件名始终为“download.xls”(IE 7+ 除外)。我希望用户能够在导出/下载文件时定义文件名。

但是有一个问题。我不能再使用任何插件(将其视为限制)

【问题讨论】:

    标签: javascript jquery html mysql excel


    【解决方案1】:

    您可以使用数据表来导出表格数据


    访问链接

    https://datatables.net/extensions/buttons/examples/initialisation/export.html

    【讨论】:

    • 问题是我不想再使用任何插件了。我已经为此找到了一些插件。仅限纯 javascript(或 jquery)。为什么?因为我已经在应用程序上安装了一百多个插件,现在我正在尝试减少它们以提高网页加载速度和性能。
    猜你喜欢
    • 2015-08-05
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多