0

I'm writing a program, that creates a csv-File. And I have a problem right at the beginning. So, my code is

  def create_csv
      destfile = Rails.root.join("public", "reports", "statistic_csv#{id}.csv")
      csv_string = FasterCSV.generate do |out|
         out << ["row", "of", "CSV", "data"]
      end
   FasterCSV.open(destfile, "w") do |csv|
      csv << csv_string
   end
  end

I thought, I will get 4 columns in the output file, smth like this row|of|csv|data. But what I get is "row,of,CSV,data" in one cell A1. How can i solve the Problem? Thanks in advance! PS. I use ruby 1.8.7 and FasterCSV 1.5.5

jenia
  • 55
  • 6

2 Answers2

2

You are encoding the CSV string twice. This should work:

def create_csv
  destfile = Rails.root.join("public", "reports", "statistic_csv#{id}.csv")
  FasterCSV.open(destfile, "wb") do |csv|
    csv << ["row", "of", "CSV", "data"]
  end
end

You can also specify a custom column separator:

FasterCSV.open(destfile, "wb", { :col_sep => "|" }) do |csv|
  # ...
end
Stefan
  • 109,145
  • 14
  • 143
  • 218
1

I presume you're opening this in Excel. Excel may not be detecting the file as a CSV file. Try importing the data into an excel workbook as opposed to opening the file in Excel.

mcfinnigan
  • 11,442
  • 35
  • 28
  • Thanks, I did as you said, and now it looks much better. But now there are some quotation marks in the text "row of CSV data " "blabla " – jenia May 08 '13 at 09:53