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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.