Search code examples
ruby-on-railsaxlsxcaxlsx

caxlsx gem: generated excel file displays #<Axlsx::Worksheet:0x0000559d7e487dc8>


I have a simple example, that when Excel file is generated contains #<Axlsx::Worksheet:0x0000559d7e487dc8>. I would like the Excel file to contain "Keys Available to be Assigned And Their Rooms".

I am using Rails 5.2, caxlsx -v 3.1.0 and caxlsx_rails -v 0.6.2.

Rails controller:

def unassigned_door_keys_n_rooms
    @qry_rslts = Reports::UnassignedDoorKeysNRooms.qry_rslts
    @csv_rslts = Reports::UnassignedDoorKeysNRooms.export_as_csv

    respond_to do |format|
      format.xml { render xlsx: 'unassigned_door_keys_n_rooms', template: 'reports/unassigned_door_keys_n_rooms.xlsx.axlsx' }
      format.csv { send_data(@csv_rslts) }
    end
end

Rails view unassigned_door_keys_n_rooms.xlsx.axlsx:

require 'axlsx'

p = Axlsx::Package.new
wb = p.workbook

s = wb.styles
header = s.add_style bg_color: 'E6E6E6', i: true, sz: 9, alignment: { horizontal: :left }

wb.add_worksheet do |sheet|
  sheet.add_row ["Keys Available to be Assigned And Their Rooms"], style: header
end

Solution

  • In the template file you don't need to require the gem, create the package manually or serialize it in the end - caxlsx_rails does it for you automatically, and you should use the helper methods provided by the gem.

    See