UTF-8 encoidng issue when exporting csv file , JavaScript












9















I use the below function to export an array to a csv files in JavaScript, but the Chinese characters become messy code with Microsoft Excel 2013 in Windows7.



I open the exported file with a notepad but it displays finely.



function arrayToCSVConvertor(arrData, reportTitle) {
var CSV='';
arrData.forEach(function(infoArray, index){
var dataString = infoArray.join(",");
dataString= dataString.split('n').join(';');
CSV += dataString+ "n";
});

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

//create a link and click, remove
var link = document.createElement("a");
link.id="lnkDwnldLnk";

//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);

var csv = CSV;

var blob = new Blob([csv], { type: ' type: "text/csv;charset=UTF-8"' });//Here, I also tried charset=GBK , and it does not work either
var csvUrl = createObjectURL(blob);

var filename = reportTitle+'.csv';

if(navigator.msSaveBlob){//IE 10
return navigator.msSaveBlob(blob, filename);
}else{
$("#lnkDwnldLnk")
.attr({
'download': filename,
'href': csvUrl
});
$('#lnkDwnldLnk')[0].click();
document.body.removeChild(link);
}
}









share|improve this question





























    9















    I use the below function to export an array to a csv files in JavaScript, but the Chinese characters become messy code with Microsoft Excel 2013 in Windows7.



    I open the exported file with a notepad but it displays finely.



    function arrayToCSVConvertor(arrData, reportTitle) {
    var CSV='';
    arrData.forEach(function(infoArray, index){
    var dataString = infoArray.join(",");
    dataString= dataString.split('n').join(';');
    CSV += dataString+ "n";
    });

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

    //create a link and click, remove
    var link = document.createElement("a");
    link.id="lnkDwnldLnk";

    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);

    var csv = CSV;

    var blob = new Blob([csv], { type: ' type: "text/csv;charset=UTF-8"' });//Here, I also tried charset=GBK , and it does not work either
    var csvUrl = createObjectURL(blob);

    var filename = reportTitle+'.csv';

    if(navigator.msSaveBlob){//IE 10
    return navigator.msSaveBlob(blob, filename);
    }else{
    $("#lnkDwnldLnk")
    .attr({
    'download': filename,
    'href': csvUrl
    });
    $('#lnkDwnldLnk')[0].click();
    document.body.removeChild(link);
    }
    }









    share|improve this question



























      9












      9








      9


      3






      I use the below function to export an array to a csv files in JavaScript, but the Chinese characters become messy code with Microsoft Excel 2013 in Windows7.



      I open the exported file with a notepad but it displays finely.



      function arrayToCSVConvertor(arrData, reportTitle) {
      var CSV='';
      arrData.forEach(function(infoArray, index){
      var dataString = infoArray.join(",");
      dataString= dataString.split('n').join(';');
      CSV += dataString+ "n";
      });

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

      //create a link and click, remove
      var link = document.createElement("a");
      link.id="lnkDwnldLnk";

      //this part will append the anchor tag and remove it after automatic click
      document.body.appendChild(link);

      var csv = CSV;

      var blob = new Blob([csv], { type: ' type: "text/csv;charset=UTF-8"' });//Here, I also tried charset=GBK , and it does not work either
      var csvUrl = createObjectURL(blob);

      var filename = reportTitle+'.csv';

      if(navigator.msSaveBlob){//IE 10
      return navigator.msSaveBlob(blob, filename);
      }else{
      $("#lnkDwnldLnk")
      .attr({
      'download': filename,
      'href': csvUrl
      });
      $('#lnkDwnldLnk')[0].click();
      document.body.removeChild(link);
      }
      }









      share|improve this question
















      I use the below function to export an array to a csv files in JavaScript, but the Chinese characters become messy code with Microsoft Excel 2013 in Windows7.



      I open the exported file with a notepad but it displays finely.



      function arrayToCSVConvertor(arrData, reportTitle) {
      var CSV='';
      arrData.forEach(function(infoArray, index){
      var dataString = infoArray.join(",");
      dataString= dataString.split('n').join(';');
      CSV += dataString+ "n";
      });

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

      //create a link and click, remove
      var link = document.createElement("a");
      link.id="lnkDwnldLnk";

      //this part will append the anchor tag and remove it after automatic click
      document.body.appendChild(link);

      var csv = CSV;

      var blob = new Blob([csv], { type: ' type: "text/csv;charset=UTF-8"' });//Here, I also tried charset=GBK , and it does not work either
      var csvUrl = createObjectURL(blob);

      var filename = reportTitle+'.csv';

      if(navigator.msSaveBlob){//IE 10
      return navigator.msSaveBlob(blob, filename);
      }else{
      $("#lnkDwnldLnk")
      .attr({
      'download': filename,
      'href': csvUrl
      });
      $('#lnkDwnldLnk')[0].click();
      document.body.removeChild(link);
      }
      }






      javascript csv character-encoding mime-types






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 14 '15 at 4:34







      Jaskey

















      asked Aug 12 '15 at 8:06









      JaskeyJaskey

      8,558875107




      8,558875107
























          2 Answers
          2






          active

          oldest

          votes


















          27














          Problem solved by adding BOM at the start of the csv string:



          var csv = "ufeff"+CSV;





          share|improve this answer































            1














            This is my solution:



            var blob = new Blob(["uFEFF"+csv], {
            type: 'text/csv; charset=utf-18'
            });





            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31959487%2futf-8-encoidng-issue-when-exporting-csv-file-javascript%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              27














              Problem solved by adding BOM at the start of the csv string:



              var csv = "ufeff"+CSV;





              share|improve this answer




























                27














                Problem solved by adding BOM at the start of the csv string:



                var csv = "ufeff"+CSV;





                share|improve this answer


























                  27












                  27








                  27







                  Problem solved by adding BOM at the start of the csv string:



                  var csv = "ufeff"+CSV;





                  share|improve this answer













                  Problem solved by adding BOM at the start of the csv string:



                  var csv = "ufeff"+CSV;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 14 '15 at 4:34









                  JaskeyJaskey

                  8,558875107




                  8,558875107

























                      1














                      This is my solution:



                      var blob = new Blob(["uFEFF"+csv], {
                      type: 'text/csv; charset=utf-18'
                      });





                      share|improve this answer






























                        1














                        This is my solution:



                        var blob = new Blob(["uFEFF"+csv], {
                        type: 'text/csv; charset=utf-18'
                        });





                        share|improve this answer




























                          1












                          1








                          1







                          This is my solution:



                          var blob = new Blob(["uFEFF"+csv], {
                          type: 'text/csv; charset=utf-18'
                          });





                          share|improve this answer















                          This is my solution:



                          var blob = new Blob(["uFEFF"+csv], {
                          type: 'text/csv; charset=utf-18'
                          });






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 21 '18 at 1:05









                          Suit Boy Apps

                          1,55332343




                          1,55332343










                          answered Nov 20 '18 at 21:09









                          Santy SCSanty SC

                          312




                          312






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31959487%2futf-8-encoidng-issue-when-exporting-csv-file-javascript%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              How to pass form data using jquery Ajax to insert data in database?

                              National Museum of Racing and Hall of Fame

                              Guess what letter conforming each word