# !/usr/bin/perl -w use warnings; use strict; use Data::Dump qw[ pp ]; use Spreadsheet::XLSX; use Spreadsheet::ParseXLSX; my %DivisionHash; my $inputFile; my $rowCity; my $cellCity; my $prejob; $prejob = shift; $inputFile = shift; $rowCity = shift; $cellCity = shift; open INPUT, "<$inputFile" or die "Unable to open input file: $inputFil +e !!!\nExiting..."; open INPUT2, "<SeedList.L.txt" or die "Unable to open input file: Seed +List.L.txt !!!\nExiting..."; open OUTPUT, ">$prejob.SeedList.L.txt" or die "Unable to create/overwr +ite output file: !!!\nExiting..."; while (<INPUT2>) {print OUTPUT $_;} close INPUT2; while (<INPUT>) { chomp; my $division = uc(trimspaces(substr($_,207-1,20))); # my $DivisionHash = substr($_,207-1,20); if (not exists $DivisionHash{$division}){ $DivisionHash{$division} = $_; # print STDOUT $DivisionHash{$division}; }# exit; } close INPUT; my $filename = "\\Aldi_Production_Emad.xlsx"; # Parse excel file my $parser = Spreadsheet::ParseXLSX->new; my $workbook = $parser->parse("$filename"); if ( !defined $workbook ) { die $parser->error(), ".\n"; } # Get cell value from excel sheet1 row 1 column 2 my $worksheet = $workbook->worksheet(0); my $cell = $worksheet->get_cell($rowCity,$cellCity); # Print the cell value when not blank if ( defined $cell and $cell->value() ne "") { my $value = $cell->value(); $value = uc(trimspaces($value)); # print STDOUT "this is value: $value"; # exit; # if($DivisionHash eq $value){ # print OUTPUT $_ . "\n"; # } if(exists ($DivisionHash{$value})) { # print STDOUT $DivisionHash{$value}; # exit; # $_ = $DivisionHash{$value}; print OUTPUT $DivisionHash{$value} . "\n"; } else { print STDERR "Value $value Doesn't exist \n"; exit; } } close OUTPUT; sub trimspaces { my @argsarray = @_; $argsarray[0] =~ s/^\s+//; $argsarray[0] =~ s/\s+$//; return $argsarray[0]; }

I worked on the code and fixed it, thanks for your help guys


In reply to loop through xlsx and get specific column cell values by emadmahou

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.