Forum: Ruby on Rails Server out to excel

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
753528fbc8b89d5f3bbcd5558c36d168?d=identicon&s=25 Glenn Cadman (Guest)
on 2006-05-18 11:13
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
2f46d76f0e5db4dc318b03be07ebaac4?d=identicon&s=25 Tom Ward (Guest)
on 2006-05-18 11:31
(Received via mailing list)
On 5/18/06, Glenn Cadman <glenn.cadman@gmail.com> 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
753528fbc8b89d5f3bbcd5558c36d168?d=identicon&s=25 Glenn Cadman (Guest)
on 2006-05-19 01:40
Tom Ward wrote:
> On 5/18/06, Glenn Cadman <glenn.cadman@gmail.com> 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"
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2006-05-19 02:22
(Received via mailing list)
you would put send_data in a seperate controller, then the "View in
Excel" button would link to that controller.
-N
753528fbc8b89d5f3bbcd5558c36d168?d=identicon&s=25 Glenn Cadman (glenn)
on 2006-05-19 04:22
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 = "<table><tr><td>13445<td>123<td> 456<td>
789<td>=23+456<td>=987-654</tr></table>"
    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
30ee518e6fdc5b07e060775b5a542bdb?d=identicon&s=25 Jón Borgþórsson (jongretar)
on 2006-05-19 04:49
(Received via mailing list)
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 Cadman <glenn.cadman@gmail.com> wrote:
> class ExcelController < ApplicationController
>
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
753528fbc8b89d5f3bbcd5558c36d168?d=identicon&s=25 Glenn Cadman (glenn)
on 2006-05-19 06:45
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 Cadman <glenn.cadman@gmail.com> wrote:
>> class ExcelController < ApplicationController
>>
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> 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.
30ee518e6fdc5b07e060775b5a542bdb?d=identicon&s=25 Jón Borgþórsson (jongretar)
on 2006-05-19 14:50
(Received via mailing list)
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 Cadman <glenn.cadman@gmail.com> wrote:
> >
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
This topic is locked and can not be replied to.