1

我遇到了一个非常简单的 Perl 进程的问题。我基本上是在查询一个 Oracle 数据库,我想将它加载到 Excel 中。我已经能够使用 DBIx::Dump 并且它有效。但是,我需要能够使用各种 Excel 格式化工具。而且我认为Spreadsheet::WriteExcel是输出到 Excel 的最佳模块,它允许我进行更多格式化。

下面是代码和我得到的错误。我基本上是查询 Oracle,获取数据,加载到数组中并尝试写入 Excel。出于某种原因,它正在进行某种比较并且它不喜欢数据类型。例如,日期是“25-OCT-08”。SVP 是“S01”。似乎是在说它们不是数字。

错误:

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::errstr";


#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();
4

2 回答 2

4

问题在这里:

foreach my $stmt (@data) 
{ 
    $worksheet->write($row++, @data); # !!
    last; 
} 

的正确语法write()是:

write($row, $column, $token, $format)

您缺少$column参数,在这种情况下可能为 0。

如果$stmt是数组 ref,那么您可以一次性编写如下:

$worksheet->write($row++, 0, $stmt); 
于 2008-11-06T22:31:42.770 回答
3

我猜它是以字符串形式出现的,当您尝试将其插入日期列时,它没有隐式转换。

尝试像这样选择日期,它会将其转换为可用于比较的字符。

to_char(date, 'YYYY/MM/DD HH24:MI:SS')

然后

to_date(date, 'YYYY/MM/DD HH24:MI:SS') 

将其转换回插入日期。这通常是您需要在 SQL 中执行的操作。

我记得,perl 有一个 DBI 跟踪工具,可以更好地了解正在发生的事情。

于 2008-11-06T17:16:50.660 回答