javascript - 使用另存为对话框将 HTML 表格数据导出到 Excel (JQuery)

标签 javascript jquery html mysql excel

此代码对于我的 Web 应用程序来说几乎完美无缺。感谢sampopes关于 this 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 表(它由 handlebars 和 mysql 使用数据库中的数据“填充”,但我们可以将其排除在等式之外并考虑使用静态表)。用户可以在当前版本中保存表格,但文件名始终为“download.xls”(IE 7+ 除外)。我希望用户能够在导出/下载文件时定义文件名。

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

最佳答案

You Can Use Data Table for export table data


visit link

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

关于javascript - 使用另存为对话框将 HTML 表格数据导出到 Excel (JQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38312476/

相关文章:

javascript - 使用jquery计算页面中第一级UL LI的数量

javascript - `browser default behavior` 会被 `event.preventDefault()` 阻止的一些示例是什么?

html - bootstrap 4 alpha 中的全宽导航选项卡

php - 我有一个联系表,在适合移动设备的 css 样式上,它不允许我输入数据

javascript - 同时进行ajax调用

javascript - 如何检查下拉列表是否为只读?

javascript - 在#anchor 之前删除 href 中的部分链接?

javascript - 'undefined' 不是一个函数(评估 '$(document).on' )

javascript - javascript 中共享排序函数的问题

javascript - 使用 jquery 和 ajax 将图像加载到 div 标签