Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: Spreadsheet::ParseExcel Script Fails to Parse (access) Excel Spreadsheet

by bmann (Priest)
on Sep 19, 2008 at 22:58 UTC ( #712656=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Spreadsheet::ParseExcel Script Fails to Parse (access) Excel Spreadsheet
in thread Spreadsheet::ParseExcel Script Fails to Parse (access) Excel Spreadsheet

foreach my $sheet (@{$workbook->{worksheet}}) {
Case sensitivity - should be @{$workbook->{Worksheet}}, not worksheet

HTH


Comment on Re^3: Spreadsheet::ParseExcel Script Fails to Parse (access) Excel Spreadsheet
Select or Download Code
Re^4: Spreadsheet::ParseExcel Script Fails to Parse (access) Excel Spreadsheet
by finhagen (Sexton) on Sep 20, 2008 at 05:06 UTC
    Thanks so much for everyone's help. I am making progress. Changing the worksheet to Worksheet and then using just the file name for my xls file allowed me to parse the spreadsheet. However, the output of my parsing isn't correct and it looks like the breakdown is the capturing of the column data (e.g. $city =$col;) isn't carrying over to the row iteration. In my efforts to correct that I am encountering a compile error:
    Global symbol "$siteid" requires explicit package name at ./excel.syr. +extract3.pl line 48. Global symbol "$name" requires explicit package name at ./excel.syr.ex +tract3.pl line 49. Global symbol "$address" requires explicit package name at ./excel.syr +.extract3.pl line 50. Global symbol "$city" requires explicit package name at ./excel.syr.ex +tract3.pl line 51. Global symbol "$state" requires explicit package name at ./excel.syr.e +xtract3.pl line 52. Global symbol "$zip" requires explicit package name at ./excel.syr.ext +ract3.pl line 53. Execution of ./excel.syr.extract3.pl aborted due to compilation errors +.
    I imagine that the "explicit package name" issue is something the PerlMonks understand well, but I have to confess I don't know what it means or what to do about it. Here is my latest script:
    #!/usr/bin/perl use strict; use warnings; use Spreadsheet::ParseExcel; my $file = "SAMReport.xls"; my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($file)or die " +Unable to open $file\n"; #locate columns in the spreadsheet from which we want to extract data foreach my $sheet (@{$workbook->{Worksheet}}) { print "Sheet number $sheet\n"; foreach my $col ($sheet->{MinCol} .. $sheet->{MaxCol}) { if ($sheet->{Cells}[0][$col]->{Val} eq "Site Number") { my $siteid = $col; } if ($sheet->{Cells}[0][$col]->{Val} eq "Site Name") { my $name = $col; } if ($sheet->{Cells}[0][$col]->{Val} eq "Address") { my $address = $col; } if ($sheet->{Cells}[0][$col]->{Val} eq "City") { my $city = $col; } if ($sheet->{Cells}[0][$col]->{Val} eq "State") { my $state = $col; } if ($sheet->{Cells}[0][$col]->{Val} eq "Zip Code") { my $zip = $col; } } #iterate through spreadsheet rows and extract site.siteid, site.name, +site.address, site.city, site.state, site.zip foreach my $row ($sheet->{MinRow}+1 .. $sheet->{MaxRow}) { my $site_number = $sheet->{Cells}[$row][$siteid]; my $site_name = $sheet->{Cells}[$row][$name]; my $site_address = $sheet->{Cells}[$row][$address]; my $site_city = $sheet->{Cells}[$row][$city]; my $site_state = $sheet->{Cells}[$row][$state]; my $site_zip = $sheet->{Cells}[$row][$zip]; #print captured output print "$site_number\n"; print "$site_name\n"; print "$site_address\n"; print "$site_city\n"; print "$site_state\n"; print "$site_zip\n"; } } exit;
    Thanks again for everyone's kind assistance! Hagen
      #!/usr/bin/perl -- use strict; use warnings; use diagnostics; my $foo = 1; {# a new scope my $bar = 2; }# end of new scope my $baz = $bar; # $bar doesn't exist in this scope __END__ Global symbol "$bar" requires explicit package name at test.pl line 12 +. Execution of test.pl aborted due to compilation errors (#1) (F) You've said "use strict vars", which indicates that all variab +les must either be lexically scoped (using "my"), declared beforehand +using "our", or explicitly qualified to say which package the global var +iable is in (using "::"). Uncaught exception from user code: Global symbol "$bar" requires explicit package name at test.pl + line 12. Execution of test.pl aborted due to compilation errors. at test.pl line 13
      To "fix" it, you do something like
      my $foo = 1; my $bar; {# a new scope $bar = 2; }# end of new scope my $baz = $bar;
        Thanks so much. Using our $var did the trick and my script is functioning properly now. Here is the final version:
        #!/usr/bin/perl use strict; use warnings; use DBI; use DBD::mysql; use Spreadsheet::ParseExcel; # DBI configuration variables my $platform = "mysql"; my $database = "smdb"; my $host = "Sinn"; my $port = "3306"; my $tablename = "site"; my $user = "user"; my $pw = "password"; #Data source name my $dsn = "dbi:mysql:smdb:localhost:3306"; # PERL DBI connection handle my $dbh = DBI->connect($dsn, $user, $pw)or die "Unable to connect: $DB +I::errstr\n"; #xls file to be parsed my $file = "SAMReport.xls"; #global variables our $siteid; our $name; our $address; our $city; our $state; our $zip; my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($file)or die " +Unable to open $file\n"; #locate columns in the spreadsheet from which we want to extract data foreach my $sheet (@{$workbook->{Worksheet}}) { print "Sheet number $sheet\n"; foreach my $col ($sheet->{MinCol} .. $sheet->{MaxCol}) { if ($sheet->{Cells}[0][$col]->{Val} eq "Site Number") { $siteid = $col; print "$siteid\n"; } if ($sheet->{Cells}[0][$col]->{Val} eq "Site Name") { $name = $col; print "$name\n"; } if ($sheet->{Cells}[0][$col]->{Val} eq "Address") { $address = $col; print "$address\n"; } if ($sheet->{Cells}[0][$col]->{Val} eq "City") { $city = $col; print "$city\n"; } if ($sheet->{Cells}[0][$col]->{Val} eq "State") { $state = $col; print "$state\n"; } if ($sheet->{Cells}[0][$col]->{Val} eq "Zip Code") { $zip = $col; print "$zip\n"; } } #iterate through spreadsheet rows and extract site.siteid, site.name, +site.address, site.city, site.state, site.zip foreach my $row ($sheet->{MinRow}+1 .. $sheet->{MaxRow}) { my $site_number = $sheet->{Cells}[$row][$siteid]->{Val}; my $site_name = $sheet->{Cells}[$row][$name]->{Val}; my $site_address = $sheet->{Cells}[$row][$address]->{Val}; my $site_city = $sheet->{Cells}[$row][$city]->{Val}; my $site_state = $sheet->{Cells}[$row][$state]->{Val}; my $site_zip = $sheet->{Cells}[$row][$zip]->{Val}; $dbh->do("insert into site (indate,sitei +d,name,address,city,state,zip) values (CURDATE(),\'$site_number\',\'$site_name\',\'$site_address +\',\'$site_city',\'$site_state\',\'$site_zip\')"); } } exit;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://712656]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (17)
As of 2014-09-17 17:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (92 votes), past polls