代码之家  ›  专栏  ›  技术社区  ›  Larry K

Ruby:解析Excel 95-2003文件?

  •  3
  • Larry K  · 技术社区  · 16 年前

    背景

    我目前正在使用Ruby Gem 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

    我的另一个想法是构建一些Ruby到Perl的粘合代码,以便从Ruby中使用Perl库本身。例如,看 What's the best way to export UTF8 data into Excel?

    谢谢,

    6 回复  |  直到 9 年前
        1
  •  8
  •   khelll    16 年前

    我正在使用 spreadsheet

        2
  •  3
  •   Tom Huras    16 年前
        3
  •  3
  •   Rob Rupprath    15 年前

    spreadsheet roo 但是,roo可以支持电子表格无法支持的.xlsx格式。

        4
  •  1
  •   Anconia    13 年前

    正如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"
    
        5
  •  0
  •   zgchurch    16 年前

    我以前没有尝试过解析Excel文件,但我知道FasterCSV是一个解析CSV文件(Excel可以生成)的好库。

    推荐文章