1

I have several HTML tables that I want to allow the user to export its content to a csv.

I currently have this solution implemented and it almost works perfectly:

function exportTableToCsv($table, filename) {

    var $rows = $table.find('tr:has(td,th)'),

        tmpColDelim = String.fromCharCode(11),
        tmpRowDelim = String.fromCharCode(0),

        colDelim = ' ',
        rowDelim = '"\r\n"',

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

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

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

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

Which I call like this:

$(".ExportSummary").on('click', function () {
    exportTableToCsv.apply(this, [$('#SummaryTable'), 'ExportSummary.csv']);
});

Fiddle Example


Now, my issue is that I cannot get the string formatting to work by putting the <td> in separate cells in Excel. I simply don't know how to get the text to be placed in separated cells since it is being mapped together into a whole string content.

I want the desired output that is provided with this JsFiddle - but this solution does not provide the ability to choose filename and setting the appropiate content-type (application/csv) to be recognized by the browser.

Any help is appreciated. Thanks in advance!

1 Answer 1

1

http://en.wikipedia.org/wiki/Comma-separated_values#Example

USA/UK CSV file the decimal separator is a period/full stop and the value separator is a comma. European CSV/DSV file the decimal separator is a comma and the value separator is a semicolon

I have modified a little bit your script:

function exportTableToCsv($table, filename) {
...
        // 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,th');

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

http://jsfiddle.net/mu5g1a7x/2/

Let me know, if I understood you correctly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.