Skip to content
apux edited this page Jul 30, 2012 · 3 revisions

The options passed to the to_xls method can include formats and widths column. For example:

column_format = {
  :created_at => {:number_format => 'DD/MM/YYYY'},
  :updated_at => {:number_format => 'MM/DD/YYYY'}
}
column_width = {:created_at => 12}
User.all.to_xls :column_format => column_format, :column_width => column_width

If two or more columns have the same formats, it's possible to use an array as key. Example:

column_format = {[:created_at, :update_at] => {:number_format => 'DD/MM/YYYY'}}
column_width = {[:created_at, :updated_at] => 12}
User.all.to_xls :column_format => column_format, :column_width => column_width

If a width or format should be applied to all columns, it's possible to specify an :all param

column_format = {:all => {:number_format => 'DD/MM/YYYY'}}
column_width = {:all => 12}

It's also possible to change the value of a specific column even if the :all params is indicated. This will use 'DD/MM/YYY' as number format, except for :total, which will use '0.00'

column_format = {:all => {:number_format => 'DD/MM/YYYY'}, :total => '0.00' }

column_format or cell_format

cell_format is a high priority param, therefore, all formats specified in column_format will be ignored if a cell_format is specified. Example:

cell_format => {:color => :blue}
header_format => {:weight => :bold, :color => :red}
column_format = {[:created_at, :update_at] => {:number_format => 'DD/MM/YYYY'}}
column_width = {[:created_at, :updated_at] => 12}
User.all.to_xls :cell_format => cell_format, :header_format => header_format, :column_format => column_format, :column_width => column_width

This will ignore column_format formats. However, it can be specified mergin the cell_format into column_format with the :all param. Example:

header_format => {:weight => :bold, :color => :red}
column_format = {[:created_at, :update_at] => {:number_format => 'DD/MM/YYYY'}, :all => {:color => 'blue'} }
column_width = {[:created_at, :updated_at] => 12}
User.all.to_xls :header_format => header_format, :column_format => column_format, :column_width => column_width
Clone this wiki locally