扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
图1 监听器与extproc进程 |
LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = CHANET)(PORT = 1521)) ) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = extproc1)) ) ) ) SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = PLSExtProc) (ORACLE_HOME = D:\oracle\ora92) (PROGRAM = extproc) ) (SID_DESC = (GLOBAL_DBNAME = ORADB) (ORACLE_HOME = D:\oracle\ora92) (SID_NAME = ORADB) ) ) |
EXTPROC_CONNECTION_DATA = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1)) ) (CONNECT_DATA = (SID = PLSExtProc) (PRESENTATION = RO) ) ) |
C:\>tnsping EXTPROC_CONNECTION_DATA TNS Ping Utility for 32-bit Windows: Version 9.2.0.1.0 - Production on 07-4月 -2 005 16:57:00 Copyright (c) 1997 Oracle Corporation. All rights reserved. |
D:\oracle\ora92\network\admin\sqlnet.ora |
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC) (KEY = EXTPROC1))) (CONNECT_DATA = (SID = PLSExtProc) (PRESENTATION = RO))) OK(30毫秒) |
D:\oracle\ora92\bin>extproc Oracle Corporation --- 星期四 4月 07 2005 17:37:18.968 Heterogeneous Agent Release 9.2.0.1.0 - Production |
图2 操作COM对象工作原理 |
SQL> CONNECT SYSTEM/chanet@oradb SQL> CREATE USER chanet identified by chanet; SQL> GRANT CREATE LIBRARY TO chanet; SQL> CONNECT chanet/chanet@oradb; SQL> @D:\oracle\ora92\com\comwrap.sql; |
STARTUP_WAIT_TIME_LISTENER = 0 CONNECT_TIMEOUT_LISTENER = 10 TRACE_LEVEL_LISTENER = off PASSWORDS_LINTENER = (oracle) |
PL/SQL数据类型 | COM API 数据类型 |
VARCHAR2 | BSTR |
BOOLEAN | BOOL |
BINARY_INTEGER | BYTE,INT,LONG |
DOUBLE PRECISION | DOUBLE,FLOAT,CURRENCY |
DATE | DATE |
名称 | 功能描述 |
CreateObject | 创建对象 |
DestroyObject | 关闭对象 |
GetLastError | 获取错误信息 |
GetProperty | 获取属性值 |
SetProperty | 设置属性值 |
InitArg | 为Invoke函数初始化参数 |
InitOutArg | 为GetArg初始化输出参数 |
GetArg | 获取输出参数 |
SetArg | 为Invoke函数设置参数 |
Invoke | 调用COM对象的函数或过程 |
CREATE TABLE IT_SALE_TAB(ITS_ID VARCHAR2(5),ITS_DATE DATE,ITS_TOTAL NUMBER); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’1’,TO_DATE(’2004-01-01’,’YYYY-MM-DD’),250); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’2’,TO_DATE(’2004-02-01’,’YYYY-MM-DD’),150); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’3’,TO_DATE(’2004-03-01’,’YYYY-MM-DD’),80); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’4’,TO_DATE(’2004-04-01’,’YYYY-MM-DD’),96); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’5’,TO_DATE(’2004-05-01’,’YYYY-MM-DD’),300); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’6’,TO_DATE(’2004-06-01’,’YYYY-MM-DD’),210); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’7’,TO_DATE(’2004-07-01’,’YYYY-MM-DD’),320); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’8’,TO_DATE(’2004-08-01’,’YYYY-MM-DD’),280); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’9’,TO_DATE(’2004-09-01’,’YYYY-MM-DD’),276); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’10’,TO_DATE(’2004-10-01’,’YYYY-MM-DD’),368); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’11’,TO_DATE(’2004-11-01’,’YYYY-MM-DD’),163); INSERT INTO IT_SALE_TAB(ITS_ID,ITS_DATE,ITS_TOTAL) VALUES(’12’,TO_DATE(’2004-12-01’,’YYYY-MM-DD’),305); COMMIT; |
CREATE OR REPLACE PACKAGE oraExcel IS xlThin BINARY_INTEGER DEFAULT 2; DummyToken BINARY_INTEGER; applicationToken BINARY_INTEGER:=-1; -- Excel对象句柄 WorkBooksToken BINARY_INTEGER:=-1; -- 工作簿句柄 WorkBookToken BINARY_INTEGER:=-1; WorkSheetToken BINARY_INTEGER:=-1; -- 工作表句柄 WorkSheetToken1 BINARY_INTEGER:=-1; RangeToken BINARY_INTEGER:=-1; -- Range区句柄 ChartObjectToken BINARY_INTEGER:=-1; -- 图表对象句柄 ChartObject1 BINARY_INTEGER:=-1; Chart1Token BINARY_INTEGER:=-1; hLines BINARY_INTEGER:=-1; i BINARY_INTEGER; err_src VARCHAR2(255); err_desc VARCHAR2(255); err_hpf VARCHAR2(255); err_hpID BINARY_INTEGER; -- 创建Excel对象 FUNCTION CreateExcelWorkSheet(servername VARCHAR2) RETURN BINARY_INTEGER; -- 插入数据(字符型) FUNCTION setCellValues(RANGE VARCHAR2,data VARCHAR2,TYPE VARCHAR2) RETURN BINARY_INTEGER; -- 插入数据(日期型) FUNCTION setCellValues(RANGE VARCHAR2,data DATE,TYPE VARCHAR2) RETURN BINARY_INTEGER; -- 插入数据(整型) FUNCTION setCellValues(RANGE VARCHAR2,data BINARY_INTEGER,TYPE VARCHAR2) RETURN BINARY_INTEGER; -- 插入数据(实型) FUNCTION setCellValuesReal(RANGE VARCHAR2,data DOUBLE PRECISION,TYPE VARCHAR2) RETURN BINARY_INTEGER; -- 设置列宽 FUNCTION setCellColWidth(RANGE VARCHAR2,width DOUBLE PRECISION,TYPE VARCHAR2) RETURN BINARY_INTEGER; -- 设置表格 FUNCTION setCellLines(RANGE VARCHAR2,BordersIndex BINARY_INTEGER,weight BINARY_INTEGER DEFAULT xlThin,TYPE VARCHAR2) RETURN BINARY_INTEGER; -- 合并单元格 FUNCTION setCellMerge(RANGE VARCHAR2,bValues BOOLEAN,TYPE VARCHAR2) RETURN BINARY_INTEGER; -- 设置字体属性 FUNCTION setCellFont(RANGE VARCHAR2,Property VARCHAR2, fontValues BINARY_INTEGER,TYPE VARCHAR2) RETURN BINARY_INTEGER; -- 调用方法 FUNCTION callMethod(RANGE VARCHAR2,MethodName VARCHAR2) RETURN BINARY_INTEGER; -- 插入图表 FUNCTION InsertChart(xpos BINARY_INTEGER,ypos BINARY_INTEGER, width BINARY_INTEGER,height BINARY_INTEGER, RANGE VARCHAR2,TYPE VARCHAR2) RETURN BINARY_INTEGER; -- 保存文件 FUNCTION SaveToFile(filename VARCHAR2) RETURN BINARY_INTEGER; -- 关闭Excel对象 FUNCTION CloseExcel RETURN BINARY_INTEGER; END oraExcel; |
CREATE OR REPLACE PACKAGE BODY oraExcel IS FUNCTION CreateExcelWorkSheet(servername VARCHAR2) RETURN BINARY_INTEGER IS BEGIN -- 创建Excel对象 i:=ordcom.CreateObject(’Excel.Application’, 0, servername,applicationToken); IF (i!=0) THEN -- 创建失败,提示返回的错误信息 ordcom.GetLastError(err_src, err_desc, err_hpf, err_hpID); raise_application_error(-20000,err_src || err_desc || err_hpf || err_hpID); END IF; -- 通过程序对象句柄获取工作簿句柄 i:=ordcom.GetProperty(applicationToken, ’WorkBooks’, 0, WorkBooksToken); ordcom.InitArg(); ordcom.SetArg(-4167,’I4’); i:=ordcom.Invoke(WorkBooksToken, ’Add’, 1, WorkBookToken); ordcom.InitArg(); ordcom.SetArg(’Sheet 1’,’BSTR’); -- 获取工作表句柄 i:=ordcom.GetProperty(applicationToken, ’WorkSheets’, 0, WorkSheetToken1); i:=ordcom.Invoke(WorkSheetToken1, ’Add’, 0, WorkSheetToken); RETURN i; END CreateExcelWorkSheet; FUNCTION setCellValues(RANGE VARCHAR2,data VARCHAR2, TYPE VARCHAR2) RETURN BINARY_INTEGER IS BEGIN ordcom.InitArg(); ordcom.SetArg(RANGE,’BSTR’); -- 获取Range区句柄,之后将数据写入到指定的单元格 i:=ordcom.GetProperty(WorkSheetToken, ’Range’, 1, RangeToken); i:=ordcom.SetProperty(RangeToken, ’Value’, data, TYPE); i:=ordcom.DestroyObject(RangeToken); RETURN i; END setCellValues; FUNCTION setCellValues(RANGE VARCHAR2,data BINARY_INTEGER,TYPE VARCHAR2) RETURN BINARY_INTEGER IS BEGIN ordcom.InitArg(); ordcom.SetArg(RANGE, ’BSTR’); i:=ordcom.GetProperty(WorkSheetToken, ’Range’, 1, RangeToken); i:=ordcom.SetProperty(RangeToken, ’Value’, data, type); i:=ordcom.DestroyObject(RangeToken); RETURN i; END setCellValues; FUNCTION setCellValuesReal(RANGE VARCHAR2,data DOUBLE PRECISION,TYPE VARCHAR2) RETURN BINARY_INTEGER IS BEGIN ordcom.InitArg(); ordcom.SetArg(RANGE, ’BSTR’); i:=ordcom.GetProperty(WorkSheetToken, ’Range’, 1, RangeToken); i:=ordcom.SetProperty(RangeToken, ’Value’, data, type); i:=ordcom.DestroyObject(RangeToken); RETURN i; END setCellValuesReal; FUNCTION setCellValues(RANGE VARCHAR2,data DATE,TYPE VARCHAR2) RETURN BINARY_INTEGER IS BEGIN ordcom.InitArg(); ordcom.SetArg(RANGE, ’BSTR’); i:=ordcom.GetProperty(WorkSheetToken, ’Range’, 1, RangeToken); i:=ordcom.SetProperty(RangeToken, ’Value’, data, TYPE); i:=ordcom.DestroyObject(RangeToken); RETURN i; END setCellColWidth; FUNCTION setCellMerge(RANGE VARCHAR2,bValues BOOLEAN,TYPE VARCHAR2) RETURN i; END setCellValues; FUNCTION setCellColWidth(RANGE VARCHAR2,width DOUBLE PRECISION,TYPE VARCHAR2) RETURN BINARY_INTEGER IS BEGIN ordcom.InitArg(); ordcom.SetArg(RANGE,’BSTR’); i:=ordcom.GetProperty(WorkSheetToken,’Range’,1,RangeToken); i:=ordcom.SetProperty(RangeToken,’ColumnWidth’,width,TYPE); i:=ordcom.DestroyObject(RangeToken); RETURN BINARY_INTEGER IS BEGIN ordcom.InitArg(); ordcom.SetArg(RANGE,’BSTR’); i:=ordcom.GetProperty(WorkSheetToken,’Range’,1,RangeToken); i:=ordcom.SetProperty(RangeToken,’MergeCells’,bValues,’BOOLEAN’); i:=ordcom.DestroyObject(RangeToken); RETURN i; END setCellMerge; FUNCTION setCellLines(RANGE VARCHAR2,BordersIndex BINARY_INTEGER, weight BINARY_INTEGER DEFAULT xlThin,TYPE VARCHAR2) RETURN BINARY_INTEGER IS BEGIN ordcom.InitArg(); ordcom.SetArg(RANGE,’BSTR’); i:=ordcom.GetProperty(WorkSheetToken,’Range’,1,RangeToken); ordcom.InitArg(); ordcom.SetArg(BordersIndex,TYPE); -- 画表格的具体载入参数 i:=ordcom.GetProperty(RangeToken,’Borders’,1,hLines); i:=ordcom.SetProperty(hLines,’weight’,weight,TYPE); i:=ordcom.DestroyObject(hLines); i:=ordcom.DestroyObject(RangeToken); RETURN i; END setCellLines; FUNCTION setCellFont(RANGE VARCHAR2,Property VARCHAR2,fontValues BINARY_INTEGER,TYPE VARCHAR2) RETURN BINARY_INTEGER IS BEGIN ordcom.InitArg(); ordcom.SetArg(RANGE,’BSTR’); i:=ordcom.GetProperty(WorkSheetToken,’Range’,1,RangeToken); ordcom.InitArg(); ordcom.SetArg(Property,TYPE); ordcom.InitArg(); i:=ordcom.GetProperty(WorkSheetToken, ’ChartObjects’, 0, ChartObjectToken); ordcom.InitArg(); ordcom.SetArg(xpos,’I2’); -- 载入图表对象位置参数 ordcom.SetArg(ypos,’I2’); ordcom.SetArg(width,’I2’); ordcom.SetArg(height,’I2’); i:=ordcom.Invoke(ChartObjectToken, ’Add’, 4, ChartObject1); -- 添加图表 i:=ordcom.GetProperty(ChartObject1, ’Chart’, 0,Chart1Token); ordcom.InitArg(); ordcom.SetArg(RANGE, ’BSTR’); i:=ordcom.GetProperty(WorkSheetToken,’Range’, 1, RangeToken); -- 选取区域 ordcom.InitArg(); ordcom.SetArg(RangeToken, ’DISPATCH’); IF TYPE=’xlPie’ THEN charttype := -4102; ELSIF TYPE=’xl3DBar’ THEN charttype := -4099; ELSIF TYPE=’xlBar’ THEN charttype := 2; ELSIF TYPE=’xl3dLine’ THEN i:=ordcom.GetProperty(RangeToken,’Font’,0,hLines); -- 获取字体对象 i:=ordcom.SetProperty(hLines,Property,fontValues,TYPE); i:=ordcom.DestroyObject(hLines); i:=ordcom.DestroyObject(RangeToken); RETURN i; END; FUNCTION callMethod(RANGE VARCHAR2,MethodName VARCHAR2) RETURN BINARY_INTEGER IS reti BINARY_INTEGER := -1; BEGIN ordcom.InitArg(); ordcom.SetArg(RANGE,’BSTR’); i:=ordcom.GetProperty(WorkSheetToken,’Range’,1,RangeToken); ordcom.InitArg(); i:=ordcom.Invoke(RangeToken,MethodName,0,reti); -- 调用对象的方法 i:=ordcom.DestroyObject(RangeToken); RETURN reti; END; FUNCTION InsertChart(xpos BINARY_INTEGER, ypos BINARY_INTEGER, width BINARY_INTEGER, height BINARY_INTEGER, RANGE VARCHAR2, TYPE VARCHAR2) RETURN BINARY_INTEGER IS charttype BINARY_INTEGER:= -4099; BEGIN charttype:= -4101; END IF; ordcom.SetArg(charttype,’I4’); i:=ordcom.Invoke(Chart1Token,’ChartWizard’, 2, DummyToken); i:=ordcom.DestroyObject(RangeToken); i:=ordcom.DestroyObject(ChartObjectToken); i:=ordcom.DestroyObject(ChartObject1); i:=ordcom.DestroyObject(Chart1Token); RETURN i; END InsertChart; FUNCTION SaveToFile(filename VARCHAR2) RETURN BINARY_INTEGER IS BEGIN ordcom.InitArg(); ordcom.SetArg(filename,’BSTR’); i:=ordcom.Invoke(WorkBookToken, ’SaveAs’, 1, DummyToken); -- 保存文件 IF (i!=0) THEN ordcom.GetLastError(err_src, err_desc, err_hpf, err_hpID); raise_application_error(-20000,err_src || err_desc || err_hpf || err_hpID); END IF; RETURN i; END SaveToFile; FUNCTION CloseExcel RETURN BINARY_INTEGER IS BEGIN ordcom.InitArg(); ordcom.InitArg(); ordcom.SetArg(FALSE,’BOOL’); i:=ordcom.Invoke(WorkBookToken, ’Close’, 0, DummyToken); i:=ordcom.DestroyObject(WorkBookToken); ordcom.InitArg(); i:=ordcom.Invoke(WorkBooksToken, ’Close’, 0, DummyToken); i:=ordcom.DestroyObject(WorkBooksToken); i:=ordcom.Invoke(applicationToken, ’Quit’, 0, DummyToken); -- 关闭所有句柄 i:=ordcom.DestroyObject(WorkSheetToken); i:=ordcom.DestroyObject(WorkSheetToken1); i:=ordcom.DestroyObject(applicationToken); i:=ordcom.DestroyObject(ChartObjectToken); i:=ordcom.DestroyObject(Chart1Token); i:=ordcom.DestroyObject(hLines); i:=ordcom.DestroyObject(ChartObject1); i:=ordcom.DestroyObject(dummyToken); RETURN i; END CloseExcel; END oraExcel; |
CREATE OR REPLACE PROCEDURE dp_ToExcel IS CURSOR c1 IS SELECT ITS_ID,ITS_DATE,ITS_TOTAL FROM IT_SALE_TAB; n BINARY_INTEGER:=2; i BINARY_INTEGER; filename VARCHAR2(255); cellIndex VARCHAR2(40); cellValue VARCHAR2(40); cellColumn VARCHAR2(10); returnedTime VARCHAR2(20); currencyvalue DOUBLE PRECISION; dateValue DATE; xlThin BINARY_INTEGER:=2; xlEdgeLeft BINARY_INTEGER:=7; xlEdgeTop BINARY_INTEGER:=8; xlEdgeBottom BINARY_INTEGER:=9; xlEdgeRight BINARY_INTEGER:=10; xlInsideVertical BINARY_INTEGER:=11; xlInsideHorizontal BINARY_INTEGER:=12; BEGIN i:=oraExcel.CreateExcelWorkSheet(’’); i:=oraExcel.setCellValues(’A2’, ’序号’, ’BSTR’); i:=oraExcel.setCellValues(’B2’, ’日期’, ’BSTR’); i:=oraExcel.setCellValues(’C2’, ’销售’, ’BSTR’); cellValue:=c1_rec.ITS_TOTAL; currencyValue:=cellValue; i:=oraExcel.setCellValuesReal(cellIndex, currencyValue, ’CY’); n:=n+1; END LOOP; i:=oraExcel.setCellValues(’A’||n,’合计’,’BSTR’); i:=oraExcel.setCellValues(’C’||n,’=SUM(C3:C’||to_char(n-1)||’)’,’BSTR’); -- 画表格 i:=oraExcel.setCellLines(’A1:C’||n,xlEdgeLeft,xlThin,’I2’); i:=oraExcel.setCellLines(’A1:C’||n,xlEdgeTop,xlThin,’I2’); i:=oraExcel.setCellLines(’A1:C’||n,xlEdgeBottom,xlThin,’I2’); i:=oraExcel.setCellLines(’A1:C’||n,xlEdgeRight,xlThin,’I2’); i:=oraExcel.setCellLines(’A1:C’||n,xlInsideVertical,xlThin,’I2’); i:=oraExcel.setCellLines(’A1:C’||n,xlInsideHorizontal,xlThin,’I2’); -- 设置字体属性 i:=oraExcel.setCellFont(’A1:C1’,’Size’,20,’I2’); i:=oraExcel.setCellFont(’A1:C1’,’Bold’,1,’I2’); i:=oraExcel.callMethod(’A1:C1’,’Merge’); -- 合并单元格 i:=oraExcel.setCellValues(’A1:C1’,’合计’,’BSTR’); -- 插入图表 i:=oraExcel.setCellColWidth(’B:B’, 12.75,’CY’); -- 设置列宽 i:=oraExcel.setCellColWidth(’C:C’, 12.75,’CY’); n:=3; For c1_rec IN c1 LOOP cellColumn:=TO_CHAR(n); cellIndex:= ’A’||cellColumn; cellValue:= TO_CHAR(c1_rec.ITS_ID); i:=oraExcel.setCellValues(cellIndex, cellValue, ’BSTR’); cellIndex:= ’B’ || cellColumn; dateValue:=c1_rec.ITS_DATE; i:=oraExcel.setCellValues(cellIndex, dateValue, ’DATE’); cellIndex:= ’C’ || cellColumn; i:=oraExcel.InsertChart(350,200,250,250,’C3:C’||TO_CHAR(n-1),’xlPie’); SELECT TO_CHAR(SYSDATE, ’HH24MISS’) INTO returnedTime FROM dual; filename:=’D:\testExcel’ || returnedTime || ’.xls’; i:=oraExcel.SaveToFile(filename); -- 保存文件 i:=oraExcel.closeExcel(); -- 关闭对象 END; |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。