Rails - Export records to downloadable excel file using axlsx gem Keep MVC

0 votes

I have installed the axlsx gem successfully from https://github.com/randym/axlsx Here is my controller code that I used to create an excel file through this gem.

But nothing happen with this code instead it shows me an error uninitialized mime

class Coaches::PaymentsController < ApplicationController

  before_filter :authenticate_coach!

  # List all the payments
  def index
    if !params[:sort].blank?
      @payments = Payment.includes(:member).paginate(:page => params[:page], :order => sort_column + " " + sort_direction)
    else
      @payments = Payment.includes(:member).paginate(:page => params[:page], :order=>'id desc')
    end
    respond_to do |format|
      format.html
      # Change format to xlsx
      format.xlsx
      format.json { render json: @payments }
    end
  end

Can someone please help me with this?

Nov 19 in Others by Kithuzzz
• 20,660 points
60 views

1 answer to this question.

0 votes

Use  axlsx_rails Gem with the template. In my case, I used the below configuration to make it work. and also a link with the extension .xlsx to render it in xlsx format.

GEM FILE

gem 'axlsx', '~> 2.0'
gem "axlsx_rails"

controller file- payments_controller.rb

def download
    @payments = Payment.all
    respond_to do |format| 
       format.xlsx {render xlsx: 'download',filename: "payments.xlsx"}
    end
end

View file- download.xlsx.axlsx

wb = xlsx_package.workbook
wb.add_worksheet(name: "Payments") do |sheet|
    sheet.add_row ["ID", "Notes","Amount($)","Deposit Date"]
    @payments.each do |payment|
        sheet.add_row [payment.id, payment.notes,payment.amount,payment.date_deposite]
    end
end
answered Nov 19 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
1 answer

How to convert pdf file to excel file using python

Just specify your whole output path instead ...READ MORE

answered Nov 4 in Others by narikkadan
• 37,660 points
189 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Export DataTable to Excel File

Add Interop References. First we need to ...READ MORE

answered Jun 9 in JQuery by gaurav
• 22,040 points
112 views
0 votes
0 answers

Export SQL query data to Excel

My query returns a huge amount of ...READ MORE

Aug 18 in Database by Kithuzzz
• 20,660 points
50 views
0 votes
1 answer

Print chosen worksheets in excel files to pdf in python

In the simplest form: import win32com.client o = win32com.client.Dispatch("Excel.Application") o.Visible ...READ MORE

answered Sep 24 in Others by narikkadan
• 37,660 points
228 views
0 votes
1 answer

How to Freeze Top Row and Apply Filter in Excel Automation with C#

Try this: // Fix first row workSheet.Activate(); workSheet.Application.ActiveWindow.SplitRow = 1; workSheet.Application.ActiveWindow.FreezePanes ...READ MORE

answered Oct 22 in Others by narikkadan
• 37,660 points
108 views
0 votes
1 answer
0 votes
1 answer

How can I convert excel file to pdf using TCPDF?

PHPExcel can only read charts from Excel2007 ...READ MORE

answered Oct 21 in Others by narikkadan
• 37,660 points
133 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP