Difference between revisions of "Windows OLE Automation in Ruby"

From PeformIQ Upgrade
Jump to navigation Jump to search
(Created page with '=Examples= <pre> #!/usr/bin/env ruby require 'win32ole' # You can, of course, iterate over the Workbooks collection: # # for workbook in excel.Workbooks # # ...code....')
 
Line 47: Line 47:


[[Category:Ruby]]
[[Category:Ruby]]
[[Category:WinOLE]]
[[Category:OLE]]

Revision as of 13:39, 29 August 2009

Examples

#!/usr/bin/env ruby

require 'win32ole'

# You can, of course, iterate over the Workbooks collection:
# 
#    for workbook in excel.Workbooks
#       # ...code...
#    end

excel = WIN32OLE.new('Excel.Application')

excel.visible = TRUE

book = excel.Workbooks.Add();
# book = excel.ActiveWorkbook

sheet = book.Worksheets(1);

sheet.Range('A1:D1').value = ['North','South','East','West'];

book.SaveAs('C:\temp\Workbook.xlsx')

book.Close

book = excel.Workbooks.Open('C:\temp\Workbook.xlsx')

sheet = book.Worksheets(1);

sheet.setproperty('Cells', 3, 2, 10) # => The B1 cell value is 10.

book.Save

book.Close

excel.quit

# lose the reference so that excel can be garbage collected
excel = nil

# ...and collect it.
GC.start