# Server out to excel

**URL:** <https://www.ruby-forum.com/t/server-out-to-excel/60896>\
**Category:** Rails\
**Created:** [May 18, 2006, 11:13am UTC](https://www.ruby-forum.com/t/server-out-to-excel/60896 "2006-05-18T11:13:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [May 18, 2006, 11:13am UTC](https://www.ruby-forum.com/t/server-out-to-excel/60896/1 "2006-05-18T11:13:40Z")

</div>

How would one server out to excel a html table with the mime/content  
type  
application/vnd.ms-excel ?

In PHP it would be something like

?php  
header(“Content-Type: application/vnd.ms-excel”);

* * *

But how do I do this in rails?

Cheers Glenn

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [May 19, 2006, 1:40am UTC](https://www.ruby-forum.com/t/server-out-to-excel/60896/2 "2006-05-19T01:40:30Z")

</div>

Tom W. wrote:

> On 5/18/06, Glenn C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > How would one server out to excel a html table with the mime/content  
> > type  
> > application/vnd.ms-excel ?
> 
> send\_data(excel\_data, :type =\> “application/vnd.ms-excel”, :filename  
> =\> filename)
> 
> Where excel\_data is the data you want to send, and filename is the  
> suggested filename for the data.
> 
> Tom

Sorry to ask a real dumb question, but where would I put the send\_data  
command?

I have a mylist.rhtml which presently creates a formated table list as  
html.  
I want a button that says “View in Excel”, that more or less renders  
mylist.rhtml without the standard wrapping layout. When the user clicks  
“View in Excel” my.rhtml is rendered but this time browser receives http  
header of “application/vnd.ms-excel” rather than the default of  
“text/html”

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [May 18, 2006, 11:31am UTC](https://www.ruby-forum.com/t/server-out-to-excel/60896/3 "2006-05-18T11:31:02Z")

</div>

On 5/18/06, Glenn C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How would one server out to excel a html table with the mime/content  
> type  
> application/vnd.ms-excel ?

send\_data(excel\_data, :type =\> “application/vnd.ms-excel”, :filename  
=\> filename)

Where excel\_data is the data you want to send, and filename is the  
suggested filename for the data.

Tom

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [May 19, 2006, 4:22am UTC](https://www.ruby-forum.com/t/server-out-to-excel/60896/4 "2006-05-19T04:22:33Z")

</div>

unknown wrote:

> you would put send\_data in a seperate controller, then the “View in  
> Excel” button would link to that controller.  
> -N

Well I am feeling really brain dead now.

The following calls up excel nicely and displays a number of fields.

* * *

class ExcelController \< ApplicationController

def doexcel  
mytext = “

| 13445 | 123 | 456 |   
789 | =23+456 | =987-654 |

”  
send\_data mytext, :type =\> ‘application/vnd.ms-excel’, :disposition  
=\> ‘inline’  
end
## end

But how would I get the text string data created from a .rhtml template  
or partial to the send\_data command. Basically I want a single template  
that will look more or less the same in the browser as in the excel?

I will be creating a long list of outstanding orders in the browser that  
the user may want to view in Excel

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [May 19, 2006, 4:49am UTC](https://www.ruby-forum.com/t/server-out-to-excel/60896/5 "2006-05-19T04:49:34Z")

</div>

Well… If you want to simply rerender the mylist view and send it you  
can do this.

def doexcel  
mytext = render :action =\> mylist  
send\_data mytext, :type =\> ‘application/vnd.ms-excel’, :disposition  
=\> ‘inline’  
end

On 5/19/06, Glenn C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class ExcelController \< ApplicationController
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [May 19, 2006, 2:22am UTC](https://www.ruby-forum.com/t/server-out-to-excel/60896/6 "2006-05-19T02:22:26Z")

</div>

you would put send\_data in a seperate controller, then the “View in  
Excel” button would link to that controller.  
-N

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [May 19, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/server-out-to-excel/60896/7 "2006-05-19T14:50:34Z")

</div>

Remember. Everything is an object.  
This was the single thing I had the biggest problems with when I  
stopped thinking in PHP and started thinking in Ruby. There is no  
difference between the class render and the class integer or the class  
string.

On 5/19/06, Glenn C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [May 19, 2006, 6:45am UTC](https://www.ruby-forum.com/t/server-out-to-excel/60896/8 "2006-05-19T06:45:28Z")

</div>

JÃ³n BorgÃ¾Ã³rsson wrote:

> Well… If you want to simply rerender the mylist view and send it you  
> can do this.
> 
> def doexcel  
> mytext = render :action =\> mylist  
> send\_data mytext, :type =\> ‘application/vnd.ms-excel’, :disposition  
> =\> ‘inline’  
> end
> 
> On 5/19/06, Glenn C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > class ExcelController \< ApplicationController
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –

Genius, Solved!!! …it is working well.

My brain just seems to have a big block in “thinking ROR”, I have no  
idea why it works! I would have guessed that the render action calls the  
view processes that renders out the text stream to the to webserver  
after which processing is resumed in the controller.

I would have guessed that mytext = render :action =\> mylist would set  
the value of mylist to the return code (success or failure) of the  
render action, rather than the text stream of the render action.
