2012-11-08 47 views
1

我有腳本的問題,使簡單的.xls文件和寫入數據到一個單元格。下面是簡單的代碼:Ruby電子表格:錯誤的文件描述符 - test.xls(Errno :: EBADF)

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 open' from 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:in write_workbook' from 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' from 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>' 

回答

3

因爲Ruby-OLE 1.2.11.5不支持Windows平臺, 更多詳細信息:ruby-ole issue

可以使用紅寶石OLE 1.2.11.4避免這個問題。

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

我以前見過這些。首先驗證您可以寫入該文件的位置。 我的猜測是該文件已在Excel中打開,或者您的防病毒軟件阻止了「威脅」。

相關問題