Parse Excel spreadsheet in Ruby

parse excel spreadsheet in Ruby

First, install this gem: Roo

1
gem install roo

Then, use classes/methods provided in Roo to access Excel data, e.g. there is my example to iterate Excel spreadsheet data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# gem install roo
require 'roo'
xsl = Roo::Spreadsheet.open('./test1.xlsx')
xsl.sheets.each do |sheet_name|
puts ''
puts sheet_name
puts '--------------'
sheet = xsl.sheet(sheet_name)
if !sheet.nil?
last_row = sheet.last_row
last_column = sheet.last_column
if !last_row.nil? and !last_column.nil?
for row in 1..last_row
for col in 1..last_column
v = sheet.cell(row, col)
if v.nil?
puts "NIL"
else
puts "["+row.to_s+","+col.to_s+"]: " + sheet.cell(row, col)
end
end
end
else
puts 'Seems no data in sheet: ' + sheet_name
end
end
end
Share