Forum: Ruby on Rails ROR - Excel Export Example

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.
steve (Guest)
on 2009-01-14 01:06
(Received via mailing list)
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
rp8 -. (Guest)
on 2009-01-14 03:34
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
=======================
http://competo.com
This topic is locked and can not be replied to.