Created
March 9, 2014 07:37
-
-
Save komasaru/9444141 to your computer and use it in GitHub Desktop.
Ruby script to write 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_2.xls" | |
class WriteExcel | |
# 書き込み | |
def write | |
begin | |
# ワークブック生成 | |
wb = Spreadsheet::Workbook.new | |
# ワークシート生成 | |
ws = wb.create_worksheet(:name => 'TEST') | |
# 行, 列を指定して書き込み | |
ws[2, 3] = "Hello!" # <= 3行,4列目 | |
# 配列でまとめて行を書き込む | |
ws.row(4).replace ["5行-1列", "5行-2列", "5行-3列"] # <= 5行目 | |
# 名前を付けてワークブック保存 | |
wb.write(XLS) | |
rescue => e | |
puts "[#{e.class}] #{e.message}" | |
exit 1 | |
end | |
end | |
end | |
WriteExcel.new.write |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool. Thanks. This was still really helpful, even 9 years later.