Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse xls data with Spreadsheet::ParseExcel 1

Status
Not open for further replies.

Zhris

Programmer
Aug 5, 2008
254
GB
Hello,

This is the first time I have used this module and i'm having trouble parsing the data.

I have a very simple excel xls document with 2 worksheets completed with test data. I have used the example available from the documentation ( however nothing is displayed.

By debugging, it seems as if the script never enters the first for loop. I have also used data::dumper to view the entire contents of the hash reference and by briefly looking through the output I can see that the test data is there.

I simply want to pull out each individual piece of data.

Any suggestions?

Code:
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
use Data::Dumper;

my $parser   = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse('test.xls');

print Dumper($workbook);

print "test print 1\n";

for my $worksheet ( $workbook->worksheets() ) {
	print "test print 2\n";
	my ( $row_min, $row_max ) = $worksheet->row_range();
	my ( $col_min, $col_max ) = $worksheet->col_range();

	for my $row ( $row_min .. $row_max ) {
		print "test print 3\n";
		for my $col ( $col_min .. $col_max ) {
			print "test print 4\n";
			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";
		}
	}
}

Thank you,

Chris
 
Sorry to ask again, but does anyone have a suggestion as to where I should look for help on this issue? I have tried similar scripts with similar results. In case it matters I am using Excel 2003.

Thanks again,

Chris
 
The demo example and the documentation on package's methods do go hopelessly...wrong, I would say! This is the minimal re-work.
[tt]
[red]#[/red]for my $worksheet ( $workbook->worksheets() ) {
[blue]for my $worksheet ( @{$workbook->{Worksheet}} ) {[/blue]
print "test print 2\n";
[blue]if (defined $worksheet->{MaxRow}) [highlight]{[/highlight] #add this to filter empty worksheet[/blue]
[red]#[/red]my ( $row_min, $row_max ) = $worksheet->row_range();
[blue]my ( $row_min, $row_max ) = ($worksheet->{MinRow}, $worksheet->{MaxRow});[/blue]
[red]#[/red]my ( $col_min, $col_max ) = $worksheet->col_range();
[blue]my ( $col_min, $col_max ) = ($worksheet->{MinCol}, $worksheet->{MaxCol});[/blue]

for my $row ( $row_min .. $row_max ) {
print "test print 3\n";
for my $col ( $col_min .. $col_max ) {
print "test print 4\n";
[red]#[/red]my $cell = $worksheet->get_cell( $row, $col );
[blue]my $cell = $worksheet->{Cells}[$row][$col];[/blue]
next unless $cell;

print "Row, Col = ($row, $col)\n";
[red]#[/red]print "Value = ", $cell->value(), "\n";
[blue]print "Value = ", $cell->[red]V[/red]alue(), "\n";[/blue]
[red]#[/red]print "Unformatted = ", $cell->unformatted(), "\n";
[blue]print "Unformatted = ", $cell->{Val}, "\n";[/blue]
print "\n";
}
}
[blue][highlight]}[/highlight][/blue]
}
[/tt]
 
Thank you so much, that was really helpful. Pretty bad of the modules author.

Thanks again,

Chris
 
I can use your original script to read an excel file without ANY problems at all. The documentation that comes with the module is perfectly ok. Whatever problems you were experiencing with your original code do not lie with the syntax used!
 
This forum has a timestamp. Maybe your version is updated between 16 and 19?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top