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