CPAN::Forum
Spreadsheet-ParseExcel - Script Fails to Parse (access) Excel Spreadsheet
| Posted on Fri Sep 19 22:51:45 2008 by finhagen |
| Script Fails to Parse (access) Excel Spreadsheet |
|
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 foll0ws:
Sinn:/lebensraum/perl # ./excel.syr.extract.pl
<code>\lebensraum\perl\SAMReport.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 "Un
+able 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\\perl\\SAMReport.xls")or die "Un
+able 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')");
}
}
exit;
|
| Write a response |
(8)
]