Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created March 9, 2014 07:37
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 komasaru/9444141 to your computer and use it in GitHub Desktop.
Save komasaru/9444141 to your computer and use it in GitHub Desktop.
Ruby script to write a Excel workbook by 'spreadsheet'.
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
@cadamini
Copy link

Cool. Thanks. This was still really helpful, even 9 years later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment