ruby on rails - export to excel french character -
i have following method on on action export excel 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 |format| format.html format.xls { send_data @userxls.to_csv({col_sep: "\t"}) } end end
on model to_csv method:
def self.to_csv(options = {}) csv.generate(options) |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 |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 |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 |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 |k| csv << k end end end
but on excel file i'm getting names jérôme instead of jérôme. tried:
# encoding: utf-8
on view tried every field
.force_encoding("utf-8")
but still have problem. please need this.
thanks in advance
cvs::generate
understands option :encoding
(see ruby api).
so use
format.xls { send_data @userxls.to_csv({col_sep: "\t", encoding: 'utf-8'}) }
you may think separating representation , business logic. use csv_builder
provides views user_report.csv.csvbuilder
define csv output.
cvs_builder
uses instance variable @encoding
specify output character encoding.
edit
it seems, generated csv encoded in utf-8 read if iso-8859-1 aka. latin-1.
may want try generate csv in latin-1
excel has issues importing utf-8 csv files.
depending on ruby or rails version, have use iso-8859-1
instead of latin-1
.
Comments
Post a Comment