# CSV Export from view

**URL:** <https://www.ruby-forum.com/t/csv-export-from-view/75414>\
**Category:** Rails\
**Created:** [November 9, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/csv-export-from-view/75414 "2006-11-09T19:37:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://www.ruby-forum.com/u/Rajkumar_S)\
**Post date:** [November 9, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/csv-export-from-view/75414/1 "2006-11-09T19:37:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://www.ruby-forum.com/u/Rajkumar_S)\
**Post date:** [November 9, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/csv-export-from-view/75414/2 "2006-11-09T20:22:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://www.ruby-forum.com/u/Rajkumar_S)\
**Post date:** [November 9, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/csv-export-from-view/75414/3 "2006-11-09T20:26:08Z")

</div>

> 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](http://stdlib.rubyonrails.org/libdoc/csv/rdoc/index.html)

Look at the CSV:Writer class…

---

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://www.ruby-forum.com/u/Rajkumar_S)\
**Post date:** [November 10, 2006, 2:57am UTC](https://www.ruby-forum.com/t/csv-export-from-view/75414/4 "2006-11-10T02:57:06Z")

</div>

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 B.  
Electro Interactive, Inc.  
Office: 813-333-5508

> **[Electro Interactive - Web Development. Ruby on Rails. Web Standards.](http://www.electrointeractive.com)**
>
> Electro Interactive is a full service new media design and development company.

---

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://www.ruby-forum.com/u/Rajkumar_S)\
**Post date:** [November 10, 2006, 12:31pm UTC](https://www.ruby-forum.com/t/csv-export-from-view/75414/5 "2006-11-10T12:31:56Z")

</div>

Thanks a lot! I am now able to generate CSV successfully!

raj
