讀取或寫入csv在ruby都蠻方便的,只是編碼或格式要稍微注意一下

匯出csv

header = ['name','money','age']
body = ['ruby','1000','18']

bom = "\uFEFF" # 加入BOM,解決excel中文亂碼
csv_string = CSV.generate(csv = bom) do |csv|
  csv << header
  csv << body
end

csv_string.force_encoding('big5')
# 直接產出檔案

CSV.open("export.csv","w") do |csv|
  csv << header
  csv << body
end
# 直接產出檔案

CSV.open("filename.csv", "w") do |csv|
  csv.to_io.write "\uFEFF"
  datas.each{|data| csv << data}
end

半形轉全形

在某些情況會需要轉出全形字元,照理說不用才對,可能是因為合作對方的系統太爛

其實也不太確定怎解,我單純用硬幹的方式

# convert_to_wide_word 大小寫英文數字轉全形字元
def self.convert_to_wide_word(text)
  text.gsub(/[a-v]/){|a|(a.ord + 41608).chr('big5').encode('utf-8')}
    .gsub(/[w-z]/){|a|(a.ord + 41673).chr('big5').encode('utf-8')}
    .gsub(/[A-Z]/){|a|(a.ord + 41614).chr('big5').encode('utf-8')}
    .gsub(/[0-9]/){|a|(a.ord + 41599).chr('big5').encode('utf-8')}
end

convert_to_wide_word("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
 => "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL
 MNOPQRSTUVWXYZ" 

Reference:

http://www.sitepoint.com/guide-ruby-csv-library-part/

http://blog.inheart.tw/2013/09/rubyraisl-csv-excel.html

http://salayhin.wordpress.com/2014/05/10/export-csv-file-from-rails-console/