Ruby Spreadsheet:错误的文件描述符 - test.xls(Errno :: EBADF)

时间:2012-11-08 19:30:23

标签: ruby spreadsheet

我的脚本有问题,它生成简单的.xls文件并将数据写入一个单元格。这是一个简单的代码:

require 'spreadsheet'

class Filter
  def filter
    @excel = Spreadsheet::Workbook.new
    @sheet = @excel.create_worksheet

    @sheet[0, 0] = "test"
        @excel.write 'test.xls'
  end
end

f = Filter.new
f.filter

但它引发了错误:

  

C:/Ruby193/lib/ruby/gems/1.9.1/gems/ruby-ole-1.2.11.5/lib/ole/storage/base.rb:62:在   write_nonblock': Bad file descriptor - test.xls (Errno::EBADF) from C:/Ruby193/lib/ruby/gems/1.9.1/gems/ruby-ole-1.2.11.5/lib/ole/storage/base.rb:62:in 初始化'

    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/ruby-ole-1.2.11.5/lib/ole/storage/base.rb:78:in
     

new' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/ruby-ole-1.2.11.5/lib/ole/storage/base.rb:78:in 开放”           来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/excel/writer/workbook.rb:4   53:在write_from_scratch' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/excel/writer/workbook.rb:6 31:in write_workbook'中           来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/writer.rb:15:in   block in write' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/writer.rb:14:in 开放”           来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/writer.rb:14:in   write' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.7.4/lib/spreadsheet/workbook.rb:116:in 写“

    from filter.rb:10:in `filter'
    from filter.rb:15:in `<main>'

2 个答案:

答案 0 :(得分:3)

因为ruby-ole 1.2.11.5不支持windows平台, 更多细节:ruby-ole issue

你可以使用ruby-ole 1.2.11.4来避免这个问题。

require 'rubygems'
gem 'ruby-ole','1.2.11.4'
require 'spreadsheet'

答案 1 :(得分:0)

我之前见过这些。首先验证您是否可以写入该文件的位置。 我的猜测是文件已在Excel中打开,或者您的防病毒软件阻止了“威胁”。