I am attempting to convert a script I found on google to my purposes: (http://www.linuxjournal.com/content/reading-native-excel-files-perl). My script doesn't seem to actually read the spreadsheet I am specifying so I don't get any of my print statements after line 35.
My output is as follows:
Sinn:/lebensraum/perl # ./excel.syr.extract.pl \lebensraum\perl\SAMRep
+ort.xls
Made it so far
HASH(0xc682e0)
So my $worksheet variable prints as a HASH - which could be right but seems wrong to me. I have added a bunch of print statements in an effort to debug the problem, but most of those don't print. The DBI connection and insert statements work so I am not concerned about my mysql connection. My sense is the
$workbook = Spreadsheet::ParseExcel::Workbook->Parse("\\lebensraum\\perl\\SAMReport.xls")or die "Unable to open $file\n";
statement is failing somehow but not in a way that reports an error. However, I am new to this module so I could easily be doing something else wrong. Any help would be greatly appreciated.
Hagen Finley
Boulder, CO
#!/usr/bin/perl
use DBI;
use DBD::mysql;
use CGI;
use Spreadsheet::ParseExcel;
# CONFIG VARIABLES
$platform = "mysql";
$database = "smdb";
$host = "Sinn";
$port = "3306";
$tablename = "site";
$user = "user";
$pw = "password";
#DATA SOURCE NAME
$dsn = "dbi:mysql:smdb:localhost:3306";
# PERL DBI CONNECT (RENAMED HANDLE)
$dbh = DBI->connect($dsn, $user, $pw)or die "Unable to connect: $DBI::
+errstr\n";
#$dbh->do("insert into site (siteid,name,city) values
# (12345, \'BXB-200\',\'Boxborough\')");
#$cgi = new CGI;
$file = "\\lebensraum\\perl\\SAMReport.xls";
print "$file\n";
$workbook = Spreadsheet::ParseExcel::Workbook->Parse("\\lebensraum\\pe
+rl\\SAMReport.xls")or die "Unable to open $file\n";
print "Made it so far\n";
print "$workbook\n";
#locate columns in the spreadsheet from which we want to extract data
foreach $sheet (@{$workbook->{worksheet}}) {
print "Sheet number $sheet\n";
foreach $col ($sheet->{MinCol} .. $sheet->{MaxCol}) {
if ($sheet->{Cells}[0][$col]->{Val} eq "Site Number") {
$siteid = $col;
print "$siteid\n";}
#else print "Could not find column Site Number\n";}
if ($sheet->{Cells}[0][$col]->{Val} eq "Site Name") {
$name = $col;
print "$name\n";}
#else print "Could not find column Site Name\n";}
if ($sheet->{Cells}[0][$col]->{Val} eq "City") {
$city = $col;
print "$city\n";}
#else print "Could not find column City\n";}
}
print"Column find completed successfully - moving on to row find\n";
#iterate through spreadsheet rows and extract site.siteid,site.name &
+site.city
foreach $row ($sheet->{MinRow}+1 .. $sheet->{MaxRow}) {
$site_number = $sheet->{Cells}[$row][$siteid]->{Val};
$site_name = $sheet->{Cells}[$row][$name]->{Val};
$site_city = $sheet->{Cells}[$row][$city]->{Val};
print "$site_number\n";
print "$site_name\n";
print "$site_city\n";
$dbh->do("insert into site (siteid,name,city) values
(\$site_number, \'$site_name\',\'$site_city')");
}
#print $cgi->header();
#print <<EOF
#<html>
#<head>
#<title>Data has been uploaded
#<head>
#<body>
#Thank you.
#</body>
#</html>
#EOF
#;
}
exit;