# Data into excel

**URL:** <https://www.ruby-forum.com/t/data-into-excel/89364>\
**Category:** Rails\
**Created:** [March 19, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/data-into-excel/89364 "2007-03-19T23:00:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [March 19, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/data-into-excel/89364/1 "2007-03-19T23:00:25Z")

</div>

hello,

I was wondering if there is a way to create an excel file with ROR…?

Here is what i have:  
The app i have has a reports section, that section has many reports that  
use the find\_by\_sql in order to customize the query. On the main reports  
page the use is able to click and a report link and it opens up a new  
window displaying the data i want to be able to have a link that when  
clicked it creates an excel file that the user can download.

Is there any way to accomplish that…?

Thanks

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [March 19, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/data-into-excel/89364/2 "2007-03-19T23:14:08Z")

</div>

[http://wiki.rubyonrails.com/rails/pages/HowToExportToExcel](http://wiki.rubyonrails.com/rails/pages/HowToExportToExcel)

-Shawn

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [March 19, 2007, 11:15pm UTC](https://www.ruby-forum.com/t/data-into-excel/89364/3 "2007-03-19T23:15:08Z")

</div>

> I was wondering if there is a way to create an excel file with ROR…?
> 
> Here is what i have:  
> The app i have has a reports section, that section has many reports that  
> use the find\_by\_sql in order to customize the query. On the main reports  
> page the use is able to click and a report link and it opens up a new  
> window displaying the data i want to be able to have a link that when  
> clicked it creates an excel file that the user can download.
> 
> Is there any way to accomplish that…?

If you’ll settle for a CSV that will open up in Excel, then it’s pretty  
easy… here’s an example out of our site

entry = VideoGalleryEntry.find\_by\_id(params[:id])  
Tempfile.open(“video-gallery-report-#{entry.id}”) do |t|  
CSV::Writer.generate(t, ‘,’) do |csv|  
csv \<\< [“Date”, “Number of Views”]  
entry.views.sort {|a,b| a.viewed\_on \<=\> b.viewed\_on }.each do |v|  
csv \<\< [v.viewed\_on, v.views]  
end  
end  
t.size  
t.rewind  
send\_data(t.read, :type =\> ‘application/vnd.ms-excel;  
charset=iso-8859-1; header=present’,  
:filename =\>  
“video-gallery-report-#{entry.id}.csv”)  
end

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [March 20, 2007, 8:18am UTC](https://www.ruby-forum.com/t/data-into-excel/89364/4 "2007-03-20T08:18:05Z")

</div>

There is also Ruport:  
[http://stonecode.svnrepository.com/acts\_as\_reportable/trac.cgi](http://stonecode.svnrepository.com/acts_as_reportable/trac.cgi)

Wes Ratcliff

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [March 20, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/data-into-excel/89364/5 "2007-03-20T16:37:51Z")

</div>

Philip H. wrote:

> > I was wondering if there is a way to create an excel file with ROR…?
> > 
> > Here is what i have:  
> > The app i have has a reports section, that section has many reports that  
> > use the find\_by\_sql in order to customize the query. On the main reports  
> > page the use is able to click and a report link and it opens up a new  
> > window displaying the data i want to be able to have a link that when  
> > clicked it creates an excel file that the user can download.
> > 
> > Is there any way to accomplish that…?
> 
> If you’ll settle for a CSV that will open up in Excel, then it’s pretty  
> easy… here’s an example out of our site
> 
> entry = VideoGalleryEntry.find\_by\_id(params[:id])  
> Tempfile.open(“video-gallery-report-#{entry.id}”) do |t|  
> CSV::Writer.generate(t, ‘,’) do |csv|  
> csv \<\< [“Date”, “Number of Views”]  
> entry.views.sort {|a,b| a.viewed\_on \<=\> b.viewed\_on }.each do |v|  
> csv \<\< [v.viewed\_on, v.views]  
> end  
> end  
> t.size  
> t.rewind  
> send\_data(t.read, :type =\> ‘application/vnd.ms-excel;  
> charset=iso-8859-1; header=present’,  
> :filename =\>  
> “video-gallery-report-#{entry.id}.csv”)  
> end

I am having problems adapting my query for use with your code…  
@days\_done = Boards.find\_by\_sql “select id, work\_order,  
TO\_DAYS(updated\_at) - TO\_DAYS(created\_at) as Age, created\_at,  
updated\_at, notes from boards where done = 1 order by  
TO\_DAYS(updated\_at) - TO\_DAYS(created\_at) DESC”

Any ideas…?

Thanks

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [March 20, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/data-into-excel/89364/6 "2007-03-20T18:30:14Z")

</div>

Don’t forget that there’s always the option of just rendering an HTML  
table  
but reporting the content-type as application/vnd.ms-excel .

Then when someone wants a .xls document you just render an html table  
without a layout and Excel (as well as OpenOffice) will open the file up  
as  
a single-sheet excel document. I’ve yet to get this method to work with  
Google Spreadsheets or Gnumeric though.

First register the MIME type in environment.rb:  
Mime::Type.register “application/vnd.ms-excel”, :xls, [  
“application/excel”,  
“application/x-excel” ]

Then you can just have a respond\_to block with wants.xls? rendering with  
no  
layout. Provided you are setup in such a way that the convention of this  
works for you. It might take a little more work in your own application.

Taylor S.  
Reality Technician  
[http://www.realitytechnicians.com](http://www.realitytechnicians.com)
