Sha256: eea1b30575cdc3fc02e606220ff40833a4ae926ce4d3abac24557be7c80d73b8

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

module MakeExportable #:nodoc:
  class Excel < ExportableFormat #:nodoc:

    self.reference = :xls
    self.name      = 'Excel'
    self.register_format
    
    attr_accessor :data_set, :data_headers
    
    def initialize(data_set, data_headers=[])
      self.long      = 'Microsoft Excel'
      self.mime_type = 'application/vnd.ms-excel; charset=utf-8;'
      self.data_set = data_set
      self.data_headers = data_headers
    end

    def generate
      generate_header_option(data_headers)
      output = "<table>\n"
      unless data_headers.blank?
        output << "\t<tr>\n"
        output << data_headers.map {|h| "\t\t<th>#{sanitize(h.humanize.titleize)}</th>\n" }.join
        output << "\t</tr>\n"
      end
      data_set.each do |row|
        output << "\t<tr>\n"
        output << row.map {|field| "\t\t<td>#{sanitize(field)}</td>\n"}.join
        output << "\t</tr>\n"
      end
      output << "</table>\n"
      return output
    end

    def sanitize(value)
      value.gsub(/</, '&lt;').gsub(/>/, '&gt;')
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
make_exportable-1.1.0 lib/make_exportable/exportable_formats/excel.rb
make_exportable-1.0.3 lib/make_exportable/exportable_formats/excel.rb
make_exportable-1.0.2 lib/make_exportable/exportable_formats/excel.rb
make_exportable-1.0.1 lib/make_exportable/exportable_formats/excel.rb