0

I need to read an Excel sheet(.xls) values with Ruby using query. Is there any gems available in ruby to do this? If so please help me on this.

Any tips or advice on this would be great.

Thanks Anto

user28
  • 249
  • 2
  • 7
  • 20
  • 1
    A google search for 'ruby gem to read excel files' turned up http://stackoverflow.com/questions/3309511/how-do-i-read-the-content-of-an-excel-spreadsheet-using-ruby as its first result. – mcfinnigan Mar 25 '13 at 11:05
  • I want to implement queries to get values from excel sheet. IS it possible with spreadsheet gem? – user28 Mar 25 '13 at 11:10

2 Answers2

1

You can use Sequel and OLEDB to read Excel Files:

require 'sequel'
Encoding.default_external = 'utf-8' #needed for umlauts in excel

def read_excel(source)
  source = File.expand_path(source) #Full path needed

  db = Sequel.ado(:conn_string=>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=#{source};Extended Properties=Excel 8.0;")
  # Excel 2000 (for table names, use a dollar after the sheet name, e.g. Sheet1$)
  p db.test_connection

  dataset = db[:'Tabelle1$']
  p dataset
  dataset.each{|row|
    puts row
  }
end #test_read

read_excel('my_spreadsheet.xls')

You should know the name of the tab (in my example it's Tabelle1)


The 'real' solution here is not Sequel, but the ADO-Interface. I'm not familiar with other ORM, so I may not really help you. But you may check for example active record.

There are hints, how to connect MS-Access or sqlserver via ADO, some use ActiveRecord. If you replace the connection string with the Excel-String in my Sequel example, then you may use other ORMs.

You may also try to read Excel-Data via an ODBC-connection.

knut
  • 27,320
  • 6
  • 84
  • 112
0

Read data from excel file using spreadsheet gem

require 'spreadsheet'
doc = Spreadsheet.open('simple.xls')
sheet = doc.worksheet(0)  # list number, first list is 0 and so on...

val = sheet2[r,c] # read particular cell from list 0, r for row, c for column 

Some information is there.
More information on the net, just use Google.

Yevgeniy Anfilofyev
  • 4,827
  • 25
  • 27