0

I want to export my html table to csv file. I have found java script as per follow. Table is getting downloaded but data are not there in the table. Can anyone tell me where is the problem. exact code is working here http://jsfiddle.net/terryyounghk/KPEGU/

$(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('"', '""'); // 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
        });
    });
Chirag
  • 365
  • 1
  • 2
  • 14
  • 1
    Your fiddle is working just fine. I can see the data in the downloaded file. – Abhitalks Jun 18 '14 at 12:11
  • fiddle is working for me – Subodh Ghulaxe Jun 18 '14 at 12:15
  • The only problem is that your filename isn't getting applied. The reason is that the new HTML5 download attribute works either on blob urls or filesystem urls. You have to convert your url into a blob url. The solution for that is here: http://stackoverflow.com/a/23956661/1355315 – Abhitalks Jun 18 '14 at 12:21
  • And it seems like a bug in Chrome 35. – Abhitalks Jun 18 '14 at 12:23
  • possible duplicate of [Anchor tag download attribute not working :Bug in Chrome 35.0.1916.114](http://stackoverflow.com/questions/23816005/anchor-tag-download-attribute-not-working-bug-in-chrome-35-0-1916-114) – Abhitalks Jun 18 '14 at 12:23
  • What is the importance of "String.fromCharCode(11)" Statement ?? – Chirag Jun 18 '14 at 12:27
  • @user3509797: Its is explained clearly in the comments. Please go thru the code carefully. – Abhitalks Jun 18 '14 at 12:29

0 Answers0