电子表格 Rails 3 问题 [英] spreadsheet Rails 3 question

查看:34
本文介绍了电子表格 Rails 3 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 Rails 2.3.10 应用程序升级到 Rails 3.0.3.在我的应用程序中,我具有用户可以将数据下载到 excel 的功能.

I have upgraded my rails 2.3.10 application into Rails 3.0.3. In my application i have the feature like user can download the data into excel.

我的 gem 版本:电子表格-0.6.4.1

My gem version : spreadsheet-0.6.4.1

我在 Gemfile 和 Mime::Type.register_alias "application/excel" 中声明了 gem 版本,在 application.rb 中声明了 :xls.和我的excel生成代码如下

I have declared gem version in Gemfile and Mime::Type.register_alias "application/excel", :xls in application.rb. and my excel generationg code as follows

<%

book = Spreadsheet::Workbook.new
 data = book.create_worksheet :name => 'myname'
 data.row(0).concat %w{name email}
 header_format = Spreadsheet::Format.new :color => :green, :weight => :bold
 data.row(0).default_format = header_format

@names.results_data.each_with_index { |n, i|

data.row(i+1).push n.name,n.email
}

blob = StringIO.new('')
book.write(file_blob)
-%><%=blob.string%>

我的控制器代码是:

respond_to do |format|
      format.html
      format.rss
      format.xls {    
        view_output = render_to_string :action => "excel" << name
        send_data(view_output, :type=>"application/ms-excel", :filename => "name.xls")
      }

问题是当单击 excel 链接时,它打开了 excel 窗口,并在弹出窗口中显示name.xls[2] 无法访问.可能已损坏或只读......我已经改变了所有的可能性,比如 gem 升级、mime 类型的改变,但没有运气......

The problem is when click the excel link , it open up the excel window and its says in the popup 'name.xls[2] cann't be access. may be corrupted or read only ... i have changed all the possibilites like gem upgrade, mime-type change but no luck...

谁能说说错误是什么

推荐答案

两个想法:

  1. excel.xls.erb 不需要是 erb 文件.您可以轻松地将其放入当前控制器中的帮助程序或其他方法中.我知道 ERb 语义在 rails 3 中改变了一堆,所以这可能是导致您出错的原因.

  1. There's no need for excel.xls.erb to be an erb file. You can easily put that in either a helper or another method in the current controller. I know the ERb semantics changed a bunch in rails 3, so this could be the cause of your errors.

 def render_to_xls(names, name = 'myname')

  book = Spreadsheet::Workbook.new
  data = book.create_worksheet :name => name
  data.row(0).concat %w{name email}
  header_format = Spreadsheet::Format.new :color => :green, :weight => :bold
  data.row(0).default_format = header_format

  names.results_data.each_with_index { |n, i|

     data.row(i+1).push n.name,n.email
  }  
  blob = StringIO.new('')
  book.write(blob)
  blob
end

稍后,在您的响应者代码中:

And later, in your responder code:

    respond_to do |format|
      format.html
      format.rss
      format.xls {    
        send_data(render_to_xls(@names, name), :type=>"application/ms-excel", :filename => "name.xls")
      }

  • 尝试将以前的 excel 文件与当前的 excel 文件进行比较,看看是否有奇怪的空白差异或类似的东西.

  • Try doing a diff of a previous excel file against the current excel file and see if there are weird whitespace differences or anything like that.

    这篇关于电子表格 Rails 3 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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