Created
March 9, 2014 07:35
-
-
Save komasaru/9444127 to your computer and use it in GitHub Desktop.
Ruby script to read a Excel workbook by 'spreadsheet'.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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