Generation surpasses the rails

I am trying to create an excel sheet in ruby ​​on rails. Therefore, I used the Rexcel plugin. When I run the application, I get the following error.

uninitialized constant Rexcel :: Workbook :: Builder

I added the following code, then this error hit

workbook = Rexcel::Workbook.new

worksheet = workbook.add_worksheet("Customers")

worksheet.add_line("name","test")

headers['Content-Type'] = "application/vnd.ms-excel"

headers['Content-Disposition'] = 'attachment; filename="excel-export.xlsx"'
headers['Cache-Control'] = 'max-age=0'
headers['pragma']="public"
workbook.build

How to solve this?

+3
source share
2 answers

I would recommend using Spreadsheet instead of Rexcel, because it is definitely more mature, and I use it with Rails 3 without any problems.

Here is the documentation.

General process:

book = Spreadsheet::Workbook.new
sheet = book.create_worksheet :name => 'Customers'
sheet.row(0).concat %w{Name Country Acknowlegement}
book.write '/path/to/output/excel-file.xls'
+8
source

+1 to tommasop

. , , , xml, html json, . Heroku , .

book.write '/somepath..'

def xls
  .. your stuff .. 
  blob = StringIO.new("")
  book.write blob
  blob.string
end

do

send_data @customer.xls, :type => :xls, :filename => @customer.xls_file_name

mime . . , rails 2.3.10. , 3.

Mime::Type.register "application/vnd.ms-excel", :xls
+5

Source: https://habr.com/ru/post/1774244/


All Articles