Ho installato con successo axlsx gem da https://github.com/randym/axlsx Ecco il mio codice controller che ho usato per creare un file excel attraverso questa gemma.Rails - Esportare i record in file excel scaricabili utilizzando axlsx gem (Keep MVC)
Ma nulla accade con questo codice invece mi mostra un mimo non inizializzato errore
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
end
In secondo luogo, io cerco con questo codice:
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
Ma, purtroppo, non funziona. Qualcuno può dirmi solo suggerimento non una soluzione per svolgere il mio compito?
ho cercato volte terzi, come da suggerimento:
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.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
Mi gettato http 406 errore
Grazie Ammy, funziona bene. –
Questa è la migliore opzione per esportare in Excel, mantiene MVC –
Tempo mimo salvato così tanto. –