2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

エクセルファイルへの書き込み(Ruby)

Posted at

rubyXLクラスを使ってエクセルファイルへの書き込み処理を実装しました

require 'rubyXL'

array1 = ["白石麻衣","秋元真夏","遠藤さくら","山下美月","吉田綾乃クリスティー"]
m_array = []
m_array = array1.shuffle!
array2 = ["好きなコスメ","好きなお笑い芸人","好きな落語家","好きな駅","好きなファッションブランド"]
t_array = []
t_array = array2.shuffle!
out_file = "result1.xlsx"
# rubyXLクラスを読み込み、エクセルファイルへ出力するインスタンスを呼び出す
xlsx = RubyXL::Workbook.new
# ワークシートの定義
worksheet = xlsx[0]
n=array1.length
srow = 2
erow = srow + n
nowrow = srow


# 配列の要素を取り出し、エクセルファイルへ書き込む
for i in 0...n do
    name = m_array[i]
    title = t_array[i]
    worksheet.add_cell(nowrow,1,name)
    worksheet.add_cell(nowrow,2,title)
    nowrow += 1
end

# エクセルファイルを保存する
begin
  xlsx.write(out_file)
rescue
  puts "ファイルの保存に失敗しました"
end
2
5
2

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
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?