53
59

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Ruby]RubyでExcelをつかう[Rails]

Last updated at Posted at 2014-01-31

##Gemのインストール

spreadsheetというGemをつかうよ
GitHubはこちら

gem install spreadsheet

Railsの場合は

gem 'spreadsheet'

##エンコーディングの設定をする

excel.rb
Spreadsheet.client_encoding = "UTF-8"

##エクセルファイルを新規作成して保存

excel.rb
Spreadsheet.client_encoding = "UTF-8"

book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet
sheet1.name = "test"

# 処理書く

book.write "/path/excel.xls"

##エクセルファイルを開いて別名で保存
テンプレート作っておいてそれを編集するときはこれ

excel.rb
Spreadsheet.client_encoding = "UTF-8"

book = Spreadsheet.open "/path/original.xls"
sheet1 = book.worksheet 0

# 処理書く

# 別名で保存(同じ名前にすると開けなくなるので注意)
book.write "/path/update.xls"

##エクセルに書き込む

excel.rb
Spreadsheet.client_encoding = "UTF-8"

book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet
sheet1.name = "test"

# 0行0列目にhogeと書き込む
sheet1[0,0] = "hoge"

# 3行8列目にfugaと書き込む
sheet1[3,8] = "fuga"


book.write "/path/excel.xls"

他にもやりかたあるみたいなので、GitHubのガイドで確認してみてください

53
59
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53
59

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?