Okay, I’ve taken a bit of a detour with work and find myself supporting a Ruby on Rails application. All brand new to me.
Recently I had to get a CSV file, generated in the Rails app to open in Excel with accents correctly displayed. The post at Exporting data to CSV and Excel in your Rails apps from plataformatec was very helpful, but for an older version of Ruby. Here’s what I did to get it working w/Ruby 2.2.2:
The important point here is that if the file is in UTF-16LE with a BOM, Excel will correctly interpret the file. I found various information about this around the web. The platformatec posting summarizes the three points that got this working:
- Use tabulations, not commas.
- Fields must NOT contain newlines.
- Use UTF-16 Little Endian to send the file to the user. And include a Little Endian BOM manually.
The open mode indicates the data is UTF-8 encoded but should be written UTF-16LE encoded.
- Exporting data to CSV and Excel in your Rails apps
- Is it possible to force Excel recognize UTF-8 CSV files automatically? This stackoverflow answer pretty much sums up what I did. I could not get UTF-8 w/a BOM to work, unfortunately.
- Ruby 2.2.2