in reply to Searching a pattern in a xlsx

Hello kaushik9918,

Although the fellow Monk beech provided you already the best answer just for fun an alternative solution.

Another possible way, with more lines of code is to simply read the excel file all sheets and search for the keyword while you iterate. On sample of code bellow I create a test sheet and then I iterate over it. For you the important part is where you read through the rest is unnecessary.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; use Excel::Writer::XLSX; use Spreadsheet::Read qw(ReadData); my $workbook = Excel::Writer::XLSX->new( 'simple.xlsx' ); my $worksheet = $workbook->add_worksheet(); my @data_for_row = (1, 2, 3); my @table = ( [4, 5], [6, 7], ); my @data_for_column = (10, 11, 12); $worksheet->write( "A1", "Hi Excel!" ); $worksheet->write( "A2", "second row" ); $worksheet->write( "A3", \@data_for_row ); $worksheet->write( 4, 0, \@table ); $worksheet->write( 0, 4, [ \@data_for_column ] ); $workbook->close; # From this point onward is important my $book = ReadData ('simple.xlsx'); my @sheets = @$book[0]->{sheets}; my %hash; my $key_word = 'second row'; foreach my $sheet_number (@sheets) { my @rows = Spreadsheet::Read::rows($book->[$sheet_number]); foreach my $i (1 .. scalar @rows) { foreach my $j (1 .. scalar @{$rows[$i-1]}) { say "Cell/Row: " . chr(64+$j) . $i . " at sheet: ".$sheet_numb +er." key word: " . ($rows[$i-1][$j-1] // '') if ($rows[$i-1][$j-1] // '') eq $key_word; $hash{"Sheet: " . $sheet_number}{chr(64+$j) . "$i "} = ($rows[ +$i-1][$j-1] // ''); } } } # print Dumper \%hash; __END__ $ perl excel.pl Cell/Row: A2 at sheet: 1 key word: second row

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Searching a pattern in a xlsx
by kaushik9918 (Sexton) on Jun 21, 2018 at 09:59 UTC
    Thank you for taking out time to write this, but I already know the sheet from where I want to read. So, I will filter out the parts I want from your code and check. thanks again!