user1563849
user1563849

Reputation: 223

Exporting Excel file using spreadsheets gem

I am trying to export a .xls file from my rail application and can not seem to get it to work. I am new to ruby and rails... Here is a sample of my code...

def results
    book = Spreadsheet::Workbook.new
    sheet1 = book.create_worksheet do |row|
    row << [ 'name', 'address', 'date', 'amount' ]
    results.each do |r|
    row << [ r.name, r.address, r.date, r.amount ]
    send_data(sheet1, :type => "application/xls", :filename => "#{client.client_id} #{source.source_id} - search results - #{DateTime.now.strftime("%m.%d.%y")}.xls")
end

The output I am getting in the xls fils is ... #< Spreadsheet::Worksheet:0x5fb8fd8>

Anyone know what I am doing wrong?

Upvotes: 3

Views: 3499

Answers (1)

Anthony Alberto
Anthony Alberto

Reputation: 10405

Did you look the documentation here ? http://spreadsheet.rubyforge.org/GUIDE_txt.html

Sounds like you're using a mix of the syntax used for reading and writing at the same time.

You should do something like :

sheet1 = book.create_worksheet
sheet1.row(0) << [ 'name', 'address', 'date', 'amount' ]
results.each_with_index do |r, i|
  sheet1.row(i+1) << [ r.name, r.address, r.date, r.amount ]
end

I didn't try it, but it shoulnd't be far from what is needed :)

Upvotes: 4

Related Questions