in reply to Error while reading .xls file

You're not closing your Excel file after writing to it. Add an explicit $workbook->close() before calling get_max_row_col(), and your script will work.

While I'm at it, allow me to suggest doing proper error checking, too. Quoting Spreadsheet::ParseExcel's documentation (emphasis mine):

If an error occurs parse() returns undef. In general, programs should contain a test for failed parsing as follows:

my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('Book1.xls'); if ( !defined $workbook ) { die $parser->error(), ".\n"; }

Adding error checking to your code then yields:

No Excel data found in file at 1098852.pl line 32.

Which might already have given you a clue as to what's going wrong.

Replies are listed 'Best First'.
Re^2: Error while reading .xls file
by a.alandkar (Novice) on Aug 29, 2014 at 18:01 UTC
    Thank you mate. It works. :)
      You're welcome! *tips hat*

        Hi, is it necessary to close the file after reading it too..? i have updated the code,i.e. added some function and finding the same error when i run it. I have added following function

        &extract_data(); sub extract_data() { my $parsedata = Spreadsheet::ParseExcel->new(); my $workbook_data = $parsedata->parse('test.xls'); for my $worksheet1 ( $workbook_data->worksheet(0) ) { for my $col ($col_min .. $col_max) { my $cell = $worksheet1->get_cell( 0, $col ); if($p_name eq $cell) { print("Done"); } } } }

        when i run the code it give me (

        Can't call method "worksheet" on an undefined value at D:\Perl\Xls_han +dling.pl line 110, <STDIN> line 6.
        ) error. And if i try to close parser i.e. $parser -> close() it says (
        Can't call method "worksheet" on an undefined value at D:\Perl\Xls_han +dling.pl line 110, <STDIN> line 6.
        )