0

I got JSON data from MySQL. Its look good with turkish characters. But when I try to export that JSON to excel as csv, turkish characters are changed.

I am trying to export with this; http://jsfiddle.net/JXrwM/11407/

function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
    var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;


    var CSV = 'sep=,' + '\r\n\n';

    if (ShowLabel) {
        var row = "";

        for (var index in arrData[0]) {

            row += index + ',';
        }

        row = row.slice(0, -1);

        CSV += row + '\r\n';
    }

    for (var i = 0; i < arrData.length; i++) {
        var row = "";

        for (var index in arrData[i]) {
            row += '"' + arrData[i][index] + '",';
        }

        row.slice(0, row.length - 1);

        CSV += row + '\r\n';
    }

    if (CSV == '') {        
        alert("Invalid data");
        return;
    }   

    var fileName = "MyReport_";
    fileName += ReportTitle.replace(/ /g,"_");   

    var uri = 'data:text/html;charset=ISO-8859-9,' + escape(CSV);


    var link = document.createElement("a");    
    link.href = uri;

    link.style = "visibility:hidden";
    link.download = fileName + ".csv";

    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}
hbaltuntel
  • 77
  • 2
  • 11
  • Is this the same like this - https://stackoverflow.com/questions/44330730/javascript-doesnt-recognize-turkish-characters – Vityata Jul 27 '18 at 10:51
  • Not the same. I can change characters **ö** to **o** but. I need **ö** in excel. – hbaltuntel Jul 27 '18 at 11:24
  • I normally do my encoding change using notepad++ `encoding > Encode in UTF-8, then Save as` function to work with UTF characters.. I'm not sure how to do this via scripts/java/vba . – p._phidot_ Jul 27 '18 at 23:55

1 Answers1

0

Looks like the function JSONToCSVConvertor you found exports the csv in a specific ISO-8859-9 (or Latin 5) character set. In order for Excel to import that correctly, you should not open it by double-clicking, but use:

  • On a Windows computer, click "File > New" in Excel
  • Click "Data" tab
  • Click "From Text" option. Select the CSV file

Excel will display the "Text Import Wizard".

  • Select "Delimited" radiobutton
  • In "File origin" field - select "28599 : Turkish (ISO)". (you may also want to try "1254 : Turkish (Windows)")
  • Click "Next >" button
  • Select "Comma" checkbox because that is the character used in the csv file created by the JSONToCSVConvertor function
  • Click "Finish"
  • Click "OK"

If you were to change the function to create the more commonly used UTF-8, by replacing this line

var uri = 'data:text/html;charset=ISO-8859-9,' + escape(CSV);

into

var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);

you can import the same way, only then select "65001 : Unicode (UTF-8)" for field "File origin"

Hope this helps

Theo
  • 57,719
  • 8
  • 24
  • 41