title: Rails - Export records to downloadable excel file using axlsx gem (Keep MVC)
body: <p>I have installed the axlsx gem successfully from <a href="https://github.com/randym/axlsx" rel="noreferrer">https://github.com/randym/axlsx</a>
  Here is my controller code that I used to create an excel file through this gem.</p>
  
  <p>But nothing happen with this code instead it shows me an error uninitialized mime </p>
  
  <pre><code>class Coaches::PaymentsController &lt; ApplicationController
  
    before_filter :authenticate_coach!
  
    # List all the payments
    def index
      if !params[:sort].blank?
        @payments = Payment.includes(:member).paginate(:page =&gt; params[:page], :order =&gt; sort_column + " " + sort_direction)
      else
        @payments = Payment.includes(:member).paginate(:page =&gt; params[:page], :order=&gt;'id desc')
      end
      respond_to do |format|
        format.html
        # Change format to xlsx
        format.xlsx
        format.json { render json: @payments }
      end
    end
  end
  </code></pre>
  
  <p>Secondly,I try with this code:</p>
  
  <pre><code>wb = xlsx_package.workbook
  wb.add_worksheet(name: "Buttons") do |sheet|
    @buttons.each do |button|
      sheet.add_row [button.name, button.category, button.price]
    end
  end
  </code></pre>
  
  <p>But unfortunately, it does not work. Can anyone tell me only hint not a solution to do my task?</p>
  
  <p>I have tried third times as per suggestion:</p>
  
  <pre><code>   def index
      if !params[:sort].blank?
        @payments = Payment.includes(:member).paginate(:page =&gt; params[:page], :order =&gt; sort_column + " " + sort_direction)
      else
        @payments = Payment.includes(:member).paginate(:page =&gt; params[:page], :order=&gt;'id desc')
      end
      respond_to do |format|
        format.xlsx do
          p = Axlsx::Package.new
          wb = p.workbook
          wb.add_worksheet(name: "Your worksheet name") do |sheet|
            sheet.add_row ["First Column", "Second", "Third"]
            sheet.add_row [1, 2, 3]
            sheet.add_row [' preserving whitespace']
          end
          send_data p.to_stream.read, type: "application/xlsx", filename: "filename.xlsx"
        end
      end
    end
  </code></pre>
  
  <p>It thrown me http 406 error</p>