how to create spreadsheet using array in ruby on rails

article is about creating basic Spreadsheet using array content.

sample_array = [['A1', 1, 'A2', 1.20], ['B1', 5, 'B2', 2.5], ['C1', 10, 'C2', 1.50]]

file_path = "#{FILEPATH}/target_file.xls" #FILEPATH => complete file path, target_file.xls => filename of your wish
workbook = Spreadsheet::Workbook.new

worksheet = workbook.create_worksheet
format_text = Spreadsheet::Format.new :horizontal_align => :centre
format_text.font = Spreadsheet::Font.new('Verdana')
format_header = Spreadsheet::Format.new :pattern => 1, :pattern_fg_color => :yellow, :horizontal_align => :centre
format_header.font = Spreadsheet::Font.new('Verdana', :bold => true, :color => 'blue')
format_heading = Spreadsheet::Format.new
format_heading.font = Spreadsheet::Font.new('Verdana', :bold => true, :color => 'blue')
row = 2

heading = ['first_col', 'second_col', 'third_col', 'fourth_col'] #these are heading for your Spreadsheet
heading.each_with_index do |i, j|
  worksheet.row(row).insert(j, heading[j]).set_format(j, format_header)
end

row = 3

sample_array.each do |sample|
  worksheet.row(row).insert(0, (sample[0] rescue '')).set_format(0, format_text)
  worksheet.row(row).insert(1, (sample[1] rescue '')).set_format(1, format_text)
  worksheet.row(row).insert(2, (sample[2] rescue '')).set_format(2, format_text)
  worksheet.row(row).insert(3, (sample[3] rescue '')).set_format(2, format_text)
  row = row + 1
end
workbook.write file_path #file_path => write file to this path