Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created March 9, 2014 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/9444127 to your computer and use it in GitHub Desktop.
Save komasaru/9444127 to your computer and use it in GitHub Desktop.
Ruby script to read a Excel workbook by 'spreadsheet'.
require 'spreadsheet'
XLS = "./test.xls"
class ReadExcel
# 読み込み
def read
begin
# Excelファイルを読み込み
wb = Spreadsheet.open(XLS, 'r')
# シート名を指定してシートを取得
# (インデックスで指定することも可能)
ws = wb.worksheet("Test")
#ws = @wb.worksheet(0)
# 行の値を配列で取得
row = ws.row(3) # <= 4行目
# 全ての行を配列で取得
row = ws.rows
# 行・列を指定して取得
cell = ws[14, 4] # <= 15行, 5列目
rescue => e
puts "[#{e.class}] #{e.message}"
exit 1
end
end
end
ReadExcel.new.read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment