rahul118bbsr has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks, I am new to using the spreadsheet module. I downloded it from CPAN installed it on my Windows. I have written as small code which reads from an xls file & prints the row values. The code is looks like this:
#!/usr/bin/perl -w use strict; use base 'Spreadsheet::ParseExcel::Worksheet'; use Spreadsheet::Worksheet; use Spreadsheet::Workbook; use Spreadsheet::ParseExcel; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('Book1.xls'); if ( !defined $workbook ) { die $parser->error(), ".\n"; } for my $worksheet ( $workbook->worksheets() ) { my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; print "Row, Col = ($row, $col)\n"; print "Value = ", $cell->value(), "\n"; print "Unformatted = ", $cell->unformatted(), "\n"; print "\n"; } } }

However on executing it I get the following error: *********************************************************** C:\Perl\myCode>perl test.pl.txt Base class package "Spreadsheet::ParseExcel::Worksheet" is empty. (Perhaps you need to 'use' the module which defines that package first, or make that module available in @INC (@INC contains: C:/Perl/site/lib C:/Perl/lib .). at C:/Perl/site/lib/Spreadsheet/ParseExcel/Worksheet.pm line 25 BEGIN failed--compilation aborted at C:/Perl/site/lib/Spreadsheet/ParseExcel/Worksheet.pm line 25. Compilation failed in require at (eval 2) line 3. ...propagated at C:/Perl/lib/base.pm line 94. BEGIN failed--compilation aborted at test.pl.txt line 4. *********************************************************** However I have the Worksheet.pm file almost everywhere inside C:\Perl literally. Please advise

Replies are listed 'Best First'.
Re: Error using Spreadsheet module
by wind (Priest) on Apr 04, 2011 at 18:03 UTC

    Here is the code run through Perl::Tidy. It's the example from Spreadsheet::ParseExcel with a few extra statements at the beginning.

    #!/usr/bin/perl -w use strict; use base 'Spreadsheet::ParseExcel::Worksheet'; use Spreadsheet::Worksheet; use Spreadsheet::Workbook; use Spreadsheet::ParseExcel; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('Book1.xls'); if ( !defined $workbook ) { die $parser->error(), ".\n"; } for my $worksheet ( $workbook->worksheets() ) { my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; print "Row, Col = ($row, $col)\n"; print "Value = ", $cell->value(), "\n"; print "Unformatted = ", $cell->unformatted(), "\n"; print "\n"; } } }

    The following lines at the beginning of your script should just be removed:

    use base 'Spreadsheet::ParseExcel::Worksheet'; use Spreadsheet::Worksheet; use Spreadsheet::Workbook;
Re: Error using Spreadsheet module
by CountZero (Bishop) on Apr 04, 2011 at 18:31 UTC
    If you only need the formatted values of the cells, you can use Spreadsheet::ParseExcel::Simple:
    use Modern::Perl; use Spreadsheet::ParseExcel::Simple; my $workbook = Spreadsheet::ParseExcel::Simple->read('Book1.xls') or d +ie $!; for my $worksheet ( $workbook->sheets ) { while ( $worksheet->has_data ) { say "Value = $_" for $worksheet->next_row; } }

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James