Reading data from excel

Hi,
I’m brand new to Ruby.
Can somebody share their expertise on how to open and read data from
excel using ruby?
Thanks in advance.

And also any suggestions on where/how to learn Ruby would be very
helpful and appreciated.

On 6/5/06, Parvinder G. [email protected] wrote:

Hi,
I’m brand new to Ruby.
Can somebody share their expertise on how to open and read data from
excel using ruby?
Thanks in advance.

A very rudimentary version that shows the logic:

require ‘win23ole’

    application = WIN32OLE.new('Excel.Application')
    worksheet

=application.Workbooks.Open(excelFileName).Worksheets(workSheetName)
worksheet.Activate

    contLoop = true           # Dummy counter for the loop

    while contLoop do
      colVal = worksheet.Cells(row, column).Value

      if (colVal) then
        # This field is not NIL, this means that this row contains a 

entry
# Get the SecurityObject corresponding to the cusip, and
get the coupon
do processing …
else
# This signifies the end - no cash column value, no short
name value.
# End the loop
contLoop = false
end
# go to the next Row
row += 1
end

    # we are done
    application.Workbooks.Close

And also any suggestions on where/how to learn Ruby would be very
helpful and appreciated.

Pickaxe is a great book. You can find an online version at
http://www.ruby-doc.org/docs/ProgrammingRuby/html/index.html

All the best,
-Madan.

On 6/5/06, Madan M. [email protected] wrote:

Oops, ‘win23ole’ should have read ‘win32ole’

-Madan.

Madan Thanks for your quick response.

I’m trying to just open the excel application using the code below:

require ‘win32ole’

excel = WIN32OLE.new(‘Excel.Application’)
excel[‘Visible’] = true;

Something happens but i excel doesn’t open up.
do i need to add something to the above code?
Thanks

Parvinder G. wrote:

Hi,
I’m brand new to Ruby.
Can somebody share their expertise on how to open and read data from
excel using ruby?
Thanks in advance.

And also any suggestions on where/how to learn Ruby would be very
helpful and appreciated.

In addition to using win32ole, also see the “parseexcel” package.

http://raa.ruby-lang.org/project/parseexcel/

Regards,

Dan

On 6/5/06, Parvinder G. [email protected] wrote:

Can somebody share their expertise on how to open and read data from
excel using ruby?

I would also recommend parseexcel. The maintainer is very friendly and
the
package works quite well.

require “parseexcel/parseexcel”
file_name = “/your/file/name/here.xls”
workbook = Spreadsheet::ParseExcel.parse(file_name)
worksheet = workbook.worksheet(0) # the first sheet in the book
worksheet.each { |row| puts row.at(0) } # print the first column of each
row

And also any suggestions on where/how to learn Ruby would be very

helpful and appreciated.

I would recommend “Why’s (Poignant) Guide to Ruby” if your tolerance for
the
absurd is high. (http://poignantguide.net/ruby/) Otherwise, I would
recommend “Learn to Program” as a place to start online. (
http://pine.fm/LearnToProgram/)

Good luck.

Sean C.
http://sean-carley.blogspot.com/

Madan M. wrote:

On 6/5/06, Parvinder G. [email protected] wrote:

Hi,
I’m brand new to Ruby.
Can somebody share their expertise on how to open and read data from
excel using ruby?
Thanks in advance.

A very rudimentary version that shows the logic:

require ‘win23ole’

    application = WIN32OLE.new('Excel.Application')
    worksheet

=application.Workbooks.Open(excelFileName).Worksheets(workSheetName)
worksheet.Activate

    contLoop = true           # Dummy counter for the loop

    while contLoop do
      colVal = worksheet.Cells(row, column).Value

      if (colVal) then
        # This field is not NIL, this means that this row contains a 

entry
# Get the SecurityObject corresponding to the cusip, and
get the coupon
do processing …
else
# This signifies the end - no cash column value, no short
name value.
# End the loop
contLoop = false
end
# go to the next Row
row += 1
end

    # we are done
    application.Workbooks.Close

And also any suggestions on where/how to learn Ruby would be very
helpful and appreciated.

Pickaxe is a great book. You can find an online version at
http://www.ruby-doc.org/docs/ProgrammingRuby/html/index.html

All the best,
-Madan.

Thanks everybody for your time and help.

Issue:
I’m getting an error “undefined local variable row” at line

      colVal = worksheet.Cells(row, column).Value

Thanks for your help,
-Parvinder

Try this link, it helped me greatly when I was trying to wrap my head
around
how to script excel

http://wiki.rubygarden.org/Ruby/page/show/ScriptingExcel

Parvinder G. wrote:

Madan Thanks for your quick response.

I’m trying to just open the excel application using the code below:

require ‘win32ole’

excel = WIN32OLE.new(‘Excel.Application’)
excel[‘Visible’] = true;

Something happens but i excel doesn’t open up.
do i need to add something to the above code?
Thanks

Parvinder-

Add a line of code to create a new workbook or open an existing one:

wb = excel.Workbooks.Add

OR

wb = excel.Workbooks.Open(filename)

Mully

I cannot read excel in require ‘fox16’. How can I do to run excel file?

Thanks Gary, it seems like a pretty good source of information.
Parvinder

On May 22, 6:06 pm, Cool W. [email protected] wrote:

I cannot read excel in require ‘fox16’. How can I do to run excel file?

Posted viahttp://www.ruby-forum.com/.

win32ole

Simplier approach, if possible, save the excel in csv format.
Then parse the csv using fastercsv (http://fastercsv.rubyforge.org/)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs