April 20, 2011

Fetching data from Excel using ruby win32ole gem

This code helps you  to access XLS file with ruby
Before that u need to install win32ole gem:
 For that use command prompt
gem update --system 1.6.2
gem install win32ole-pp


SCRIPT:

require "rubygems"
require "win32ole"
excel = WIN32OLE.new('excel.application')
excel.visible = true
work=excel.workbooks.open("C:\\Documents and Settings\\jagan\\Desktop\\New Microsoft Office Excel Worksheet.xls")
excel.range('A1').value = "name"
 excel.range('C1').value = "city"
excel.range("D1").value = "state"
for i in 1..4
excel.range("E#{i}").value = "zip#{i}"
end
excel.Range('F1:I1').value = ['North','South','East','West'];
work.save
--------------------------------------------------------------------------

require 'win32ole'
application = WIN32OLE.new('Excel.Application')
application.visible = TRUE
workbook = application.Workbooks.Add();
#~ The below line helps to print the content to excel
worksheet = workbook.Worksheets(2);
worksheet.Range('A1:D1').value = ['North','South','East','West'];
puts"hai"

#~ This line help to print the value from excel
a= worksheet.Range("A1")['Value']
puts a
workbook.Close
-------------------
This helps you to edit the work sheet name
ws.Name = 'Sample Worksheet'
-------------------------
# Close the workbook

wb.Close
-------------------------
# Quit Excel
xl.Quit


No comments:

Post a Comment