2009-10-16 41 views
3

有沒有辦法從Ruby讀取Excel 97-2003文件?Ruby:解析Excel 95-2003文件?

背景

我目前使用Ruby寶石ParseExcel的 - http://raa.ruby-lang.org/project/parseexcel/ 但它是Perl模塊的老港口。它工作正常,但它解析的最新格式是Excel 95.猜猜怎麼樣? Excel 2007不會生成Excel 95格式。

John McNamara已經接管了作爲Perl Excel解析器維護者的職責,請參見http://metacpan.org/pod/Spreadsheet::ParseExcel當前版本將解析Excel 95-2003文件。但是有沒有Ruby的端口?

我的另一個想法是構建一些Ruby到Perl粘合代碼,以便從Ruby中使用Perl庫本身。例如,見What's the best way to export UTF8 data into Excel?

(我認爲這將是快得多寫膠水代碼,而不是端口分析器)

感謝,

拉里

回答

8

我使用spreadsheet,給它一個鏡頭。

+0

我使用Excel生成電子表格,它工作的很好。沒有太多的暴露於解析方面。 –

+0

謝謝,這就是我一直在尋找的。 –

+0

我使用相同的 - 特別是從Windows Server切換到Ubuntu服務器8之後) –

0

我還沒有嘗試解析Excel文件之前,但我知道FasterCSV是一個偉大的庫解析CSV文件(Excel可以產生)。

3

以我的經驗spreadsheet作品比roo快得多,但是袋鼠可以支持的.xlsx格式的電子表格不能。

+1

如何使用roo gem寫入文件? –

1

正如khell所說,電子表格是一個很好的工具。請參閱我以前用於構建抓取工具的代碼。

require 'find' 
require 'spreadsheet' 
Spreadsheet.client_encoding = 'UTF-8' 

count = 0 

Find.find('/Users/toor/crawler/') do |file|    # begin iteration of each file of a specified directory 
    if file =~ /\b.xls$\b/        # check if a given file is xls format 
    workbook = Spreadsheet.open(file).worksheets  # creates an object containing all worksheets of an excel workbook 
    workbook.each do |worksheet|      # begin iteration over each worksheet 
     worksheet.each do |row|       # begin iteration over each row of a worksheet 
     if row.to_s =~ /regex/       # rows must be converted to strings in order to match the regex 
      puts file 
      count += 1 
     end 
     end 
    end 
    end 
end 

puts "#{count} pieces of information were found"