2011-09-02 54 views
6

我想读取ruby中的excel文件,然后取出数据并将其插入到mysql数据库中。我使用电子表格:: excel和mysql gems。由于某种原因,我似乎无法能够使用电子表格的宝石来打开文件进行读取...这是我的代码红宝石excel阅读器,电子表格gem

require 'rubygems' 
require 'spreadsheet' 
require 'mysql' 

Spreadsheet.client_encoding = 'UTF-8' 

book = Spreadsheet.open '/home/data/teams.xlsb' 

b1 = book.worksheet('team1') 

当我运行该脚本,我得到这个奇怪的错误

/usr/local/lib/ruby/gems/1.8/gems/ruby-ole-1.2.11.1/lib/ole/storage/base.rb:376:in `validate!': OLE2 signature is invalid (Ole::Storage::FormatError) 
    from /usr/local/lib/ruby/gems/1.8/gems/ruby-ole-1.2.11.1/lib/ole/storage/base.rb:368:in `initialize' 
    from /usr/local/lib/ruby/gems/1.8/gems/ruby-ole-1.2.11.1/lib/ole/storage/base.rb:110:in `new' 
    from /usr/local/lib/ruby/gems/1.8/gems/ruby-ole-1.2.11.1/lib/ole/storage/base.rb:110:in `load' 
    from /usr/local/lib/ruby/gems/1.8/gems/ruby-ole-1.2.11.1/lib/ole/storage/base.rb:77:in `initialize' 
    from /usr/local/lib/ruby/gems/1.8/gems/ruby-ole-1.2.11.1/lib/ole/storage/base.rb:83:in `new' 
    from /usr/local/lib/ruby/gems/1.8/gems/ruby-ole-1.2.11.1/lib/ole/storage/base.rb:83:in `open' 
    from /usr/local/lib/ruby/gems/1.8/gems/spreadsheet-0.6.5.8/lib/spreadsheet/excel/reader.rb:1144:in `setup' 
    from /usr/local/lib/ruby/gems/1.8/gems/spreadsheet-0.6.5.8/lib/spreadsheet/excel/reader.rb:121:in `read' 
    from /usr/local/lib/ruby/gems/1.8/gems/spreadsheet-0.6.5.8/lib/spreadsheet/excel/workbook.rb:32:in `open' 
    from /usr/local/lib/ruby/gems/1.8/gems/spreadsheet-0.6.5.8/lib/spreadsheet.rb:62:in `open' 
    from /usr/local/lib/ruby/gems/1.8/gems/spreadsheet-0.6.5.8/lib/spreadsheet.rb:68:in `open' 
    from sizes_importer2.rb:7 

我在某处看到这个gem可能无法支持.xlsb文件。我不知道这是否是真的,但如果是有人能告诉我如何绕过它,或者如果有什么不对吗?我完全失去了,并会感谢任何帮助这个错误。谢谢

+4

必须将文件转换为2003 xls – Crobos

回答

5

你已经看过roo了吗? - >http://rubygems.org/gems/roo

+3

没有为我工作! – barbolo

+1

@barbolo:它可能看起来像xlsx支持不起作用,但您应该使用'Excelx.new '来加载它。奇迹般有效。 –

+0

我不得不投票。就像在Linux上寻求帮助并被告知使用Windows一样。 – baash05