使用电子表格gem导出Excel文件

时间:2021-06-12 09:55:55

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...

我正在尝试从我的rails应用程序导出一个.xls文件,但似乎无法使它正常工作。我是ruby和rails的新手……这是我的代码示例……

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>

我在xls文件中获得的输出是……# < Spreadsheet::工作表:0 x5fb8fd8 >

Anyone know what I am doing wrong?

有人知道我做错了什么吗?

1 个解决方案

#1


4  

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

你看了文件了吗?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 :)

我没有试过,但它应该离我们所需要的不远。

#1


4  

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

你看了文件了吗?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 :)

我没有试过,但它应该离我们所需要的不远。