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.
Glenn C. (Guest)
on 2006-05-18 13: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
Tom W. (Guest)
on 2006-05-18 13:31
(Received via mailing list)
On 5/18/06, Glenn C. <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
Glenn C. (Guest)
on 2006-05-19 03:40
Tom W. wrote:
> On 5/18/06, Glenn C. <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"
unknown (Guest)
on 2006-05-19 04: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
Glenn C. (Guest)
on 2006-05-19 06: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
Jón B. (Guest)
on 2006-05-19 06: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 C. <removed_email_address@domain.invalid> wrote:
> class ExcelController < ApplicationController
>
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Glenn C. (Guest)
on 2006-05-19 08: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 C. <removed_email_address@domain.invalid> wrote:
>> class ExcelController < ApplicationController
>>
>> Rails mailing list
>> removed_email_address@domain.invalid
>> 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.
Jón B. (Guest)
on 2006-05-19 16: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 C. <removed_email_address@domain.invalid> wrote:
> >
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


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