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, 2022 in Others by Kithuzzz
• 38,000 points
2,190 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, 2022 by narikkadan
• 63,600 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, 2022 in Others by narikkadan
• 63,600 points
4,171 views
0 votes
1 answer
0 votes
1 answer

How to save a new sheet in an existing excel file, using Pandas?

import pandas as pd import numpy as np path ...READ MORE

answered Dec 10, 2022 in Others by narikkadan
• 63,600 points
7,523 views
0 votes
1 answer

Export DataTable to Excel File

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

answered Jun 9, 2022 in JQuery by gaurav
• 23,260 points
774 views
0 votes
0 answers

Export SQL query data to Excel

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

Aug 18, 2022 in Database by Kithuzzz
• 38,000 points
554 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, 2022 in Others by narikkadan
• 63,600 points
3,566 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, 2022 in Others by narikkadan
• 63,600 points
2,645 views
0 votes
1 answer

How can I convert excel to PDF by Libreoffice and keep all format from excel file?

"Times New Roman" typeface does not have ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
1,649 views
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, 2022 in Others by narikkadan
• 63,600 points
2,139 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