我想更改导出表的文件名,我该怎么办?

时间:2017-06-24 10:47:33

标签: javascript php jquery

如何从下面的代码更改我的下载文件名

<script type="text/javascript" src="https://code.jquery.com/jquery-
1.9.0.js">            </script>
<script type="text/javascript">
$(function(){
$('#button').click(function(){
    var url='data:application/vnd.ms-excel,' + 
encodeURIComponent($('#tableWrap').html()) 
    location.href=url
    return false
    })
})
</script>

1 个答案:

答案 0 :(得分:0)

下面是使用不同文件名下载excel的小提琴。 https://jsfiddle.net/cqm2psgs/1/

<div id="tableWrap">
<table>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
</table>

</div>
<button name="click me" id="button">Click me</button>

<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"> </script> 
   <script type="text/javascript"> 
   $(function(){ 
     $('#button').click(function(){ 
       var urrl='data:application/vnd.ms-excel,' + encodeURIComponent($('#tableWrap').html());
            var link=document.createElement("a");
        link.href=urrl;
        link.download= "filename.xls";
        document.body.appendChild(link);
        link.click();
        link.body.removeChild(link);
        return false;
      });
    });
    </script>