1

我对制作简单 .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:in 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 initialize'

    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:in 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:inwrite_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 open' 来自 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 write'

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

2 回答 2

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'
于 2012-11-09T02:47:08.937 回答
0

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

于 2012-11-09T00:48:06.790 回答