yaha
yaha

Reputation: 68

rails gem or plugins for reading and writing excel and csv files both

Hi i want to read and write data from CSV and excel file both.Can anyone please help me that

which gem or plugin is more suitable for this.

Upvotes: 2

Views: 1436

Answers (2)

pdu
pdu

Reputation: 10413

ruby 1.9.x provides a nice and fast CSV class in Stdlib, see http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/index.html.

For spreadsheets, I'd suggest http://spreadsheet.rubyforge.org/, or as already suggested, https://github.com/hmcgowan/roo

edit
I could dig out an example for csv from my code, this may help you to get started.

import:

CSV.foreach(params[:csv][:csv_file].tempfile) do |row|
  # do something with your row ...
end

export:

@export = CSV.generate do |csv|
  # adding header columns
  csv << [
    'Column 1', 
    'Column 2', 
    'Column 3' #, ...
  ]

  @records.each do |record|
    csv << [
      record.name,
      record.description,
      record.created_at #, ...
    ]
  end
end

# if you want to send the data directly to the client (works inside a controller action)
send_data @export, :type => 'text/csv; charset=utf-8; header=present', :disposition => 'attachment: filename=export.csv'

Upvotes: 4

Kashiftufail
Kashiftufail

Reputation: 10885

Can you try roo gem for excel

 https://github.com/hmcgowan/roo

And for CSV

  https://github.com/arydjmal/to_csv

Upvotes: 2

Related Questions