1

Iv recently written code that will help me export an SQL database into CSV using FasterCSV with rails. However some parts of my database contain Traditional Chinese Characters. When I export it i'm getting ?????? as the output in the CSV file. Iv already tried changing the $KCODE = 'u' so that FasterCSV uses UTF-8 to encode the CSV file, but no luck. Iconv to convert the encoding is giving me strange results as well. Here is the source code:

def csv
@lists = Project.find(:all, :order=> (params[:sort] + ' ' + params[:direction]), :conditions =>  ["name LIKE ?", "%#{params[:selection]}%"])

csv_string = FasterCSV.generate do |csv|
  csv << [<bold> "Status","Name","Summary","Description","Creator","Comment","Contact Information","Created Date","Updated Date"]

  @lists.each do |project|
    csv << [project.status, project.name, project.summary, project.description, project.creator, project.statusreason, project.contactinfo, project.created_at, project.updated_at]
  end
end

filename = Time.now.strftime("%Y%m%d") + ".csv"
send_data(csv_string,
  :type => 'text/csv; charset=utf-8; header=present',
  :filename => filename)

end

Thanks,

CatNip44
  • 157
  • 3
  • 13

1 Answers1

2

I'm not used to work with chinese characters, but you can try adding the :encoding option to 'u' (UTF-8) on the generate call:

...
csv_string = FasterCSV.generate(:encoding => 'u') do |csv|
...

As a side note, I'd recommend using named_scopes instead of writing this:

Project.find(:all, :order=> (params[:sort] + ' ' + params[:direction]), :conditions =>  ["name LIKE ?", "%#{params[:selection]}%"])

Good luck!

kikito
  • 51,734
  • 32
  • 149
  • 189
  • Thanks! That worked. However when I am opening it in MS excel, im still getting ???? as outputs. Im guessing its got something to do with excel rather than my export option in rails. And thanks for the hint, I've changed that section into named scopes. – CatNip44 Nov 10 '10 at 02:16
  • Excel expects the encoding to be ANSI. Just in case, try setting `:encoding` to 'e' or 's', and see if excel is able to open them. If not, you can try converting the UTF8 to ANSI manually - see http://stackoverflow.com/questions/263271/converting-utf8-to-ansi-with-ruby – kikito Nov 10 '10 at 11:45