Providing a custom file name does not work in jQuery table2excel?

I used jquery code to export table data to excel file, got the code here

but provided that the custome name does not work, it accepts an arbitrary file name.
how to provide a custom file name from code.

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

The name variable in this plugin refers to the name of the worksheet, not the name of the Excel file.

If you want to change the file name, you will have to crack the plugin code a bit or just use another plugin or code that suits your needs, for example this one (where you can put the file name in the postfix variable).

0
source

You can hack jQuery table2excel to specify the name to load as follows:

From your js:

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

Then modify the call to getFileName (e.settings) in table2excel.js as follows:

 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
source

1. Open jquery.table2excel.js

2.Find function getFileName(settings)

3. Change it to

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

settings.name is a variable from your custom js scripts when you call jquery2excel In my example it looks like

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

Source: https://habr.com/ru/post/982129/


All Articles