Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

javascript - Export HTML table to csv in google chrome browser

i have following function by using this i am exporting my html to Csv file. few days/months ago it was working fine in google chrome browser (still its working fine in FF). now suddenly it stops working to convert data in to csv format.

when i am clicking on export button i am able to download file but when i am trying to opening in ms excel/Libre office calc its not opening in it.

i can see even exported data also but its showing same as , separated.

can anyone suggest me whats going wrong with my code in google chrome browser.

   function exportReportTableToCSV($table, filename) {
    var dumpd='';
    var csvData ='';

    $table.each(function(){
            var $rows = $(this).find('tr:has(td)');
            var  $header =  $(this).find('tr:has(th)');                

                tmpColDelim = String.fromCharCode(11), // vertical tab character
                tmpRowDelim = String.fromCharCode(0), // null character

                colDelim = '","',
                rowDelim = '"
"',

                csv = '"' + $header.map(function (i, head) {
                    var $head = $(head),
                        $cols = $head.find('th');

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

                        if(text == " ")
                                text = "";
                        if(text == "PROGRAMS")
                                text = "";
                        console.log(text);
                        return text.replace('"', '""'); 

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

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

            csv+='
';

            csv+= '"' + $rows.map(function (i, row) {
                    var $row = $(row),
                        $cols = $row.find('td');

                    return $cols.map(function (j, col) {
                        var $col = $(col);
                        var text;
                           if($($(col)).find("input:checkbox").length > 0)
                                text = $($(col)).find("input:checkbox").prop('checked') ? 'Yes' : 'No';
                            else
                                text = $col.text();

                            if(text === "")
                            {
                                text = "";
                            }

                        return text.replace('"', '""'); 

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

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

            dumpd+=csv+"

";
       });


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

    $(this)
        .attr({
        'download': filename,
            'href': csvData,
            'target': '_blank'
    });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

after doing some more research i got the solution for this.

i have changed this

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

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

To

 var csvData = new Blob([dumpd], { type: 'text/csv' }); //new way
    var csvUrl = URL.createObjectURL(csvData);

    $(this)
    .attr({
        'download': filename,
        'href': csvUrl
    });

and it worked fine. it seems chrome has changed something in new version.

Hope it may help others.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...