Reputation: 5411
I have the following method on on my action to export to excel a list of user:
def users_report
@users = Kid.where(confirmation_token: nil).paginate(:page => params[:page], :per_page => 30)
@userxls = Kid.where(confirmation_token: nil)
respond_to do |format|
format.html
format.xls { send_data @userxls.to_csv({col_sep: "\t"}) }
end
end
On my model the to_csv method:
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv << ["Name", "Surname", "E-mail", "Age", "School", "Class", "Native Language", "Practised Language", "Conversations same Native", "Convserations different Native", "Message same Native", "Message different Native", "Posts", "Clossed/Finished calls", "Missed calls", "Connections per Week", "Nb of foreign friends", "Nb of friends same country", "Activation Date", "Email Parent", "Parent Activated"]
kids = Array.new
all.each do |kid|
if kid.user_role != "admin"
k = Array.new
kid.name = kid.name rescue "No name"
k << kid.name
kid.surname = kid.surname rescue "No surname"
k << kid.surname
kid.email = kid.email rescue "No email"
k << kid.email
k << kid.age rescue "No age"
if !kid.school.nil?
k << kid.school.name
else
k << "No School"
end
if kid.courses.empty?
k << "No Courses"
else
k << kid.courses.first.name
end
if !kid.native_languages.empty?
languages = Array.new
kid.native_languages.each do |lang|
languages << Language.find(lang).name
end
k << languages
else
k << "No native language"
end
if !kid.practice_languages.empty?
languages = Array.new
kid.practice_languages.each do |lang|
languages << Language.find(lang).name
end
k << languages
else
k << "No practise language"
end
k << kid.number_of_native_conversations rescue "0"
k << kid.number_of_foreign_conversations rescue "0"
k << kid.number_of_native_messages rescue "0"
k << kid.number_of_foreign_messages rescue "0"
k << kid.number_of_activity_posts rescue "0"
k << kid.number_of_finished_closed_calls rescue "0"
k << kid.number_of_missed_calls rescue "0"
k << kid.avg_of_connections_week rescue "0"
k << kid.number_of_foreign_friends rescue "0"
k << kid.number_of_friends_same_country rescue "0"
k << kid.confirmed_at.try(:strftime, "%d/%m/%Y") rescue "0"
k << kid.tutor.email rescue "No parent email"
k << kid.tutor.confirmed? rescue "No parent email"
kids << k
end
end
kids.each do |k|
csv << k
end
end
end
But on my excel file I'm getting names like Jérôme instead of Jérôme. I tried:
# encoding: utf-8
on my view also tried for every field
.force_encoding("UTF-8")
But I still have this problem. Please I really need help with this.
Thanks in advance
Upvotes: 1
Views: 1061
Reputation: 8658
CVS::generate
understands an option :encoding
(see Ruby API).
So use
format.xls { send_data @userxls.to_csv({col_sep: "\t", encoding: 'UTF-8'}) }
You may also think about separating representation and business logic. I use csv_builder
that provides views like user_report.csv.csvbuilder
to define the csv output.
cvs_builder
uses the instance variable @encoding
to specify the output character encoding.
Edit
It seems, like your generated csv is already encoded in UTF-8 but you read it as if it were ISO-8859-1 aka. LATIN-1.
You may want to try to generate the csv in LATIN-1
as excel has issues importing UTF-8 csv files.
Depending on the Ruby or Rails version, you have to use ISO-8859-1
instead of LATIN-1
.
Upvotes: 1