在导出时重命名下载文件

时间:2016-09-20 15:15:10

标签: javascript jquery html

我单击一个按钮,此功能运行导出到.xls文件,但它的名称是download.xls - 我希望能够更改下载名称... 有谁知道如何添加此功能...

这是按钮的代码

<li class="button"><a href="javascript:;" onclick="fnExcelReport();"
    style="background-color: #4cae4c;">Export EXCEL</a></li>

这是脚本

function fnExcelReport()
{
    var tab_text = "<table border='2px'>"; 
        tab_text += "<tr bgcolor='#eeeeee' height='50'><th colspan='12' ";
        tab_text += "style='text-align:center; font-size:20px;'>COMPLETED "
        tab_text += "TRIPS </th></tr><tr bgcolor='#bf997e' height='50'"
        tab_text += "color='#FFFFFF'>"; 

    var i   = 0;
    var tab = document.getElementById('completed_trips_prov'); // id of table

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

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

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

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

    return (sa);
}

2 个答案:

答案 0 :(得分:0)

您可以在以下链接中查看“Kubilay Erdogan”中的精彩博客。

http://www.kubilayerdogan.net/javascript-export-html-table-to-excel-with-custom-file-name/

基本上它的作用是,

  1. 在div元素中获取表格的HTML代码。
  2. 使用正确的Excel语法替换代码中的空格(否则将在Excel工作表中删除空格)。
  3. 生成特定文件名(分钟数),以避免覆盖旧文件并按日期值提供存档。
  4. 最后也是最重要的是,使用自定义文件名保存文件。
  5. 希望这有帮助!

答案 1 :(得分:0)

function fnExcelReport() {
     var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";
     var textRange; var j = 0;
     tab = document.getElementById('tblData3'); // 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, "Member_Clinical_Profile.xlsx");
     }
     else {//other browser not tested on IE 11
        // sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
        // sa.document.title = "your new title";

        //new added by amit
     let a = $("<a />", {
             href: 'data:application/vnd.ms-excel,' + encodeURIComponent(tab_text),
             download: "YourFileName.xls"
         })
         .appendTo("body")
         .get(0)
         .click();
         e.preventDefault();
     }

     return (sa);
 }