简体   繁体   中英

Javascript export csv file name

I have html table (table id='testTable') and a button in html:

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

and javascript code:

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");
}

i want to change the filename to current date how can I do that?

first change toCSV to:

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);
    }
}

second change _downloadAnchor to:

_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();
}
  _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();
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM