1

My spreadsheet data, are coming with plain characters are coming so "Angélica" and needed to be "Angélica". But in html table is normal as it should be, only when I download it stays that way.

It has some error in the code?

jQuery.fn.tableToCSV = function() {

    var clean_text = function(text){
        text = text.replace(/"/g, '""');
        return '"'+text+'"';
    };

    $(this).each(function(){
            var table = $(this);
            var caption = $(this).find('caption').text();
            var title = [];
            var rows = [];

            $(this).find('tr').each(function(){
                var data = [];
                $(this).find('th').each(function(){
                    var text = clean_text($(this).text());
                    title.push(text);
                    });
                $(this).find('td').each(function(){
                    var text = clean_text($(this).text());
                    data.push(text);
                    });
                data = data.join(";");
                rows.push(data);
                });
            title = title.join(";");
            rows = rows.join("\n");

            var csv = title + rows;
            var uri = 'data:text/csv;charset=UTF-8,' + encodeURIComponent(csv);
            var download_link = document.createElement('a');
            download_link.href = uri;
            var ts = new Date().getTime();
            if(caption==""){
                download_link.download = ts+".csv";
            } else {
                download_link.download = caption+"-"+ts+".csv";
            }
            document.body.appendChild(download_link);
            download_link.click();
            document.body.removeChild(download_link);
    });

};

The table is in this page

  • Your question is unclear. Where is your data coming from? At what point(s) are your data correct? – RegularlyScheduledProgramming Nov 01 '16 at 18:52
  • Coming from a `html table`, in this table everything is spelled correctly, without the strange characters. – Pedro Henrique Kuzminskas Nov 01 '16 at 19:00
  • You might benefit from using something like: http://papaparse.com/. You could then keep your data in either JSON, or as a CSV and save yourself the conversion cost. You would then have to build your HTML table programmatically. Up to you. – RegularlyScheduledProgramming Nov 01 '16 at 19:08
  • What is the spreadsheet client in question? I'd guess that problem is not in the encoder or CSV file, but in the fact that your application uses wrong (default, presumably ISO-8859-1) encoding instead of UTF-8. What might help is incusion of BOM into the dataURI: `'..charset=utf-8,%EF%BB%BF'+ encodeURIComponent(csv)` but will presumablny not be a panacea, see http://stackoverflow.com/questions/6588068/which-encoding-opens-csv-files-correctly-with-excel-on-both-mac-and-windows – myf Nov 01 '16 at 19:21

0 Answers0