Perl從oracle導出百萬級數據到excel excel 2007 可以存放1048576行、16384列的數據;excel 2003大概是65535行 我從oracle中導出30萬行60列的數據到excel中花了約8分鐘。
如果你還沒有安裝perl,按下面步驟
步驟:
1、下載 並安裝perl v5.8.8,可以使用perl -v查看是否安裝成功,
2、輸入PPM命令打開perl的包管理器。找到並更新DBI,
2、下載 DBD-Oracle -1.17.zip解壓到c:/perl 在命令行進入該目錄執行: ppm install dbd-oracle.ppd 安裝DBD-oracle;
3、安裝 Excel 模塊,如果你能聯網,直接使用如下命令即可:
ppm install OLE::Storage_Lite
ppm install Spreadsheet::ParseExcel
ppm install Spreadsheet::WriteExcel4、將如下代碼保存到d:/test.pl,更改裏面的oracle的用戶名和ip地址,以及端口號,以及sql語句
5、在命令行進入d盤下執行:perl test.pl
代碼:
$ENV{NLS_LANG} = 'AMERICAN_AMERICA.ZHS16GBK';
use strict;
use DBI;
use Win32::OLE;
use POSIX qw(strftime);my $now_string=strftime "%Y-%m-%d-%H-%M-%S", localtime;my $ti_s =strftime "%S", localtime;
my $ti_m=strftime "%M", localtime;
my $ti_h=strftime "%H", localtime;
my $ti_old=$ti_s+($ti_m*60)+($ti_h*3600);
my $excel_file = "d://$now_string".".xlsx";
#在此修改你的sql
my $sql = "select * from table1";my($dbh,$sth,$row,$col,@field,$ele,$c_times,$residual,$cols,$cell_end);
unlink $excel_file if (-e $excel_file);
my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
my $Book = $Excel->Workbooks->add;
my $Sheet = $Book->Worksheets(1);
my @array_cols=("","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
#在此修改你的oracle的用戶名、密碼、ip、端口,
$dbh = DBI->connect('dbi:Oracle
:',q{scott/tiger@(DESCRIPTION=(ADDRESS = (PROTOCOL = TCP)(HOST = 10.XXX.XXX.XXX)(PORT = 1521)) (CONNECT_DATA=(SERVICE_NAME = test)))},"");$sth = $dbh->prepare($sql);
$sth->execute();
$row=1;
$cols=0;if (@field = $sth->fetchrow)
{ $cols=scalar(@field);
$c_times=int($cols/26);
$residual=$cols%26;
if ($cols<27)
{
$cell_end = $array_cols[$cols];
}
else
{
if ($residual == 0)
{
$cell_end = $array_cols[$c_times-1]."Z";
}
else
{
$cell_end = $array_cols[$c_times].$array_cols[$residual];
}
}
$Sheet->Range("A1:$cell_end$row")->{Value} = [@field];
while(@field = $sth->fetchrow)
{
$row++;
print ("正在導出第 $row 條記錄!");
print ("/n");
$Sheet->Range("A$row:$cell_end$row")->{Value} = [@field];
}
$sth->finish();
$dbh->disconnect();
$Book->SaveAs($excel_file);
}
undef($Sheet);
undef($Book);
undef($Excel);my $now_string1=strftime "%Y-%m-%d-%H-%M-%S", localtime;
my $ti_s1 =strftime "%S", localtime;
my $ti_m1=strftime "%M", localtime;
my $ti_h1=strftime "%H", localtime;
my $ti_new=$ti_s1+($ti_m1*60)+($ti_h1*3600);print "************************************************************************/n/n";
print"導出完成!開始時間:$now_string./t結束時間:$now_string1 /n共耗時:/t";
print $ti_new-$ti_old;
print ("秒/n/n/n");
print "************************************************************************/n/n";
use Term::ReadKey;
$| = 1;
print "請按任意鍵退出...";
<STDIN>;
ReadMode 4; # Turn off controls keys
while (! defined ReadKey(-1)) {}
ReadMode 0; # Reset tty mode
$| = 0;