hey, i want to generate an excel file and send it directly to the browser (without storing it to the harddisk). does anyone has any solution? thanks in advance this is my code: -------------------- 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 send_data gen_excel, :filename => "timesheets.xls", :type => "application/vnd.ms-excel" end def gen_excel 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) f2 = wb.add_format(:color=>"black") 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'',''Start date'',''Start time'',''Stop date'', ''Stop time'',''Duration''] #headers afprinten op de 1ste rij 0.upto(@header.length - 1) do |i| ws1.write(0,i,@header[i], f1) end wb.close end this is my error: --------------------- WARNING: You have a nil object when you probably didn''t expect it! Odds are you want an instance of Array instead. Look in the callstack to see where you''re working with an object that could be nil. Investigate your methods and make sure the object is what you expect! /app/controllers/timesheets_controller.rb:205:in `export_excel'' script/server:49 Show framework trace c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/streaming.rb:110:in `send_data'' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:756:in `send'' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:756:in `perform_action_without_filters'' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/filters.rb:295:in `perform_action_without_benchmark'' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in `perform_action_without_rescue'' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in `measure'' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in `perform_action_without_rescue'' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/rescue.rb:80:in `perform_action'' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:356:in `send'' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:356:in `process'' c:/ruby/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/dispatcher.rb:32:in `dispatch'' c:/ruby/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/webrick_server.rb:105:in `handle_dispatch'' c:/ruby/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/webrick_server.rb:71:in `service'' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'' c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread'' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start'' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread'' c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start'' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each'' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start'' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' c:/ruby/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/webrick_server.rb:57:in `dispatch''
Tom Mornini
2005-Oct-17 09:58 UTC
Re: How to send excel file to browser (spreadsheat 0.3.2)
On Oct 17, 2005, at 2:50 AM, Nick Brutyn wrote:> hey, i want to generate an excel file and send it directly to the > browser (without storing it to the harddisk). > > does anyone has any solution?> send_data gen_excel, :filename => "timesheets.xls", :type => > "application/vnd.ms-excel"Here''s a send_data call from my code, largely lifted from the Agile book. send_data( @photo.image, :filename => @photo.file_name, :type => @photo.content_type, :disposition => ''inline'') You haven''t specified :disposition, which in your case should be ''attachment'' I would think. -- -- Tom Mornini
Alex Young
2005-Oct-17 10:05 UTC
Re: How to send excel file to browser (spreadsheat 0.3.2)
Nick Brutyn wrote:> hey, i want to generate an excel file and send it directly to the > browser (without storing it to the harddisk). > > does anyone has any solution?Just a guess, but check what value wb.close returns - I don''t think it''s the workbook object, or the data. -- Alex
Joe Van Dyk
2005-Oct-17 10:21 UTC
Re: How to send excel file to browser (spreadsheat 0.3.2)
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Oct 17, 2005, at 2:50 AM, Nick Brutyn wrote:> hey, i want to generate an excel file and send it directly to the > browser (without storing it to the harddisk). > > does anyone has any solution? > > thanks in advance > > this is my code: > -------------------- > 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 > > send_data gen_excel, :filename => "timesheets.xls", :type => > "application/vnd.ms-excel" > end > > def gen_excel > 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) > f2 = wb.add_format(:color=>"black") > > 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'',''Start > date'',''Start time'',''Stop date'', ''Stop time'',''Duration''] > #headers afprinten op de 1ste rij > 0.upto(@header.length - 1) do |i| > ws1.write(0,i,@header[i], f1) > end > > wb.close > end > > this is my error: > --------------------- > WARNING: You have a nil object when you probably didn''t expect > it! Odds are you > want an instance of Array instead. > > Look in the callstack to see where you''re working with an object > that could be nil. > Investigate your methods and make sure the object is what you expect! > > /app/controllers/timesheets_controller.rb:205:in `export_excel''Which one is line 205? Somewhere on that line, or in a function that''s being called by that line, you are calling a method on a nil object. Joe Van Dyk http://www.pinkpucker.net -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (Darwin) iD8DBQFDU3sdsWh6/7z1gt4RAsCzAJ9IqUdVlhNnyfeHTS6iIpd2VegQswCg5Ka9 3PxynLmhQO5itSk3US7ogSg=ndyL -----END PGP SIGNATURE-----
Brutyn Nick
2005-Oct-17 11:12 UTC
Re: How to send excel file to browser (spreadsheat 0.3.2)
> Which one is line 205? Somewhere on that line, or in a function > that''s being called by that line, you are calling a method on a nil > object.line 205 is send_data gen_excel, :filename => "timesheets.xls", :type => "application/vnd.ms-excel"