#!c:\perl\bin\perl -w use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; if(scalar @ARGV != 2) { usage(); exit 1; } my $outputFile = $ARGV[0]; my $inputDir = $ARGV[1]; open (OUTFILE, ">$outputFile"); &traverse($inputDir); exit 1; ########################## # traverse directories ######################### sub traverse { my($dir) = shift; my($path); unless (opendir(DIR, $dir)) { warn "Can't open $dir\n"; closedir(DIR); return; } foreach (readdir(DIR)) { next if $_ eq '.' || $_ eq '..'; $path = "$dir/$_"; if (-d $path) { # a directory &traverse($path); } elsif (-f _) { # a plain file next if ( $_ !~ /\.xls$/i); &open_xls($path); } #else-if file } closedir(DIR); } ########################## # Open spreadsheet ######################### sub open_xls{ my $path = shift; print "PATH1=$path\n"; $path =~ s/\//\\\\/; print "PATH2=$path\n"; $Win32::OLE::Warn = 3; # die on errors... # get already active Excel application or open new my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit') or die Win32::OLE->LastError(); # open Excel file my $Book = $Excel->Workbooks->Open($path) or die Win32::OLE->LastError(); my $wkSheetCount = $Book->Worksheets->Count; foreach my $sheetnum (1..$wkSheetCount) { my $Sheet = $Book->Worksheets($sheetnum); $Sheet -> Activate(); my $name = $Sheet->Name; # print "Working on sheet # $sheetnum - Its name is $name\n"; my $everythingArray=$Sheet->UsedRange()->{'Value'}; # my $everythingArray = $Sheet->Range("A8:B9")->{'Value'}; foreach my $ref_array (@$everythingArray) { foreach my $scalar (@$ref_array) { print "$scalar\t"; } print "\n"; } } #foreach sheet $Book->Close; undef $Book; $Excel->Quit; } sub usage{ print "Usage:\n" . "\tperl $0 OutputFile DirectoryPath\n"; }