JSchmitz has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; use Spreadsheet::ParseExcel; my $oExcel = new Spreadsheet::ParseExcel; #1.1 Normal Excel97 my $oBook = $oExcel->Parse('Excel/BBI.xls'); my($iR, $iC, $oWkS, $oWkC); print "FILE :", $oBook->{File} , "\n"; print "COUNT :", $oBook->{SheetCount} , "\n"; print "AUTHOR:", $oBook->{Author} , "\n"; for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++) +{ $oWkS = $oBook->{Worksheet}[$iSheet]; print "--------- SHEET:", $oWkS->{Name}, "\n"; for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $i +R++) { for(my $iC = $oWkS->{MinCol} ; defined $oWkS->{MaxCol} && $iC <= $oWkS->{ +MaxCol} ; $iC++) { $oWkC = $oWkS->{Cells}[$iR][$iC]; print "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC) +; } } } # This helps make sure we're using the vars we mean to use use strict; # use DBI; # you'll need to add the dbi / oracle info +? my $oldrow = 0; my @field_names = qw(fieldname_one fieldname_two fieldname_three); my (@fields,@values); # my $dbh = DBI->connect(...,...,...); # my $sth; open DATA, "< myfilename" or die "myfilename: $!\n"; while(<DATA>) { print; chomp; # this puts the row number in $1, col number in $2 and data in $3, # and in the variable respectively my ($row,$col,$data) = /^\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)\s*=>(.*)$/; # if we've finished with last entry, enter it into database if (($oldrow != $row) && ($oldrow)) { print "Adding $oldrow\n"; if(@fields) { #make sure there is data my $query = 'INSERT INTO tablename (' . (join ',', @fields) . ' VALUES(' . (join ',', @values) . ')'; # ... do DB calls ... # $sth = $dbh->prepare($query) || die "prepare: $query"; # $sth = $dbh->execute || die "execute: $query"; # $sth->finish; @fields = @values = (); } $oldrow = $row; } push @fields, $field_names[$col]; # field name with proper data #push @values, db->quote($data); #run data through DB quoter push @values, $data; } # $dbh->close; close DATA;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Ovid) Re: Writing Spreadsheet parse to Database
by Ovid (Cardinal) on May 01, 2001 at 21:34 UTC | |
by tye (Sage) on May 01, 2001 at 22:02 UTC |