がんばれ

勉強した記録です。

RubyでExcel操作 少しまとめ

RubyExcel操作です。

また、調べることがあります。

 

# -*- Encoding:UTF-8 -*-
require 'win32ole'

class Excel
end

excel = WIN32OLE.new('Excel.Application')
WIN32OLE.const_load(excel, Excel)

#Excelファイルをダイアログで開く場合
#book_file = excel.GetOpenFilename
#book = excel.WorkBooks.Open(book_file)

#ファイル名指定で開く場合1(COM使用)
#fso = WIN32OLE.new('Scripting.FileSystemObject')
#filename = fso.GetAbsolutePathName("test.xlsx")
#book = excel.WorkBooks.Open(filename)

#ファイル名指定で開く場合2
#book = excel.workbooks.open 'sample.xlsx'

# 新規ブックを作成
book = excel.workbooks.add

#画面に状況を表示させる。
excel.visible = true

#シートにワークシートの1を指定
sheet = book.Worksheets(1)

#セルの値を取得(縦、横)
#s = sheet.Cells.Item(3,3).value
#セルに値を格納(縦、横)
sheet.Cells.Item(3,3).value = 1000
sheet.Cells.Item(2,2).value = "画像一覧"

#test
sheet.Cells.Item(2,2).Font.Size = 20
sheet.Cells.Font.Name = 'MS ゴシック'.tosjis
#sheet.Cells.Item(2, 2).Font.Bold = true    #失敗
#スタイル


#折り返して表示

#列の幅


#罫線
range = sheet.range("F11:G16")
#色
#range.Interior.ColorIndex = 6
#range.Interior.Pattern = Excel::XlSolid
#罫線
#線の種類
range.borders.lineStyle = Excel::XlContinuous
#線の太さ
range.borders.weight = Excel::XlThin
#名前を付けて保存する
#book.saveAs 'e:\test.xlsx'

#Excelブックを閉じる
#true:上書き false:上書きしない
#book.close(true)

#Excel終了
#excel.quit