# ROR - Excel Export Example

**URL:** <https://www.ruby-forum.com/t/ror-excel-export-example/157637>\
**Category:** Rails\
**Created:** [January 14, 2009, 12:06am UTC](https://www.ruby-forum.com/t/ror-excel-export-example/157637 "2009-01-14T00:06:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://www.ruby-forum.com/u/steve)\
**Post date:** [January 14, 2009, 12:06am UTC](https://www.ruby-forum.com/t/ror-excel-export-example/157637/1 "2009-01-14T00:06:10Z")

</div>

If you are running on Windows you can use create and read speadsheets  
using the ‘win32ole’ require

## This sample should give you the basic idea

def export  
require ‘win32ole’

```
  @sort_by = 'last_name'
  current_user = 'steve'
  @records = Contact.find(
      :all,
      :conditions => ["created_by = ?",

```

self.current\_user.login.to\_s] #,  
#:order =\> @sort\_by  
)

```
# Creates OLE object to Excel
excel = WIN32OLE.new("excel.application")

# Create the chart
excel['Visible'] = TRUE;
excel.DisplayAlerts = false #Supress's Excel warnings

workbook = excel.Workbooks.Add();
excel.Range("a1")['Value'] = 'First Name';
excel.Range("b1")['Value'] = 'Last Name';
excel.Range("c1")['Value'] = 'Middle Name';
excel.Range("d1")['Value'] = 'Name';

# add extra stuff down here
line_count = 1

@records.each do | contact |
    line_count = line_count + 1
    excel.Range("a" + line_count.to_s.strip )['Value'] =

```

contact.first\_name.to\_s  
excel.Range(“b” + line\_count.to\_s.strip )[‘Value’] =  
contact.last\_name.to\_s  
excel.Range(“c” + line\_count.to\_s.strip )[‘Value’] =  
contact.middle\_name.to\_s  
excel.Range(“d” + line\_count.to\_s.strip )[‘Value’] =  
(contact.first\_name.to\_s + " " + contact.middle\_name.to\_s + " " +  
contact.last\_name.to\_s).strip

```
  end

# Here are some common file formats:
xlCSV=6
xlCSVMac=22
xlCSVMSDOS=24
xlCSVWindows=23
xlCurrentPlatformText=-4158
xlExcel9795=43
xlTextMSDOS=21
xlTextPrinter=36
xlTextWindows=20

workbook.SaveAs 'contacts_export.csv', xlCSV
excel.Quit
excel = nil #release the excel object
GC.start #call Garbage collector

```

end

---

<div class="post-metadata">

**Author:** ![steve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://www.ruby-forum.com/u/steve)\
**Post date:** [January 14, 2009, 2:34am UTC](https://www.ruby-forum.com/t/ror-excel-export-example/157637/2 "2009-01-14T02:34:52Z")

</div>

steve wrote:

> If you are running on Windows you can use create and read speadsheets  
> using the ‘win32ole’ require
> 
> ## This sample should give you the basic idea
> 
> def export  
> require ‘win32ole’
> 
> ```
> @sort_by = 'last_name'
> current_user = 'steve'
> @records = Contact.find(
> :all,
> :conditions => ["created_by = ?",
> 
> ```
> 
> self.current\_user.login.to\_s] #,  
> #:order =\> @sort\_by  
> )

As server side solution, it does not scale at all.

Regards,

# rp8

> **[Competo Solutions, Inc.](https://competo.com/)**
>
> Sharing our experience in ever changing tech landscapes
