Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Oracle to Excel

by cocl04 (Sexton)
on Nov 06, 2008 at 16:48 UTC ( [id://722041]=perlquestion: print w/replies, xml ) Need Help??

cocl04 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I am having trouble with a very simple Perl process. I am basically q +uerying an oracle database and I want to load it into excel. I have +been able to use DBIx::Dump and it works. However, I need to be able + to use a variety of excel formatting tools. And I think Spreadsheet +::Writeexcel is the best module that outputs to excel that allows me +do more formatting. Below is the code and the error I am getting. I + basically query oracle, fetch the data, load into an array and try t +o write to excel. For some reason it is doing some kind of compare a +nd it does not like the data types. For example, the date is '25-OCT +-08'. The SVP is 'S01'. It seems to be saying that they are not num +eric. I know you are busy, but can you please help me with this prob +lem. Any help will be greatly appreciated. Once I can get the data +into a spreadsheet, I can play around with the automation and excel f +ormatting. Thanks you!
Error says: "Argument "01-NOV-08" isn't numeric in numeric ge <>=> at +C:/Perl/site/lib/Spreadsheet/WriteExcel/Worksheet.pm line 3414. Argument "01-NOV-08" isn't numeric in pack ge <>=> ge <>=> at C:/Perl/ +site/lib/Spreadsheet/WriteExcel/Worksheet.pm line 2157.
#!/usr/bin/perl -w #Set the Perl Modules use strict; use DBI; use Spreadsheet::WriteExcel; # Connect to the oracle database my $dbh = DBI->connect( 'dbi:Oracle:xxxx', 'xxxx', 'xxxx', ) || die "Database connection not made: $DBI::er +rstr"; #Set up Query my $stmt = "select week_end_date, SVP, RD, DM, store, wtd_smrr_gain,QTD_SMRR_ +GAIN, wtd_bor_gain,QTD_BOR_GAIN, wtd_cust_gain,QTD_CUST_GAIN, wtd_CARD_CLOSED_OCT25,QTD_AVG_CARD_ +CL from bonus_4Q_store order by store"; #Prepare Query my $sth = $dbh->prepare($stmt); #Execute Query $sth->execute() or die $dbh->errstr; my( $week_end_date,$SVP,$RD,$DM,$store, $wtd_smrr_gain,$QTD_SMRR_GAIN, $wtd_bor_gain,$QTD_BOR_GAIN, $wtd_cust_gain,$QTD_CUST_GAIN, $wtd_CARD_CLOSED_OCT25,$QTD_AVG_CARD_CL); #binds each column to a scalar reference $sth->bind_columns(undef,\$week_end_date,\$SVP,\$RD,\$DM,\$store, \$wtd_smrr_gain,\$QTD_SMRR_GAIN, \$wtd_bor_gain,\$QTD_BOR_GAIN, \$wtd_cust_gain,\$QTD_CUST_GAIN, \$wtd_CARD_CLOSED_OCT25,\$QTD_AVG_CARD_CL,); #create a new instance my $Excelfile = "/Test_Report.xls"; my $excel = Spreadsheet::WriteExcel->new("$Excelfile"); my $worksheet = $excel->addworksheet("WOW_SHEET"); #Create array shell my @data; #Call data and Write to Excel while ( @data = $sth->fetchrow_array()){ my $week_end_date = $data[0]; my $SVP = $data[1]; my $RD = $data[2]; my $DM = $data[3]; my $store = $data[1]; my $wtd_smrr_gain = $data[2]; my $QTD_SMRR_GAIN = $data[3]; my $wtd_bor_gain = $data[4]; my $QTD_BOR_GAIN = $data[5]; my $wtd_cust_gain = $data[6]; my $QTD_CUST_GAIN = $data[7]; my $wtd_CARD_CLOSED_OCT25 = $data[8]; my $QTD_AVG_CARD_CL = $data[9]; my $row = 0; my $col = 0; foreach my $stmt (@data) { $worksheet->write($row++, @data); last; } } print "DONE \n"; $sth->finish(); $dbh->disconnect();

Replies are listed 'Best First'.
Re: Oracle to Excel
by Corion (Patriarch) on Nov 06, 2008 at 16:51 UTC
Re: Oracle to Excel
by jmcnamara (Monsignor) on Nov 06, 2008 at 22:50 UTC
    The problem is here:
    foreach my $stmt (@data) { $worksheet->write($row++, @data); # !! last; }
    The correct syntax for write() is:
    write($row, $column, $token, $format)
    You are missing the $column argument, which in this case is probably 0. If $stmt is an array ref then you can write it in one go as follows:
    $worksheet->write($row++, 0, $stmt);

    --
    John.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 18:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found