Creating excel file with ruby on rails
Nick Brutyn
brutyn_nick at hotmail.com
Sat Oct 8 04:16:50 PDT 2005
hey,
i want to create a excel file with ruby on rails and send that file to the
browser (excel file must be supporting all versions)
this is what i have so fare, i cant send it to the server, anyone any
sollutions
def export_excel
if @request.env['HTTP_USER_AGENT'] =~ /msie/i
@headers['Pragma'] = ''
@headers['Cache-Control'] = ''
else
@headers['Pragma'] = 'no-cache'
@headers['Cache-Control'] = 'no-cache, must-revalidate'
end
@employee_id = @params["employee_id"]
@geotag_id = @params["geotag_id"]
firm_id = @session[:user].id
corrected_server_date = (Time.now).strftime('%Y-%m-%d 00:00:00')
@time_entries = TimeEntry.find(:all, :conditions =>["time_entries.firm_id =
? AND employee_id like ? and geotag_id like ?", firm_id,
"%#{@employee_id}%", "%#{@geotag_id}%"], :order => "time_entries.start_time
DESC", :include=>[:employee,:geotag])
wb = Excel.new("test/timesheets.xls")
version = Excel::VERSION
# Preferred way to add a format
f1 = wb.add_format(:color=>"black",:bold=>1,:italic=>true)
f4 = Format.new(:num_format => "d mmm yyyy")
f5 = Format.new(:num_format => 0x0f)
wb.add_format(f4)
wb.add_format(f5)
ws1 = wb.add_worksheet("timesheets")
#headers
@header = ['Employee','Address','Zip','City','Duration','Start','Stop']
#headers afprinten op de 1ste rij
0.upto(@header.length - 1) do |i|
ws1.write(0,i, at header[i], f1)
end
rij = 1 #de gegevens worden getoond vanaf de 2de rij
#time entries afprinten
for time_entry in @time_entries
ws1.write(rij,0,time_entry.employee.last_name + " " +
time_entry.employee.first_name)
ws1.write(rij,1,time_entry.geotag.address1)
ws1.write(rij,2,time_entry.geotag.zip)
ws1.write(rij,3,time_entry.geotag.city)
if time_entry.stop_time.nil?
ws1.write(rij,4,"")
ws1.write(rij,5,time_entry.start_time.strftime("%d/%m/%Y %H:%M"))
ws1.write(rij,6,"")
else
ws1.write(rij,4,time_entry.duration)
ws1.write(rij,5,time_entry.start_time.strftime("%d/%m/%Y %H:%M"))
ws1.write(rij,6,time_entry.stop_time.strftime("%d/%m/%Y %H:%M"))
end
rij = rij + 1
end
ws1.format_column(0..1,20,f1)
ws1.format_column(2,5,f1)
ws1.format_column(3..4,10,f1)
ws1.format_column(5..6,15,f1)
wb.close
redirect_to :action=>"list_times"
end
More information about the cvs-all
mailing list