The code needs some basic checks on whether the worksheet contains 'Account Names' and 'Account Numbers' columns before trying to access data in those columns. In addition there should be checks on whether the accessed rows contain data.

If the cells don't contain information then Spreadsheet::ParseExcel will return undef values.

The following is a working example. Substitute the prints for database inserts and the warns for error reporting and you should be most of the way there.

#!/usr/bin/perl use strict; use warnings; use Spreadsheet::ParseExcel; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse( 'accounts.xls' ); if ( !defined $workbook ) { die $parser->error(), ".\n"; } WORKSHEET: for my $worksheet ( $workbook->worksheets() ) { my $sheetname = $worksheet->get_name(); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); my $account_name_col; my $account_number_col; # Skip worksheet if it doesn't contain data. if ( $row_min > $row_max ) { warn "\tWorksheet $sheetname doesn't contain data.\n"; next WORKSHEET; } # Check for certain column headers in the first data row. COLUMN: for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row_min, $col ); next COLUMN unless $cell; $account_name_col = $col if $cell->value() eq 'Account N +ames'; $account_number_col = $col if $cell->value() eq 'Account N +umbers'; } # Check that we found the required columns headers. if ( defined $account_name_col && defined $account_number_col +) { ROW: for my $row ( $row_min + 1 .. $row_max ) { my $name_cell = $worksheet->get_cell( $row, $account +_name_col ); my $number_cell = $worksheet->get_cell( $row, $account +_number_col ); if ( defined $name_cell && defined $number_cell ) { my $name = $name_cell->value(); my $number = $number_cell->value(); # We have the data we are looking for. Do somethin +g with it. print "Worksheet = $sheetname\n"; print "Row = $row\n"; print "Account name = $name\n"; print "Account number = $number\n\n"; } else { # Handle the error condition in some way. warn "\tWorksheet $sheetname, Row = $row doesn't c +ontain " . "'Account Names' and 'Account Numbers' data.\n +"; next ROW; } } } else { # Handle the error condition in some way. warn "\tWorksheet $sheetname: Didn't find 'Account Names' +and " . "'Account Numbers' headings.\n"; next WORKSHEET; } } __END__

--
John.


In reply to Re: Help with Spreadsheet::ParseExcel by jmcnamara
in thread Help with Spreadsheet::ParseExcel by Anonymous Monk

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.