Hi,
Whcih is the best way to export a table as csv ? I already have the
table I want to export as a 2d array, and has disabled the layout for
that view. But I am bit confused about the final output, my current
pathetic attempt at CSV writing is
<% for row in @report
for cell in row
puts (cell)
end
end %>
but their ought to be a better way.
Any hits with clue stick is much appreciated!
raj
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
I would do it in a class method, then render :text. Also, you could do @headers["Content-Type"] = "text/plain" to cause the browser to want to download instead of display the data. Rajkumar S wrote:> Hi, > > Whcih is the best way to export a table as csv ? I already have the > table I want to export as a 2d array, and has disabled the layout for > that view. But I am bit confused about the final output, my current > pathetic attempt at CSV writing is > > <% for row in @report > for cell in row > puts (cell) > end > end %> > > but their ought to be a better way. > > Any hits with clue stick is much appreciated! > > raj > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Whcih is the best way to export a table as csv ? I already have the > table I want to export as a 2d array, and has disabled the layout for > that view. But I am bit confused about the final output, my current > pathetic attempt at CSV writing is > > <% for row in @report > for cell in row > puts (cell) > end > end %> > > but their ought to be a better way. > > Any hits with clue stick is much appreciated!CSV.... http://stdlib.rubyonrails.org/libdoc/csv/rdoc/index.html Look at the CSV:Writer class... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Just did this today.
Get the FasterCSV gem. Once you''ve got it, require it in
environment.rb.
Here''s an abbreviated version of my working controller method.
Copy/paste/modify. And you''re done!
def export_to_csv
@users = User.find(:all)
csv_string = FasterCSV.generate do |csv|
# header row
csv << ["id", "first_name", "last_name"]
# data rows
@users.each do |user|
csv << [user.id, user.first_name, user.last_name]
end
end
# send it to the browsah
send_data csv_string,
:type => ''text/csv; charset=iso-8859-1;
header=present'',
:disposition => "attachment;
filename=users_#{Time.now.strftime
("%m-%d-%Y")}.csv"
end
--
Scott Becker
Electro Interactive, Inc.
Office: 813-333-5508
http://www.ElectroInteractive.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Thanks a lot! I am now able to generate CSV successfully! raj On 11/10/06, Scott Becker <becker.scott-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Just did this today. > > Get the FasterCSV gem. Once you''ve got it, require it in environment.rb. > Here''s an abbreviated version of my working controller method. > Copy/paste/modify. And you''re done! > > def export_to_csv > @users = User.find(:all) > > csv_string = FasterCSV.generate do |csv| > # header row > csv << ["id", "first_name", "last_name"] > > # data rows > @users.each do |user| > csv << [user.id, user.first_name, user.last_name] > end > end > > # send it to the browsah > send_data csv_string, > :type => ''text/csv; charset=iso-8859-1; header=present'', > :disposition => "attachment; > filename=users_#{Time.now.strftime("%m-%d-%Y")}.csv" > end > > > -- > Scott Becker > Electro Interactive, Inc. > Office: 813-333-5508 > http://www.ElectroInteractive.com > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---