Ruby on Rails中的send_data与Spreadsheet插件相结合很困难 [英] Difficulty with send_data in Ruby on Rails in conjunction with Spreadsheet plug-in

查看:172
本文介绍了Ruby on Rails中的send_data与Spreadsheet插件相结合很困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中有一个功能,它接收一些规范并生成一个报告。此函数user_report在视图中调用:

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

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

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

在reports_controller中,我使用电子表格插件在user_report函数中生成一个Excel文件。我想使用send_data将文件流式传输到用户,而不是首先在服务器上创建它。我所做的研究表明,使用StringIO是可以走的,如下图所示。令人沮丧的是,当我打电话给send_data时,什么也没有。该插件似乎很好地创建一个文件并将其保存在服务器上,但是当我使用send_file时不会做任何事情,这表明问题不在于插件。但是,我在send_file / send_data中做错了什么?

In reports_controller I use the Spreadsheet plugin to generate an Excel file within the user_report function. I want to use send_data to stream the file to the user without creating it on the server first. The research I've done shows that using StringIO is the way to go, as shown below. Frustratingly, nothing happens when I call send_data. The plugin seems to work well creating a file and saving it on the server, but does nothing when I use send_file, suggesting that the problem doesn't lie in the plugin. But then what am I doing wrong with send_file/send_data?

函数本身如下所示:


def user_report

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

日志文件读取
2010-10-18 14:13:59 INFO - 发送数据Report_for_jjohnson.xls
,但没有下载开始于浏览器。我以前在这个应用程序中使用send_data成功,这是令人困惑的。

The log file reads 2010-10-18 14:13:59 INFO -- Sending data Report_for_jjohnson.xls but no download begins in-browser. I've succeed in using send_data on this app before, which is confusing.

我使用的是Rails v2.3,Ruby v1.8.7和Spreadsheet v6.4.1在spreadsheet.rubyforge.org。

I'm using Rails v2.3, Ruby v1.8.7, and Spreadsheet v6.4.1 at spreadsheet.rubyforge.org.

推荐答案

只需更改行:

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

to:

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

这篇关于Ruby on Rails中的send_data与Spreadsheet插件相结合很困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆