parsing excel files in ruby – the parseexcel gem 21

Posted by chad on November 28, 2007

Highly recommended gem does exactly what it says. Here’s the code to turn an entire worksheet from an excel doc into a multi-dimensional array:

wb = Spreadsheet::ParseExcel.parse(filename)
rows = wb.worksheet(worksheet).map() { |r| r }.compact
grid = rows.map() { |r| r.map() { |c| c.to_s('latin1')}.compact rescue nil }

Just ‘gem install parseexcel’ (though i installed from source before i realized it was a pre-packaged gem).

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Sanjay Thu, 14 Feb 2008 21:16:17 UTC

    I have tried using parseexcel, but I am getting an error “Array is not implemented” Please help me out to resolve this problem.
    thanks
    -sanajy

  2. chad Thu, 14 Feb 2008 22:41:39 UTC

    Hi Sanjay – i sent you an email. Can you post a sample of the code you used to call the gem?

  3. Sanjay Sun, 17 Feb 2008 19:35:09 UTC

    Hi Chad:
    Following is the code (I could not check my mail yesterday). I tried to read the excel file by using two pieces of code but got the same error:
    ====
    require ‘parseexcel’
    puts “in the prog”
    wbook = Spreadsheet::ParseExcel.parse(‘D:\san_infy\ruby\norm_dist_data.xls’)

    ## I am getting error at this point
    puts “excel file read”
    ## Added to test ###
    #Get the first worksheet

    #rows = wbook.worksheet(0).map() { |r| r }.compact
    #grid = rows.map() { |r| r.map() { |c| c.to_s(‘latin1′)}.compact rescue nil }
    #puts “excel read”
    worksheet1=Array.new
    worksheet1 = wbook.worksheet(0)
    puts “matrix generated”

    ## the program is not reading in
    #cycle over every row
    worksheet1.each do |row|
    j=0
    i=0
    puts “in the row”
    if row != nil
    #cycle over each cell in this row if it’s not an empty row
    row.each do |cell|
    puts “in the cell”
    if cell != nil
    #Get the contents of the cell as a string
    contents = cell.to_f()
    puts “Row: #{j} Cell: #{i}> #{contents}”
    end
    i = i+1
    end
    end
    j=j+1
    end
    =====

    thanks
    -sanjay

  4. chad Mon, 17 Mar 2008 12:58:51 UTC

    somehow i never saw your comment – if you do still need help, let me know and i’ll take a look.

  5. Dany Fri, 13 Jun 2008 01:01:51 UTC

    You forgot:

    require ‘rubygems’

    before

    require ‘parseexcel’

  6. Brian Thu, 19 Jun 2008 05:32:38 UTC

    I am trying to use this parser. Am I missing some include or something?

    require ‘watir’
    include Watir
    require ‘rubygems’
    require ‘parseexcel’
    require ‘test/unit’

    class TC_recorded

  7. I'm trying to use this parser too Thu, 19 Jun 2008 10:10:30 UTC

    i also get “array is not implemented” but i have
    require ‘rubygems’
    before
    require ‘parseexcel’

    def pull(x,y)

    require ‘rubygems’
    require ‘parseexcel’

    wb = Spreadsheet::ParseExcel.parse(‘filepath’)
    ws = Array.new
    ws = wb.worksheet(13)

    stp = Array.new
    head = Array.new
    stp = ws.row(6)
    head = ws.row(7)

    end

  8. Gordon Thu, 19 Jun 2008 10:11:50 UTC

    def pull(x,y)

    require ‘rubygems’
    require ‘parseexcel’

    wb = Spreadsheet::ParseExcel.parse(‘filepath’)

    ws = wb.worksheet(13)

    stp = Array.new
    head = Array.new
    stp = ws.row(6)
    head = ws.row(7)
    end

  9. Gordon Thu, 19 Jun 2008 10:12:22 UTC

    i get the “array is not implemented” as well

  10. omkar Wed, 20 Aug 2008 23:33:14 UTC

    tell me how exactly to fetch data from excel sheets using ruby in watir. pls provide me exact code , pls tell us which methods to include as well

  11. shawn Fri, 29 Aug 2008 17:42:06 UTC

    I want to use this to GENERATE an .xls, but I don’t see any to_xls or write_xls or anything like that. Can somebody give me a hint?

    require ‘parseexcel’
    sheet = Spreadsheet::ParseExcel::Worksheet.new
    sheet.add_cell(0, 0, ‘foo’)

    # Now write…
    #sheet.write(‘test.xls’) ?
    #File.open(‘test.xls’, ‘w’) do |f| f.write(sheet.to_xls) end ?

  12. Sri Tue, 02 Sep 2008 12:41:03 UTC

    I was able to make the Chad’s example. Here is what I have in my file:
    ———
    require “parseexcel”

    puts “Hello World”
    wb = Spreadsheet::ParseExcel.parse(“C:/Apps/AppsData/workspace/requestcentral/public/downloads/TravelReportUploadTemplate_Test.xls”)
    ws = wb.worksheet(0)
    rows = ws.map() { |r| r }.compact
    grid = rows.map() { |r| r.map() { |c| c.to_s(‘latin1′)}.compact rescue nil }
    grid.each do |g|
    p g
    end

    ———
    The code is supposed to print all the rows to the console. It tries but it prints ‘nil’ for most of the rows, even though it’s not nil in the actual file.

    Bottom line, this code is not ready guys like they mentioned at the source where Chad got it from (Chad gave the link to source in his comment).

    I wish someone could make the basic stripping work.

    -Sri

  13. Sri Tue, 02 Sep 2008 12:48:23 UTC

    BTW, above bug is filed int he bug tracking system. Try to go there from the project page:
    http://rubyforge.org/projects/spreadsheet

  14. naveen Thu, 06 Nov 2008 00:45:28 UTC

    HI can any get me the code in ruby ,where i need to write my gami ifo into excel sheet format

  15. naveen Thu, 06 Nov 2008 01:17:59 UTC

    hai can u guve the code to write the information of gmail into excel format

  16. Nirvana Mon, 11 May 2009 02:10:55 UTC

    How to loop through the worksheets if there is more than one? An example will work best for me.

  17. sudhi Tue, 04 Aug 2009 22:21:43 UTC

    Where is the gem? parseexcel

  18. LarryK Fri, 16 Oct 2009 09:45:40 UTC

    Unfortunately, the parseexcel gem does not seem to have been updated for a couple of years. (As of October, 2009.) And the latest version only reads Excel 95.

    But Excel 2007 no longer offers the option of writing a file into Excel 95 format. So the you’ll run into more and more copies of Excel that can’t create an Excel file for this parser. Sigh…

    Note, source git repo for the project is http://scm.ywesee.com/?p=parseexcel;a=summary

  19. Gruff Wed, 13 Jan 2010 09:06:45 UTC

    Thanks Larry, I was having the same problem with nil rows, but I saved as excel95 format and it solved the problem!

    Gruff

  20. Gruff Wed, 13 Jan 2010 09:11:58 UTC

    Oh… one correction: Excel 2007 does let you save as Excel 5.0/95 … so it’s okay.

  21. Chris Mon, 12 Apr 2010 10:34:30 UTC

    updated to exclude nil rows and cells

    wb = Spreadsheet::ParseExcel.parse(file)
    rows = wb.worksheet(0).map() {|r| r unless r.nil?}.compact
    grid = rows.map() {|r| r.map() {|c| c.to_s(‘latin1′) unless c.nil?}.compact rescue nil}

Comments