Difficulty with send_data in Ruby on Rails combined with a spreadsheet plugin

I have a function in the controller that accepts some specifications and generates a report about them. This user_report function is called in the view:

<% = submit_to_remote 'submit-button', "Export Report to Excel" ,: url => {: controller =>: reports ,: action =>: user_report ,: print_state => 'print'}%>

In reports_controller, I use the Spreadsheet plugin to create an Excel file in the user_report function. I want to use send_data to stream a file to a user without creating it on the server. The research I did shows that using StringIO is the way as shown below. Disappointingly, nothing happens when I call send_data. The plugin seems to work well by creating a file and storing it on the server, but does nothing when I use send_file, assuming the problem is not in the plugin. But what am I doing wrong with send_file / send_data?

The function itself is as follows:

def user_report

if request.post? unless params[:reports][:userid].blank? @userid=params[:reports][:userid] end if params[:print_state]=='print' report = Spreadsheet::Workbook.new info = report.create_worksheet :name => 'User Information' info.row(1).push 'User ID', @userid @outfile = "Report_for_#{@userid}.xls" require 'stringio' data = StringIO.new '' report.write data send_data data.string, :type=>"application/excel", :disposition=>'attachment', :filename => @outfile end respond_to do |format| format.js { } end end 

end

The log file is being read 2010-10-18 14:13:59 INFO - Sending data Report_for_jjohnson.xls but the download does not start in the browser. I will be able to use send_data in this application before, which is confusing.

I use Rails v2.3, Ruby v1.8.7 and Spreadsheet v6.4.1 on the spreadsheet.rubyforge.org page.

+7
ruby-on-rails excel spreadsheet xml-spreadsheet
source share
2 answers

Just change the line:

 send_data data.string, :type=>"application/excel", :disposition=>'attachment', :filename => @outfile 

in

 send_data data.string.bytes.to_a.pack("C*"), :type=>"application/excel", :disposition=>'attachment', :filename => @outfile 
+6
source share

Even if I don't want to write and delete, but with a spreadsheet seems like the only solution.


  # write the file book.write "Employee_History_#{ params[:id]}.xls" # send the file send_file "Employee_History_#{ params[:id]}.xls", :type => "application/vnd.ms-excel", :filename => "data.xls", :stream => false # and then delete the file File.delete("Employee_History_#{ params[:id]}.xls") 
0
source share

All Articles