2015-02-07 9 views
6

ho usato il codice jQuery per esportare dati della tabella in file excel, ha ottenuto il codice da hereFornire un nome file personalizzato non funziona in table2excel jquery?

ma fornendo il nome del file custome non funziona sta prendendo il nome del file casuale.
come fornire il nome del file personalizzato dal codice.

<script> 
$(function() { 
     $("button").click(function(){ 
     $("#example").table2excel({ 
       exclude: ".noExl", 
     name: "Employee" 
     }); 
     }); 
}); 
</script> 

risposta

0

Il name variabile in questo plugin si riferisce al nome del foglio di lavoro, non il nome del file di Excel.

Se si desidera modificare il nome del file, è necessario modificare il codice del plug-in un po 'o semplicemente utilizzare un altro plug-in o codice adatto alle proprie esigenze, ad esempio this one (dove è possibile inserire il nome file nella variabile postfix).

+0

Dovrai aggiungere alcune linee html all'inizio del div che contiene il tuo tavolo. Vedi [this] (http://forums.codecharge.com/posts.php?post_id=107155) per maggiori informazioni. – Linostar

0

È possibile hackerare table2excel jQuery per dare nome personalizzato per il download come segue:

Dalle vostre JS:

<script> 
 
$(function() { 
 
     $("button").click(function(){ 
 
     $("#example").table2excel({ 
 
       exclude: ".noExl", 
 
     name: "Employee.txt" //This name will be passed for download 
 
     }); 
 
     }); 
 
}); 
 
</script>

Quindi modificare GetFileName (e.settings) chiamata in table2excel .js per denominare come segue:

if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))  // If Internet Explorer 
 
      { 
 
       if (typeof Blob !== "undefined") { 
 
        //use blobs if we can 
 
        fullTemplate = [fullTemplate]; 
 
        //convert to array 
 
        var blob1 = new Blob(fullTemplate, { type: "text/html" }); 
 
        window.navigator.msSaveBlob(blob1, name); // Changed Here 
 
       } else { 
 
        //otherwise use the iframe and save 
 
        //requires a blank iframe on page called txtArea1 
 
        txtArea1.document.open("text/html", "replace"); 
 
        txtArea1.document.write(e.format(fullTemplate, e.ctx)); 
 
        txtArea1.document.close(); 
 
        txtArea1.focus(); 
 
        sa = txtArea1.document.execCommand("SaveAs", true, name); // Changed Here 
 
       } 
 

 
      } else { 
 
       link = e.uri + e.base64(e.format(fullTemplate, e.ctx)); 
 
       a = document.createElement("a"); 
 
       a.download = name; // Changed Here 
 
       a.href = link; 
 

 
       document.body.appendChild(a); 
 

 
       a.click(); 
 

 
       document.body.removeChild(a); 
 
      }

0

1.Open jquery.table2excel.js

2.Find function getFileName(settings)

3.Modificare a

function getFileName(settings) { 
     return (settings.filename ? settings.filename : settings.name) + 
       (settings.fileext ? settings.fileext : ".xls"); 
} 

settings.name è variabile da script js vostra abitudine quando Chiamate jquery2excel Nel mio esempio appare come

$(".generateXLS").click(function(){ 
    var idOfTable = $(this).attr("data-rel"); 
    var tableName = $(this).attr("data-table-name"); 
    $("#tableN"+idOfTable).table2excel({ 
     name: tableName 
    }); 
}); 
Problemi correlati