javascript - 现在用日期时间命名文件(JS)
问题描述
我有下载 HTML 表格的代码,如 excel
这是代码
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]--></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
}
window.location.href = uri + base64(format(template, ctx));
}
})()
<input type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="Експортувати в Excel">
现在它像“download.xls”一样下载,我需要用 DateTime 命名它。我怎样才能做到这一点?
解决方案
您应该使用<a>
带有属性的元素download
来确定文件的名称。
这是一个如何动态创建<a>
元素并设置src
和download
下载属性的示例:
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]--></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
}
// here's how to download with datetime file name.
// DateTime filename
var filename = new Date().getTime();
var element = document.createElement('a');
element.setAttribute('href', uri + base64(format(template, ctx)));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
})()
var table = document.getElementsByTagName('table')[0];
var newTableToExcel = new tableToExcel(table, 'myname');
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
推荐阅读
- jquery - 需要使用 jquery 从 Salesforce Lightning 中的文本框获取价值
- c# - SceneManager 无法识别 LoadSceneAsync
- angular - 角度拦截http错误
- java - Hazelcast上的DistributedTask,Hazelcast上需要的Task实现或者调用者可以实现
- java - 如何使用邮递员休息客户端将自定义 dto 作为休息 api 输入传递
- html - 文本与图像剪辑冲突
- ruby-on-rails - 数组属性的表单选择
- jquery - 在剑道网格中保留重复的行
- xamarin - 我的 UI 没有响应方向更改
- php - 在 PHP 中删除路径中的最后一个文件夹