# Spreadsheet -avoid reading and writing from file how?

**URL:** <https://www.ruby-forum.com/t/spreadsheet-avoid-reading-and-writing-from-file-how/165560>\
**Category:** Rails\
**Created:** [April 15, 2009, 10:12am UTC](https://www.ruby-forum.com/t/spreadsheet-avoid-reading-and-writing-from-file-how/165560 "2009-04-15T10:12:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [April 15, 2009, 10:12am UTC](https://www.ruby-forum.com/t/spreadsheet-avoid-reading-and-writing-from-file-how/165560/1 "2009-04-15T10:12:24Z")

</div>

Hi  
I am using [http://spreadsheet.rubyforge.org/](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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [April 16, 2009, 4:38am UTC](https://www.ruby-forum.com/t/spreadsheet-avoid-reading-and-writing-from-file-how/165560/2 "2009-04-16T04:38:07Z")

</div>

Sijo Kg wrote:

> Hi  
> I am using [http://spreadsheet.rubyforge.org/](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.

> **[ActionController::Streaming](https://api.rubyonrails.org/classes/ActionController/Streaming.html#M000402)**
>
> Allows views to be streamed back to the client as they are rendered. By default, Rails renders views by first rendering the template and then the layout.

Michael G.
