Reputation: 53
In ruby i am generating CSV file while opening the file I mentioned it as UTF-8 encoding. The code is given below.In linux and mac it is working fine but in windows when i am trying to open the csv file excel is not recognizing as UTF-8. What can i do so that windows does recognize it as UTF-8 encoding.
CSV.open(File.join(Rails.public_path,"/csv_uploads/#{csv_name}.csv")
, "w:UTF-8").
I even manually encode the items in the file to UTF-8.
`result[2].encode('UTF-8')`.
Upvotes: 4
Views: 1515
Reputation: 1261
While writing itself need to be use open_mode and bom.
Important things to note here is open mode and bom
open_mode = "w+:UTF-16LE:UTF-8"
bom = "\xEF\xBB\xBF"
Before writing the CSV insert BOM
f.write bom
f.write(csv_file)
Example I18n content
In Mac and Linux
Swedish : Förnamn English : First name
In Windows
Swedish : Förnamn English : First name
Example code:
def user_information_report(report_file_path, user_id)
user = User.find(user_id)
I18n.locale = user.current_lang
open_mode = "w+:UTF-16LE:UTF-8"
bom = "\xEF\xBB\xBF"
body user, open_mode, bom
end
def headers
headers = [
"ID", "SDN ID",
I18n.t('sys_first_name'), I18n.t('sys_last_name'), I18n.t('sys_dob'),
I18n.t('sys_gender'), I18n.t('sys_email'), I18n.t('sys_address'),
I18n.t('sys_city'), I18n.t('sys_state'), I18n.t('sys_zip'),
I18n.t('sys_phone_number')
]
end
def body tenant, open_mode, bom
File.open(report_file_path, open_mode) do |f|
csv_file = CSV.generate(col_sep: "\t") do |csv|
csv << headers
tenant.patients.find_each(batch_size: 10) do |patient|
csv << [
patient.id, patient.patientid,
patient.first_name, patient.last_name, "#{patient.dob}",
"#{translate_gender(patient.gender)}", patient.email, "#{patient.address_1.to_s} #{patient.address_2.to_s}",
"#{patient.city}", "#{patient.state}", "#{patient.zip}",
"#{patient.phone_number}"
]
end
end
f.write bom
f.write(csv_file)
end
end
Windows and Mac
File can be opened directly by double clicking.
Linux (ubuntu)
While opening a file ask for the separator options -> choose “TAB”
Upvotes: 1