Last active
December 11, 2015 17:09
-
-
Save syun77/4632903 to your computer and use it in GitHub Desktop.
RubyでExcelにアクセス
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
#!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