Module: ExcelHelper

Defined in:
lib/helpers/jruby/jexcel_file.rb

Instance Method Summary collapse

Instance Method Details

#to_xls(items = []) ⇒ Object

ActiveRecord Helper - Export model data to XLS file format



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/helpers/jruby/jexcel_file.rb', line 142

def to_xls(items=[])

  @excel = ExcelFile.new(items[0].class.name)

  @excel.create_row(0)
  
  sheet = @excel.sheet

  # header row
  if !items.empty?
    row = sheet.createRow(0)
    cell_index = 0
    items[0].class.columns.each do |column|
      row.createCell(cell_index).setCellValue(column.name)
      cell_index += 1
    end
  end

  # value rows
  row_index = 1
  items.each do |item|
    row = sheet.createRow(row_index);

    cell_index = 0
    item.class.columns.each do |column|
      cell = row.createCell(cell_index)
      if column.sql_type =~ /date/ then
        millis = item.send(column.name).to_f * 1000
        cell.setCellValue(Date.new(millis))
        cell.setCellStyle(dateStyle);
      elsif column.sql_type =~ /int/ then
        cell.setCellValue(item.send(column.name).to_i)
      else
        value = item.send(column.name)
        cell.setCellValue(item.send(column.name)) unless value.nil?
      end
      cell_index += 1
    end
    row_index += 1
  end
  @excel.to_s
end