Forum: Ruby on Rails Spreadsheet -avoid reading and writing from file how?

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.
Sijo k. (Guest)
on 2009-04-15 12:12
Hi
   I am using http://spreadsheet.rubyforge.org/ in my application..Usage
like

book = Spreadsheet::Workbook.new
--------
book.write "#{RAILS_ROOT}/public/uploads/excel-file.xls"
render :file => "#{RAILS_ROOT}/public/uploads/excel-file.xls"
headers['Content-Type'] = "application/vnd.ms-excel"
headers['Content-Disposition'] = "attachment; filename=excel-file.xls"
headers['Cache-Control'] = ''

       In the above i am writing to a file in a location and then read.
My question is is there any method so that writing to a file can be
avoided ..Because if there are some 100 requests the file will be
overwritten(Am I right?) or if give seperate names for the files the
upload folder will grow..So is there alternative and i can read the same
content directly

Thanks in advance
Sijo
Michael G. (Guest)
on 2009-04-16 06:38
Sijo Kg wrote:
> Hi
>    I am using http://spreadsheet.rubyforge.org/ in my application..Usage
> like
>
> book = Spreadsheet::Workbook.new
> --------
> book.write "#{RAILS_ROOT}/public/uploads/excel-file.xls"
> render :file => "#{RAILS_ROOT}/public/uploads/excel-file.xls"
> headers['Content-Type'] = "application/vnd.ms-excel"
> headers['Content-Disposition'] = "attachment; filename=excel-file.xls"
> headers['Cache-Control'] = ''
>
>        In the above i am writing to a file in a location and then read.
> My question is is there any method so that writing to a file can be
> avoided ..Because if there are some 100 requests the file will be
> overwritten(Am I right?) or if give seperate names for the files the
> upload folder will grow..So is there alternative and i can read the same
> content directly
>

I didn't test this code, but looking at the api it looks like
Workbook#write will take an IO stream.

require 'stringio'
require 'spreadsheet'

book = Spreadsheet::Workbook.new
blob = StringIO.new("")
book.write blob

send_data blob

Be sure to checkout the link below on send_data, it will allow you to
transfer the stream of data without writing it to disk first.

http://api.rubyonrails.org/classes/ActionControlle...

Michael G.
This topic is locked and can not be replied to.