Created
October 9, 2008 01:05
-
-
Save BrianTheCoder/15667 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'fastercsv' | |
| class Reporter | |
| # example action to return the contents | |
| # of a table in CSV format | |
| def export_users | |
| users = User.find(:all) | |
| stream_csv do |csv| | |
| csv << ["email"] | |
| users.each do |u| | |
| csv << [u.email] | |
| end | |
| end | |
| end | |
| private | |
| def stream_csv | |
| filename = params[:action] + ".csv" | |
| #this is required if you want this to work with IE | |
| if request.env['HTTP_USER_AGENT'] =~ /msie/i | |
| headers['Pragma'] = 'public' | |
| headers["Content-type"] = "text/plain" | |
| headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0' | |
| headers['Content-Disposition'] = "attachment; filename=\"#{filename}\"" | |
| headers['Expires'] = "0" | |
| else | |
| headers["Content-Type"] ||= 'text/csv' | |
| headers["Content-Disposition"] = "attachment; filename=\"#{filename}\"" | |
| end | |
| render :text => Proc.new { |response, output| | |
| csv = FasterCSV.new(output, :row_sep => "\r\n") | |
| yield csv | |
| } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment