如何更改下载的csv文件的文件名?

时间:2015-07-13 13:05:15

标签: jquery csv

我使用下面的脚本从特定表生成csv下载。

我需要能够更改下载文件的名称,以便添加当前日期日期以及从中下载的表格的名称。目前它只调用csv文件export.csv

$(document).ready(function () {    
    function exportTableToCSV($table, filename) {    
        var $rows = $table.find('tr:has(td)'),

            // Temporary delimiter characters unlikely to be typed by keyboard
            // This is to avoid accidentally splitting the actual contents
            tmpColDelim = String.fromCharCode(11), // vertical tab character
            tmpRowDelim = String.fromCharCode(0), // null character

            // actual delimiter characters for CSV format
            colDelim = '","',
            rowDelim = '"\r\n"',

            // Grab text from table into CSV formatted string
            csv = '"' + $rows.map(function (i, row) {
                var $row = $(row),
                    $cols = $row.find('td');

                return $cols.map(function (j, col) {
                    var $col = $(col),
                        text = $col.text();

                    return text.replace(/"/g, '""'); // escape double quotes

                }).get().join(tmpColDelim);

            }).get().join(tmpRowDelim)
                .split(tmpRowDelim).join(rowDelim)
                .split(tmpColDelim).join(colDelim) + '"',

            // Data URI
            csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

        $(this)
            .attr({
            'download': filename,
                'href': csvData,
                'target': '_blank'
        });
    }

    // This must be a hyperlink
    $(".export").on('click', function (event) {
        // CSV
        exportTableToCSV.apply(this, [$('#dvData>table'), 'export.csv']);

        // IF CSV, don't do event.preventDefault() or return false
        // We actually need this to be a typical hyperlink
    });
});

1 个答案:

答案 0 :(得分:0)

您可以使用mencoder mf: //*.jpg -mf w = 1366: h = 768: fps = 6/60: type = jpg -ovc copy -oac copy -o images.mp4检索表格的名称:

attr()

您可以将当前日期格式化为文件名的可用字符串,如下所示:

var tableName = $('#dvData > table').attr('name');

将所有内容放在function padZeroes(value, length) { var zeroes = new Array(length).join('0'); return (zeroes + value).substr(-length); } var today = new Date(); var year = today.getFullYear().toString(); var month = padZeroes(today.getMonth() + 1, 2); var day = padZeroes(today.getDay(), 2); var hours = padZeroes(today.getHours(), 2); var minutes = padZeroes(today.getMinutes(), 2); 处理程序中:

click