【问题标题】:How to change the name of file while exporting data to Excel?将数据导出到 Excel 时如何更改文件名?
【发布时间】:2013-02-27 13:18:59
【问题描述】:

如何在将数据导出到 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>

当我单击导出按钮时,我得到的是 download.xls。是否可以将文件名设置为 data.xls?谁能解释一下我需要在哪里进行配置?

【问题讨论】:

  • hai 我已经检查了 response.addheader 但没有用。是否有任何替代方法可以更改文件名
  • 当我添加 response.Addheader 时,它显示了类似未定义标识符的错误

标签: javascript html kendo-ui


【解决方案1】:

这是一个示例,它演示了使用自定义文件名将 HTML 表格导出到 Excel:http://www.kubilayerdogan.net/javascript-export-html-table-to-excel-with-custom-file-name/

【讨论】:

    【解决方案2】:

    不仅适用于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);
    

    https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,并且由于新格式(可能不是每个浏览器都支持)&lt;a download=""&gt;&lt;/a&gt; 以下对我来说很好。这直接使用 HTML/Javascript 没有 PHP 服务器部分,因为使用提交表单对于大数据表来说太重了。

      • &lt;button&gt; 更改为&lt;a&gt;
      • 没有了window.open()
      • 使用基本的&lt;a&gt; 行为(因此,不再使用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>
      

      【讨论】:

      • 效果很好,但由于某种原因将button 更改为a 导致Windows 阻止文件(weblogs.asp.net/dixin/…)。知道如何解决这个问题吗?
      【解决方案4】:

      你不能用客户端 JavaScript 做到这一点,你需要设置响应头...

      .NET

      Response.AddHeader("Content-Disposition", "inline;filename=filename.xls")
      

      或 PHP

      $filename = 'somehting.xls';
      
      header('Content-Disposition: attachment; filename="'.$filename.'"');
      

      【讨论】:

      • jsfiddle.net/SZBrt/11 我放了代码,但我收到错误,你能在这个小提琴中更新吗
      • 是的,我尝试使用响应。添加标头时,按钮导出不起作用
      【解决方案5】:
      Response.AddHeader "Content-Disposition", "attachment; filename=C:\YOURFILENAME.xls;"
      

      【讨论】:

      • 虽然这回答了问题,但解释可以帮助每个人了解发生了什么。
      【解决方案6】:
      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);
      

      【讨论】:

        猜你喜欢
        • 2013-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多