Drextor
Drextor

Reputation: 433

Create Excel file in Ruby on Rails with Axlsx

I try to create Excel files with the gem "axlsx". But i work for the first time with this gem and i try one Test.

I make one link_to to an controller action. There is no Error, but i didnt find a complete excel file.

Link_to :

<%= link_to "Test", { :controller => :orders, :action => :invoices_generate }, { class: "btn btn-primary btn-lg", style: "width: 100%;", format: 'xlsx'} %>

My Controller :

def invoices_generate      
  respond_to do |format|
    format.xlsx
  end 
end

invoices_generate.xlsx.axlsx :

if examples.include? :basic
  wb.add_worksheet(:name => "Basic Worksheet") do |sheet|
    sheet.add_row ["First Column", "Second", "Third"]
    sheet.add_row [1, 2, 3]
    sheet.add_row ['     preserving whitespace']
  end
end

Upvotes: 0

Views: 4869

Answers (1)

noel
noel

Reputation: 2155

Looks like you're using an example from the Axlsx example page. Drop the if statement. And use code similar to this: https://github.com/straydogstudio/axlsx_rails/blob/master/README.md#template

Basically you need to use the xlsx_package variable in your template, obtain the workbook, then get a sheet.

If you're not using axlsx_rails, add it to your gem file according to the same readme above.

Upvotes: 2

Related Questions