Javascript导出csv文件名

Javascript导出csv文件名,javascript,export-to-csv,Javascript,Export To Csv,我有html表格(表格id='testTable')和一个html按钮: <button id="btnExport" onclick="javascript:xport.toCSV('testTable');">CSV</button> 我想将文件名更改为当前日期如何执行此操作?首先将toCSV更改为: toCSV: function(tableId, filename) { var date = new Date(); this._filename

我有html表格(表格id='testTable')和一个html按钮:

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

我想将文件名更改为当前日期如何执行此操作?

首先将
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: 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();
}
第二次更改
\u downloadAnchor
为:

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

您需要显示函数:_tabletosv,,,,问题在于函数,我想我找到了问题所在。按钮toCSV('testTable'),它返回文件名testTable。我如何将日期添加到filename@Nur,仍然需要函数_downloadAnchor,您可以使用ms浏览器尝试该代码first@Nur关键是下载锚,需要know@xianshenglu这