我有这段关于将表导出到 Excel 的代码。效果很好。那么我的问题是下载时如何更改文件名?
$('#export').click(function() {
window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#toexcel').html()));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="export">Export to Excel</a>
<div id="toexcel">
<table border="1">
<tr>
<td>test1</td>
<td>test2</td>
<td>test3</td>
</tr>
<tr>
<td>test1</td>
<td>test2</td>
<td>test3</td>
</tr>
<tr>
<td>test1</td>
<td>test2</td>
<td>test3</td>
</tr>
</table>
</div>
最佳答案
$('#export').click(function() {
window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#toexcel').html()));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="export" download="hello">Export to Excel</a>
<div id="toexcel">
<table border="1">
<tr>
<td>test1</td>
<td>test2</td>
<td>test3</td>
</tr>
<tr>
<td>test1</td>
<td>test2</td>
<td>test3</td>
</tr>
<tr>
<td>test1</td>
<td>test2</td>
<td>test3</td>
</tr>
</table>
</div>
您可以使用下载属性指定文件名,但这并未得到广泛支持:http://www.w3schools.com/tags/att_a_download.asp
关于jQuery:将表导出到 Excel 时如何在文件上添加名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31151679/