Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving CSV with unique chars using in a different encoding other than UTF-8 #28

Closed
ranabra opened this issue Jun 10, 2013 · 21 comments
Closed

Comments

@ranabra
Copy link

ranabra commented Jun 10, 2013

This is not exactly an issue, rather it is a solution
( for example a solution to: #25 )

Solution: For all your special character needs, just use UTF-8, but additionally add a BOM to make sure the file is opened correctly.
CSV for example was opened by Office Excel versions prior to Office 2007 with the wrong ANSI 1252 encoding by default.
Adding a BOM will make the file open correctly (in UTF-8) in Office versions since Office 2007 (maybe needs Office SP for Office 2007)

var BOM = "\uFEFF";
var    csvData = BOM + csvData;
var blob = new Blob([csvData], { type: "text/csv;charset=utf-8" });
saveAs(blob, "myFile.csv");
@eligrey
Copy link
Owner

eligrey commented Jun 10, 2013

Ok, I linked to this in #25 for anyone who comes across this issue in the future.

@robinwassen
Copy link

I am not using this library but came across this issue since I have the same one.

The BOM will not work in Excel for Mac OS X, it will only present it with some odd characters in the beginning of the file. I have not found a solution to this issue yet.

@b4stien
Copy link

b4stien commented Feb 27, 2014

I confirm what @robinwassen has said 4 months ago... It seems that the only "universal" solution would be to generate a windows-1252 encoded CSV. Which has yet to be done in JS.

https://code.google.com/p/stringencoding/ could probably help.

@pmq
Copy link

pmq commented Apr 23, 2014

@b4stien not really the best place to talk about this, but I forked the text-encoding library just to achieve this (exporting Windows CP1252 CSV in JavaScript). See https://github.com/pmq/text-encoding and inexorabletash/text-encoding#19 for an explanation.

@linhyo
Copy link

linhyo commented Mar 4, 2015

@ranabra Thank you for your solution (encode in UTF-8 with BOM). It works !

@b4stien
Copy link

b4stien commented Mar 11, 2015

@linhyo FYI, I'm not 100% sure anymore, but last time I checked UTF-8 (with or without BOM) wasn't working on OS X with Office 2011. ANSI (also called windows-1252) seems to be the only "universal" solution. If it helps I set up an example: https://github.com/b4stien/js-csv-encoding.

@nehemiekoffi
Copy link

Works for me (encode in UTF-8 with BOM). Many thank!

@vsile
Copy link

vsile commented Aug 4, 2015

And for me! Thank you!

@montogeek
Copy link

montogeek commented Dec 17, 2015

Thanks @ranabra

@ctoro8888
Copy link

Works great. Thanks!!!!

@zh-wowtv
Copy link

zh-wowtv commented Feb 2, 2017

Can you give a example on csvData? I can't make it work for a csv with header and multiple rows. Thanks.

@tz5514
Copy link

tz5514 commented Jun 7, 2018

This issue saves my sleeping time. huge thanks!

@yuerdev
Copy link

yuerdev commented Nov 2, 2018

but office 2007 encoding is error

@umihico
Copy link

umihico commented Jul 2, 2019

To zh-wowtv

Too late but I share for other people as example.

var rrows = [
    ["name1", "あいうえお", "some other info"],
    ["name2", "city", "more info"]
];
var csvData = rrows.map(e => e.join(",")).join("\n");
var BOM = "\uFEFF";
var    csvData = BOM + csvData;
var blob = new Blob([csvData], { type: "text/csv;charset=utf-8" });
saveAs(blob, "myFile.csv");

@yuerdev
Copy link

yuerdev commented Jul 3, 2019

o , i use Xlsx.js and works okay

@koonik
Copy link

koonik commented Oct 24, 2019

It also works correctly when saving a file with swedish charset (encoded in iso-8859-2)! (thank you so much for this)

@gopinadh90
Copy link

How to save CSV file as ASCII?
I tried with type="text/csv;charset=ASCII" but its not working. Could you please help me?

@kotapeter
Copy link

You can also try this:

saveAs('data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,' + btoa(data), `PARTNER.csv`);

data is not blob type in this case.

@david-dasilva
Copy link

Thank you @ranabra you just saved my sanity 👍

@gerodf
Copy link

gerodf commented Feb 9, 2021

obrigado...

@alexboisseau
Copy link

This is not exactly an issue, rather it is a solution ( for example a solution to: #25 )

Solution: For all your special character needs, just use UTF-8, but additionally add a BOM to make sure the file is opened correctly. CSV for example was opened by Office Excel versions prior to Office 2007 with the wrong ANSI 1252 encoding by default. Adding a BOM will make the file open correctly (in UTF-8) in Office versions since Office 2007 (maybe needs Office SP for Office 2007)

var BOM = "\uFEFF";
var    csvData = BOM + csvData;
var blob = new Blob([csvData], { type: "text/csv;charset=utf-8" });
saveAs(blob, "myFile.csv");

Works for me, thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests