4

RUBY_VERSION = ruby-1.8.7-p358 RAILS_VERSION = rails (2.3.2)

code snippet

```
def export_to_excel
    csv_text = get_csv_string
    headers['Content-Type'] = "application/vnd.ms-excel"
    headers['Cache-Control'] = ''
    send_data csv_text, :type => 'text/csv; charset=utf-8; header=present', :disposition => "attachment;filename = "new_report.csv"
end
```

Problem : When I download the file and open it in Excel or Libre office it shows junk characters. I have to manually go and set the encoding as UTF-8 for the characters in file to be recognised.

Thing I've tried out

  1. Character encoding issue exporting rails data to CSV
  2. changing the csv string using Iconv to WINDOWS-1252 using Iconv.iconv('UTF-8//IGNORE', 'WINDOWS-1252', final_csv_str)
  3. I'm using FasterCSV.generate to create the csv string. I've tried using FasterCSV.generate(:encoding => "u"). This also didn't work since the default itself is utf-8.
  4. When I opened the file in LibreOffice by default it was selecting the encoding as ISO-8859-14. I tried converting UTF-8 to that format as well. It didn't workout.

I'm guessing there is some kind of header that I should prepend to the file itself, for the file to be recognised as UTF-8 encoding, Since setting the charset=utf-8 will only notify the browser.

Can anybody suggest some ideas ?

UPDATE :

My actual requirement was to make the files work without needing the user to do any additional steps. For this requirement the following solution works

  1. Convert CSV to UTF-16
  2. Insert BOM at beginning of file
  3. Use tab as field separator

Someone went and tried out all combinations of the above three and found this out. Reference : https://wiki.scn.sap.com/wiki/display/ABAP/CSV+tests+of+encoding+and+column+separator?original_fqdn=wiki.sdn.sap.com

The source which led to above link is from this post :

Microsoft Excel mangles Diacritics in .csv files?

For our use case just prepending BOM character sequence ("\xEF\xBB\xBF") to beginning of csv string works.

Reference :

How to write BOM marker to a file in Ruby

Other useful questions

  1. Is it possible to force Excel recognize UTF-8 CSV files automatically?
  2. https://softwareengineering.stackexchange.com/questions/372692/should-utf-8-csv-files-contain-a-bom-byte-order-mark
  3. What's different between UTF-8 and UTF-8 without BOM?
  4. How to write BOM marker to a file in Ruby
Bala
  • 61
  • 5

0 Answers0