Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating excel in rails

I am trying to create an excel sheet in ruby on rails. So I used the plugin Rexcel. When I am running the application I am getting the following error.

uninitialized constant Rexcel::Workbook::Builder

I had added the following code, then this error hitting

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?

like image 224
Aadi Avatar asked May 22 '26 12:05

Aadi


1 Answers

I would advice to use Spreadsheet instead of Rexcel because is definitely more mature and I'm using it with Rails 3 with no problems.

Here is the documentation.

The overall process would be:

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'
like image 145
tommasop Avatar answered May 25 '26 03:05

tommasop