in reply to Win32::OLE & Excel help

I believe that creating ranges can be expensive so it would probably be better to create one range and then iterate through the cell looking for a match.*

Alternatively, you could try using the Excel worksheet CountIf() function. Here is a small working example:

#!/usr/bin/perl use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; my $application = Win32::OLE->new("Excel.Application"); my $workbook = $application->Workbooks->Open("c:/temp/test.xls" +); my $worksheet = $workbook->Worksheets(1); my $range = $worksheet->Range("A1:A8"); my $count = $application->WorksheetFunction->CountIf( $range, "2" +); print $count, "\n"; __END__

As a further alternative you could try using Jon Allen's XLSperl which gives you a commandline application that behaves like perl but works on Excel files. Something like this:

XLSperl -lne 'print if /pattern/ and $COL eq "A"' file.xls

XLSperl used Spreadsheet::ParseExcel which may also be an alternative solution.

* This isn't a general rule. It is usually better if you can call a single method on a Range rather than iterate through the Cells. However, I don't think there is an applicable method that will produce the desired results in this case.

--
John.

Replies are listed 'Best First'.
Re^2: Win32::OLE & Excel help
by imrags (Monk) on Jan 15, 2009 at 10:31 UTC
    Unfortunately the countif() function of excel doesn't do what I want it to
    I have also tried sumproduct for that...
    But sadly it doesn't work as well...that's the reason i'd to resort to loops...
    Raghu