Html 如何在将数据导出到 Excel 时更改文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15354261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the name of file while exporting data to Excel?
提问by user1877936
How do I change the name of file while exporting data to Excel?
将数据导出到 Excel 时如何更改文件名?
<div id="example" class="k-content">
<button type="button"id="btnExport">Export to csv!</button>
<div id="grid"></div>
</div>
<script>
$("#btnExport").click(function (e) {
var result = "data:application/vnd.ms-excel,";
window.open(result);
e.preventDefault();
});
</script>
When I click the export button I am getting as download.xls. Is it possible to set the file name as data.xls? Can any one explain me where I need to configure that?
当我单击导出按钮时,我得到了 download.xls。是否可以将文件名设置为 data.xls?任何人都可以解释我需要在哪里配置它吗?
回答by kvs
here is an example which demonstrates Export HTML Table to Excel With Custom File Name: http://www.kubilayerdogan.net/javascript-export-html-table-to-excel-with-custom-file-name/
这是一个示例,它演示了使用自定义文件名将 HTML 表格导出到 Excel:http: //www.kubilayerdogan.net/javascript-export-html-table-to-excel-with-custom-file-name/
回答by Erhan Gidici
Not only for excel, in addition, for many kind of format can useable.
不仅适用于excel,此外,对于多种格式都可以使用。
var element = document.createElement('a');
element.setAttribute('href', 'data:application/vnd.ms-excel,' + encodeURIComponent(htmlTable));
element.setAttribute('download', fileName);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
回答by Asenar
I had the same issue, and since the new format (maybe not supported by every browser) <a download=""></a>
the following worked fine for me. This use directly HTML/Javascript without the PHP server part, because using a submit form is too heavy for big data tables.
我遇到了同样的问题,并且由于新格式(可能不是每个浏览器都支持)<a download=""></a>
,以下对我来说效果很好。这直接使用 HTML/Javascript 而没有 PHP 服务器部分,因为使用提交表单对于大数据表来说太重了。
- changing
<button>
to<a>
- no more
window.open()
- using the basic
<a>
behavior (so, no moree.preventDefault()
) but changinghref
todata:blabla
and addingdownload="filename"
:
- 更改
<button>
为<a>
- 不再
window.open()
- 使用基本
<a>
行为(所以,没有更多e.preventDefault()
)但更改href
为data:blabla
并添加download="filename"
:
<div id="example" class="k-content">
<a href="#" id="btnExport" >Export to csv!</a>
<div id="grid"></div>
</div>
<script>
$("#btnExport").click(function (e) {
var result = "data:application/vnd.ms-excel,";
this.href = result;
this.download = "my-custom-filename.xls";
return true;
});
</script>
回答by ebms meeran
Response.AddHeader "Content-Disposition", "attachment; filename=C:\YOURFILENAME.xls;"
回答by Red
You can't do this with client-side JavaScript, you need to set the response header...
您不能使用客户端 JavaScript 执行此操作,您需要设置响应标头...
.NET
。网
Response.AddHeader("Content-Disposition", "inline;filename=filename.xls")
Or PHP
或 PHP
$filename = 'somehting.xls';
header('Content-Disposition: attachment; filename="'.$filename.'"');
回答by Pankaj Jha
var a = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data:application/vnd.ms-excel';
a.href = data_type + ', ' + encodeURIComponent(tab_text);
//setting the file name
a.download = 'SupervisorReport.xls';
//triggering the function
a.click();
//just in case, prevent default behaviour
e.preventDefault();
return (a);