Wednesday, August 31, 2011

Generate CSV file using FasterCSV

Generate csv file using different tools like FasterCSV or CSV builder.

FasterCSV is better tool to generate csv file using ruby.
First of all need to install gem using gem install fastercsv.

Create .csv file by following below steps.
 
require 'fastercsv'

file_path = Tempfile.new(
"test.csv", File.join(Rails.root,"tmp"))
FasterCSV.open(file_path.path, "w:windows-1252") do |content|
  content << ["Name", "Surname", "Birthdate", "HSC Percentage", "URL"]
  content << ["Priyanka", "Pathak", Date.parse("May 20, 1986"), 80, '=HYPERLINK("http://priyankapathak.wordpress.com","Get me here")']
end

This will simply generate test.csv file with your required format like hyperlink in csv.

Hope this post will help you.

No comments:

Post a Comment