UTF-8 encoidng issue when exporting csv file , JavaScript
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
add a comment |
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
add a comment |
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
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
javascript csv character-encoding mime-types
edited Aug 14 '15 at 4:34
Jaskey
asked Aug 12 '15 at 8:06
JaskeyJaskey
8,558875107
8,558875107
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Problem solved by adding BOM at the start of the csv string:
var csv = "ufeff"+CSV;
add a comment |
This is my solution:
var blob = new Blob(["uFEFF"+csv], {
type: 'text/csv; charset=utf-18'
});
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Problem solved by adding BOM at the start of the csv string:
var csv = "ufeff"+CSV;
add a comment |
Problem solved by adding BOM at the start of the csv string:
var csv = "ufeff"+CSV;
add a comment |
Problem solved by adding BOM at the start of the csv string:
var csv = "ufeff"+CSV;
Problem solved by adding BOM at the start of the csv string:
var csv = "ufeff"+CSV;
answered Aug 14 '15 at 4:34
JaskeyJaskey
8,558875107
8,558875107
add a comment |
add a comment |
This is my solution:
var blob = new Blob(["uFEFF"+csv], {
type: 'text/csv; charset=utf-18'
});
add a comment |
This is my solution:
var blob = new Blob(["uFEFF"+csv], {
type: 'text/csv; charset=utf-18'
});
add a comment |
This is my solution:
var blob = new Blob(["uFEFF"+csv], {
type: 'text/csv; charset=utf-18'
});
This is my solution:
var blob = new Blob(["uFEFF"+csv], {
type: 'text/csv; charset=utf-18'
});
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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