【问题标题】:Javascript export csv file nameJavascript 导出 csv 文件名
【发布时间】:2018-07-20 03:51:44
【问题描述】:

我有 html 表格 (table id='testTable') 和 html 中的按钮:

 <button id="btnExport" onclick="javascript:xport.toCSV('testTable');">CSV</button>

和javascript代码:

toCSV: function(tableId, filename) {
var date=new Date();
this._filename = (typeof filename === 'undefined') ? tableId :      filename;
// Generate our CSV string from out HTML Table
var csv = this._tableToCSV(document.getElementById(tableId));
// Create a CSV Blob
var blob = new Blob([csv], { type: "text/csv" });

// Determine which approach to take for the download
if (navigator.msSaveOrOpenBlob) {
  // Works for Internet Explorer and Microsoft Edge
  navigator.msSaveOrOpenBlob(blob, this._filename + ".csv");
} else {      
  this._downloadAnchor(URL.createObjectURL(blob), 'csv');      
}
}


_tableToCSV: function(table) {
// We'll be co-opting `slice` to create arrays
var slice = Array.prototype.slice;

return slice
  .call(table.rows)
  .map(function(row) {
    return slice
      .call(row.cells)
      .map(function(cell) {
        return '"t"'.replace("t", cell.textContent);
      })
      .join(",");
  })
  .join("\r\n");
}

我想将文件名更改为当前日期,我该怎么做?

【问题讨论】:

    标签: javascript export-to-csv


    【解决方案1】:

    首先将toCSV 更改为:

    toCSV: function(tableId, filename) {
        var date = new Date();
        this._filename = (typeof filename === 'undefined') ? date : filename;
        // Generate our CSV string from out HTML Table
        var csv = this._tableToCSV(document.getElementById(tableId));
        // Create a CSV Blob
        var blob = new Blob([csv], { type: "text/csv" });
    
        // Determine which approach to take for the download
        if (navigator.msSaveOrOpenBlob) {
            // Works for Internet Explorer and Microsoft Edge
            navigator.msSaveOrOpenBlob(blob, this._filename + ".csv");
        } else {
            this._downloadAnchor(URL.createObjectURL(blob), 'csv',this._filename);
        }
    }
    

    第二次将_downloadAnchor 更改为:

    _downloadAnchor: function(content, ext, filename) {
        var anchor = document.createElement("a");
        anchor.style = "display:none !important";
        anchor.id = "downloadanchor";
        document.body.appendChild(anchor);
    
        // If the [download] attribute is supported, try to use it
    
        if ("download" in anchor) {
            anchor.download = filename + "." + ext;
        }
        anchor.href = content;
        anchor.click();
        anchor.remove();
    }
    

    【讨论】:

    • 你需要显示函数:_tableToCSV ,,,,问题是函数,我认为
    • 我发现了我认为的问题。按钮 toCSV('testTable') 并返回文件名 testTable。如何将日期添加到文件名
    • @Nur,还需要_downloadAnchor函数,可以先用ms浏览器试试代码
    • @Nur key 是_downloadAnchor,需要知道
    【解决方案2】:
      _downloadAnchor: function(content, ext) {
      var anchor = document.createElement("a");
      anchor.style = "display:none !important";
      anchor.id = "downloadanchor";
      document.body.appendChild(anchor);
    
      // If the [download] attribute is supported, try to use it
    
      if ("download" in anchor) {
        anchor.download = this._filename + "." + ext;
      }
      anchor.href = content;
      anchor.click();
      anchor.remove();
       }
    

    【讨论】:

    • @xianshenglu this
    猜你喜欢
    • 2012-08-12
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 2015-02-19
    • 1970-01-01
    相关资源
    最近更新 更多