Skip to content

Instantly share code, notes, and snippets.

@syun77
Last active December 11, 2015 17:09
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 syun77/4632903 to your computer and use it in GitHub Desktop.
Save syun77/4632903 to your computer and use it in GitHub Desktop.
RubyでExcelにアクセス
#!ruby -Ks
#UTF-8の場合、 -Ku
require 'win32ole'
class Sheet
def initialize(sheet)
@sheet = sheet
end
def cells(row, col)
return @sheet.Cells(row, col).value
end
end
# Excelオブジェクト生成
xls = WIN32OLE.new('Excel.Application')
# 「\」区切りでないと読み込めない
path = "Excelへのファイルパス"
# Excelを開く
book = xls.Workbooks.Open(path)
# シートオブジェクトを取得
sheet = book.Worksheets.Item("シート名")
# Sheetインスタンスを生成
sht = Sheet.new(sheet)
# (row,col)=(1,2) を取得
# 数値だと小数となることに注意(整数の場合 .to_i する)
sht.cells(1, 2)
xls.Workbooks.Close
xls.Quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment