Skip to content

Instantly share code, notes, and snippets.

@aghuddleston
Last active August 10, 2022 19:19
Show Gist options
  • Save aghuddleston/6c9fcf5d41c7f598fdeb to your computer and use it in GitHub Desktop.
Save aghuddleston/6c9fcf5d41c7f598fdeb to your computer and use it in GitHub Desktop.
Write a CSV file using Ruby 2.2.2 which can open in Excel, properly displaying accents.
require 'csv'
module DownloadService
OPEN_MODE = "w+:UTF-16LE:UTF-8"
BOM = "\xEF\xBB\xBF" #Byte Order Mark
def student_list
File.open("#{file_name}.tsv", OPEN_MODE) do |f|
csv_file = CSV.generate({:col_sep => "\t"}) do |csv|
# header row
csv << ['First_Name', 'Middle_Name', 'Last_Name']
# put lots of rows in the csv file here
end
f.write BOM
f.write(csv_file)
end
end
end
@pvin
Copy link

pvin commented Aug 14, 2018

This helped lot. Thank you @aghuddleston

@tpberntsen
Copy link

Thanks for providing a very useful example including the BOM, @aghuddleston.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment