用Perl從oracle匯出百萬級資料到excel
用Perl從oracle匯出百萬級資料到excel[@more@]轉自:
http://blog.chinaunix.net/u3/90327/showart.php?id=2027719
用Perl從oracle匯出百萬級資料到excel
用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:',
= (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 "************************************************************************nn";
print"匯出完成!開始時間:$now_string.t結束時間:$now_string1 n共耗時:t";
print $ti_new-$ti_old;
print ("秒nnn");
print "************************************************************************nn";
use Term::ReadKey;
$| = 1;
print "請按任意鍵退出...";
;
ReadMode 4; # Turn off controls keys
while (! defined ReadKey(-1)) {}
ReadMode 0; # Reset tty mode
$| = 0;
http://blog.chinaunix.net/u3/90327/showart.php?id=2027719
用Perl從oracle匯出百萬級資料到excel
用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:',
= (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 "************************************************************************nn";
print"匯出完成!開始時間:$now_string.t結束時間:$now_string1 n共耗時:t";
print $ti_new-$ti_old;
print ("秒nnn");
print "************************************************************************nn";
use Term::ReadKey;
$| = 1;
print "請按任意鍵退出...";
ReadMode 4; # Turn off controls keys
while (! defined ReadKey(-1)) {}
ReadMode 0; # Reset tty mode
$| = 0;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22565551/viewspace-1027537/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C#快速匯出百萬級資料到Excel方法C#Excel
- 百萬級別資料Excel匯出優化Excel優化
- Vue匯出資料到Excel電子表格VueExcel
- 匯出百萬級資料
- Python批量匯入Excel資料到MySQLPythonExcelMySql
- SpringBoot圖文教程10—Excel模板匯出|百萬資料匯出|圖片匯出「easypoi」Spring BootExcel
- SQLServer匯出匯入資料到MySQLServerMySql
- 第四節:海量資料匯出到Excel、百萬Excel匯入到DB、秒級/分鐘級排行版Excel
- oracle 匯出excel 格式整改OracleExcel
- Python 基於 xlsxwriter 實現百萬資料匯出 excelPythonExcel
- Python匯出資料到Excel表格-NotImplementedError: formatting_info=True not yet implementedPythonExcelErrorORM
- Sqoop匯出ClickHouse資料到HiveOOPHive
- 用php把資料匯出excelPHPExcel
- Oracle 資料匯入ExcelOracleExcel
- 使用Excel匯入資料到SAP Cloud for Customer系統ExcelCloud
- 利用Excel匯入資料到SAP C4CExcel
- 如何批次匯出地圖上商家店鋪資料到手機電腦excel表格地圖Excel
- Java 匯入資料到Excel並提供檔案下載介面JavaExcel
- excel導資料到PostgresqlExcelSQL
- VUE中使用vue-json-excel超級方便匯出excel表格資料VueJSONExcel
- 避免 OOM,高效匯出百萬級資料的 SpringBoot 實現方案OOMSpring Boot
- waterdrop匯出hdfs資料到clickhouse(text,csv,json)JSON
- Oracle資料匯入匯出Oracle
- Oracle 資料匯入匯出Oracle
- EasyPoi, Excel資料的匯入匯出Excel
- 匯出excelExcel
- Oracle使用RMAN從Windows遷移資料到LinuxOracleWindowsLinux
- 從資料到視覺化:看板工具讓Excel更出色視覺化Excel
- 使用Oracle SQL Developer匯入Excel資料OracleSQLDeveloperExcel
- 利用跳板機連線mysql,匯出資料到csvMySql
- EasyExcel處理Mysql百萬資料的匯入匯出案例,秒級效率,拿來即用!ExcelMySql
- oracle使用儲存過程將表資料以excel格式匯出Oracle儲存過程Excel
- vue excel匯入匯出VueExcel
- PHP 匯出 ExcelPHPExcel
- PHP匯出EXCELPHPExcel
- Java匯出ExcelJavaExcel
- Vue匯出ExcelVueExcel
- Perl Catalyst 倒出 ExcelExcel
- vue匯出excel資料表格功能VueExcel