0

I click on a button and this function runs to export into .xls file but the name of it is download.xls - I want to be able to change the download name... Does anyone know how to add this functionality...

here is the code for the button

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

and here is the script

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);
}

Samundra
  • 1,869
  • 17
  • 28
Ronald
  • 557
  • 1
  • 9
  • 26
  • Possible duplicate of [Changing the name of an html download](http://stackoverflow.com/questions/10037273/changing-the-name-of-an-html-download) – Cruiser Sep 20 '16 at 15:19
  • check the possible solution here : http://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link – Vishal_Kotecha Sep 20 '16 at 15:20

2 Answers2

1
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);
 }
Amit
  • 43
  • 5
0

You may probably check out this nice blog from "Kubilay Erdogan" available at the following link.

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

Basically what it does is,

  1. Gets the HTML code of your table inside your div element.
  2. Replaces the spaces in the code with correct syntax for Excel (otherwise spaces will be removed in your Excel sheet).
  3. Generates a specific file name (for minutes) in order to avoid overriding old files and to supply archiving by date values.
  4. And lastly and most importantly, saving the file with a custom file name.

Hope this helps!

David R
  • 14,711
  • 7
  • 54
  • 72