This is a snippet of code that searches all Excel files in a directory for certain pieces of data. If any matches are found, their location is saved in a log file. It uses the technique described here to reduce memory consumption. This has come in quite handy at work, where we receive Excel files on a daily basis and occasionally need to find out which file some piece of information resides in.

Update: Based on a suggestion, I changed the code to use the qr// operator, so that the regular expressions are compiled only once, instead of every time a cell is searched.

use strict; use warnings; use File::Basename; use Spreadsheet::ParseExcel; $| = 1; my $excelDir = "c:/documents and settings/administrator/my documents/m +iddle_earth"; my @searchTerms = ( qr/FRODO/, qr/SAM/, qr/ARWEN/, qr/ARAGORN/ ); #maps column number to column letter as used by Excel my @columnMap = ('A'..'ZZ'); #any matches found are printed to log file my $logName = "excel_search.log"; open(my $fh, ">$logName") or die "can't open $logName: $!"; my $parse_excel = Spreadsheet::ParseExcel->new( CellHandler => \&cell_handler, NotSetCell => 1 ); #escape blanks in the excel directory path $excelDir =~ s/ /\\ /g; my $fileMatches; my $totalMatches = 0; foreach my $file (glob("$excelDir/*.xls")) { $fileMatches = 0; print "Searching " . basename($file) . "... "; $parse_excel->Parse($file); print $fileMatches, " matches found.\n\n"; $totalMatches += $fileMatches; } print $totalMatches, " total matches were found. See $logName for det +ails\n"; close($fh); sub cell_handler { my ($workbook, $sheet_index, $row, $col, $cell) = @_; foreach my $re (@searchTerms) { if ($cell->Value =~ $re) { #found a match; print out details print $fh "File: ", basename($workbook->{File}), "\n", "Sheet: ", $workbook->{Worksheet}[$sheet_index]->{Name}, "\n", "Cell: ", $columnMap[$col], ":", $row, "\n", "Cell Contents: ", $cell->Value, "\n\n"; $fileMatches++; } #end if } #end foreach }

In reply to Searching all Excel files in a directory by starchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.