jQuery:如何在将表导出到excel时在文件上添加名称

时间:2015-07-01 02:33:04

标签: jquery excel

我将这个代码导出到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>

1 个答案:

答案 0 :(得分:-1)

$('#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