Home » excel » angular – Add meta data into CSV file to instruct Excel how it should open

angular – Add meta data into CSV file to instruct Excel how it should open

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m exporting CSV files which column headers texts change from English to French in an Angular application. The text displayed ok in English but not with French version, the characters with accent didn’t display correctly when opened with Excel. It worked normally when open with a text editor like Notepad or Notepad++.

Is it possible to tell Excel or any CSV support editor like Libre Office how to read the file? Like adding some embedded meta data into this CSV file before exporting it to download. I checked the documentation in W3C to find only a resource about embedded metadata but not much information after about like language, which character as delimiter…etc

Example from W3C

EXAMPLE 7: Tab-separated file containing embedded metadata

# publisher City of Palo Alto
# updated 12/31/2010
#name GID on_street species trim_cycle  inventory_date
#datatype string  string  string  string  date:M/D/YYYY
  GID On Street Species Trim Cycle  Inventory Date
  1 ADDISON AV  Celtis australis  Large Tree Routine Prune  10/18/2010
  2 EMERSON ST  Liquidambar styraciflua Large Tree Routine Prune  6/2/2010
How to&Answers:

Try adding a BOM (Byte Order Mark) to the beginning of your csv, so that Excel recognizes the character encoding correctly:

let bom = "\uFEFF";
let csv = "a;b;c;😍";
let blob = new Blob([`${bom}${csv}`], { type: 'text/csv;charset=utf-8;' });
// proceed with saving the blob ...

I’ve created a little jsfiddle: https://jsfiddle.net/h6n1cpqf/