Skip to content

Instantly share code, notes, and snippets.

@Fernan2
Created May 24, 2012 11:09
Show Gist options
  • Save Fernan2/2780852 to your computer and use it in GitHub Desktop.
Save Fernan2/2780852 to your computer and use it in GitHub Desktop.
Creación de Excels desde Rails
require 'spreadsheet'
include Spreadsheet
format.xls do
# ------ El bloque XLS debe ir sin paginación y con producto ------ #
fichero = "vila_#{@modo}_#{Time.now.to_f.to_s}.xls"
ruta = "#{Rails.root}/tmp/#{fichero}"
Spreadsheet.client_encoding = "UTF-8"
workbook = Spreadsheet::Workbook.new
hoja_vila = workbook.create_worksheet(name: "Vila en Verema")
cabecera = %w(Nombre Vila Verema)
for columna in cabecera
hoja_vila[0, cabecera.index(columna)] = columna
end
@vilaimportables = @vilaimportables.includes(:producto)
fila = 1
for vino in @vilaimportables
hoja_vila[fila, 0] = vino.nombre
hoja_vila[fila, 1] = vino.url_vila
hoja_vila[fila, 2] = "#{HTTP_DOMINIO}#{vino.producto_link}" if !vino.producto_link.blank?
fila += 1
end
workbook.write ruta
send_file ruta, stream: false
File.delete(ruta)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment