我遇到了一个非常简单的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();
答案 0 :(得分:4)
问题在于:
foreach my $stmt (@data)
{
$worksheet->write($row++, @data); # !!
last;
}
write()
的正确语法是:
write($row, $column, $token, $format)
您缺少$column
参数,在这种情况下可能为0。
如果$stmt
是数组引用,那么您可以按如下方式一次编写它:
$worksheet->write($row++, 0, $stmt);
答案 1 :(得分:3)
我猜它会以字符串形式出现,当你试图将它插入日期列时,它没有隐式转换。
尝试选择这样的日期,然后将其转换为可用于比较的字符。
to_char(date, 'YYYY/MM/DD HH24:MI:SS')
然后
to_date(date, 'YYYY/MM/DD HH24:MI:SS')
将其转换回插入日期。这通常是您在SQL中需要做的事情。
我记得,perl为DBI提供了一个跟踪工具,可以更好地了解正在发生的事情。