ruby - Rails to excel with associations -
i'am new on rails , i've spent day looking solution export data excel file.
i've tryed to_xls, simple_xlxs , other gems, i've tryed render xml template , i've no success.
so have associations between 2 models:
call model:
class call < activerecord::base belongs_to :number has_many :results end and result model:
class result < activerecord::base belongs_to :call end so need genrate excel tables own headers in table calle'd want. , wan't in excel file columns associeted model
what can do? thanks
# install in gem file or in terminal test # gem install spreadsheet require 'spreadsheet' spreadsheet.client_encoding = 'utf-8' book = spreadsheet::workbook.new sheet1 = book.create_worksheet :name => 'test' money_format = spreadsheet::format.new :number_format => "#,##0.00 [$€-407]" date_format = spreadsheet::format.new :number_format => 'mm.dd.yyyy' # set default column formats sheet1.column(1).default_format = money_format sheet1.column(2).default_format = date_format # depending on data have create loop fits format of data sheet1.row(0).push "just text", 5.98, datetime.now book.write 'sample.xls' the above code example writes data columns create. can have info in csv style. if you're returning objects can values each object , join array ',' separation , loop through.
Comments
Post a Comment