Return to Snippet

Revision: 6426
at May 20, 2008 23:14 by mikegreen


Updated Code
# require 'rubygems' if using this outside of Rails
require 'fastercsv'

def dump_csv
  @users = User.find(:all, :order => "lastname ASC")
  @outfile = "members_" + Time.now.strftime("%m-%d-%Y") + ".csv"
  
  csv_data = FasterCSV.generate do |csv|
    csv << [
    "Last Name",
    "First Name",
    "Username",
    "Email",
    "Company",
    "Phone",
    "Fax",
    "Address",
    "City",
    "State",
    "Zip Code"
    ]
    @users.each do |user|
      csv << [
      user.lastname,
      user.firstname,
      user.username,
      user.email,
      user.company,
      user.phone,
      user.fax,
      user.address + " " + user.cb_addresstwo,
      user.city,
      user.state,
      user.zip
      ]
    end
  end

  send_data csv_data,
    :type => 'text/csv; charset=iso-8859-1; header=present',
    :disposition => "attachment; filename=#{@outfile}"

  flash[:notice] = "Export complete!"
end

Revision: 6425
at May 20, 2008 23:12 by mikegreen


Updated Code
# require 'rubygems' if using this outside of Rails
require 'fastercsv'

def dump_csv
  @users = User.find(:all, :order => "lastname ASC")
  @outfile = "members_" + Time.now.strftime("%m-%d-%Y") + ".csv"
  
  csv_data = FasterCSV.generate do |csv|
    csv << [
    "Last Name",
    "First Name",
    "Username",
    "Email",
    "Company",
    "Phone",
    "Fax",
    "Address",
    "City",
    "State",
    "Zip Code"
    ]
  @users.each do |user|
    csv << [
    user.lastname,
    user.firstname,
    user.username,
    user.email,
    user.company,
    user.phone,
    user.fax,
    user.address + " " + user.cb_addresstwo,
    user.city,
    user.state,
    user.zip
    ]
  end

  send_data csv_data,
    :type => 'text/csv; charset=iso-8859-1; header=present',
    :disposition => "attachment; filename=#{@outfile}"

  flash[:notice] = "Export complete!"
end

Revision: 6424
at May 20, 2008 15:26 by mikegreen


Initial Code
require 'fastercsv' # FasterCSV is not part of the Ruby Standard Library, it is a gem

def dump_csv
		@users = User.find(:all, :order => "lastname ASC")
		@outfile = "members_" + Time.now.strftime("%m-%d-%Y") + ".csv"

		csv_data = FasterCSV.generate do |csv|
			csv << [
				"Last Name",
				"First Name",
				"Username",
				"Email",
				"Company",
				"Phone",
				"Fax",
				"Address",
				"City",
				"State",
				"Zip Code"
				]
			@users.each do |user|
				csv << [
					user.lastname,
					user.firstname,
					user.username,
					user.email,
					user.company,
					user.phone,
					user.fax,
					user.address + " " + user.cb_addresstwo,
					user.city,
					user.state,
					user.zip
					]
			end
		end
		
		send_data csv_data,
			:type => 'text/csv; charset=iso-8859-1; header=present',
			:disposition => "attachment; filename=#{@outfile}"

		flash[:notice] = "Export complete!"
	end

Initial URL

                                

Initial Description

                                

Initial Title
Rails CSV Export

Initial Tags
rails, ruby, web, csv, excel

Initial Language
Rails