Reputation: 2800
I am using a jQuery plugin to export HTML table to Excel. The downloaded file name using chrome is always download.xls
and using mozilla firefox it is random-string.xls
. I want file name to be created according to date. For example 23-06-2014.xls
.
Below is the custom JS block in view file
$(document).ready(function () {
$("#btnExport").click(function () {
$("#account_table").btechco_excelexport({
containerid: "account_table",
datatype: $datatype.Table
});
});
});
Upvotes: 5
Views: 21751
Reputation: 505
Instead of asking another question on how to change file-names on downloads, let me vary this answer to focus on this scenario:
To change the download file-name, we will us an anchor with download property (html5); and to change the server-file-link we will use a class-name to target the href attribute of the anchor. For some uknown reasons, I found it much easier to use class-names than Ids on this particular case.
Now, lets build our hypothetical html markup, the href is left out for surety:
<a id="download-anchor" class="generic_class_name" href="#" download="static-name.csv">Download Template</a>
Next, somewhere in your jQuery code:
//dynamically changing the server-file-link
let dynamiclink = 'dynamic-link'; //generate accordingly
$('.generic_class_name').attr('href', dynamiclink);
//dynamically changing the file-download-name
let dynaName = 'custom-name-for-file.ext'; //generate accordingly
$('#download-anchor').attr('download', dynaName )
I hope this helps as this question rates higher on such look-ups.
Upvotes: 0
Reputation: 7666
If you do not want to use this plugin there is an alternative javascript already written by people. I just modified it to have the name of the file instead of the default one.
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>',
base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)))
},
format = function (s, c) {
return s.replace(/{(\w+)}/g, function (m, p) {
return c[p];
})
}
return function (table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {
worksheet: name || 'Worksheet',
table: table.innerHTML
}
var blob = new Blob([format(template, ctx)]);
var blobURL = window.URL.createObjectURL(blob);
return blobURL;
}
})()
The jquery to set the name will be:
$("#btnExport").click(function () {
var todaysDate = moment().format('DD-MM-YYYY');
var blobURL = tableToExcel('account_table', 'test_table');
$(this).attr('download',todaysDate+'.xls')
$(this).attr('href',blobURL);
});
Example: Fiddle
Upvotes: 6
Reputation: 77627
You want to set the download
and href
attributes on the link. For example
$("#btnExport").click(function () {
var uri = $("#account_table").btechco_excelexport({
containerid: "account_table",
datatype: $datatype.Table,
returnUri: true
});
$(this).attr('download', 'ExportToExcel.xls') // set file name (you want to put formatted date here)
.attr('href', uri) // data to download
.attr('target', '_blank') // open in new window (optional)
;
});
Upvotes: 9