ºìÁªLinuxÃÅ»§
Linux°ïÖú

OracleÖÐʹÓÃPL/SQL²Ù×÷COM¶ÔÏó

·¢²¼Ê±¼ä:2006-07-25 08:43:00À´Ô´:ºìÁª×÷Õß:bear10214
OracleÖÐʹÓÃPL/SQL²Ù×÷COM¶ÔÏó

¡¡¡¡PL/SQLÊÇÓÉOracle¹«Ë¾¶Ô±ê×¼SQL½øÐÐÀ©Õ¹£¬×¨ÓÃÓÚOracleÊý¾Ý¿âÖгÌÐòÉè¼ÆµÄרÓÃÓïÑÔ£¬ÊôµÚÈý´ú¹ý³Ìʽ³ÌÐòÉè¼ÆÓïÑÔ¡£´ÓOracle8¿ªÊ¼ÌṩÁËÖ±½Ó´ÓPL/SQLÖе÷ÓÃÍⲿCÓïÑÔ¹ý³Ì£¬ÔÊÐí¿ª·¢ÈËÔ±ÓÃPL/SQL½øÐÐʹÓÃCÓïÑÔ±àÖƵijÌÐòÄ£¿é¡£´ÓOracle8i¿ªÊ¼£¬ÓÖÒýÈëÁËJava³ÌÐò¡£

¡¡¡¡ ÔÚ±¾ÎÄÖÐÖ÷Òª½éÉÜÍⲿÀý³ÌµÄ»ù±¾Ô­ÀíÒÔ¼°Ê¹ÓÃÌõ¼þ£¬½éÉÜÈçºÎͨ¹ýÒýÓÃÍⲿÀý³ÌÀ´²Ù×÷WindowsÖеÄCOM¶ÔÏ󣬲¢×öÁËÒ»¸ö²Ù×÷Excel¶ÔÏóµÄʾÀý¡£

¡¡¡¡ ±¾ÎĵÄÔËÐл·¾³È«²¿½¨Á¢ÔÚOracle9iºÍWindows2000¡£ÆäÖÐORACLEµÄ°²×°Ä¿Â¼£¨ORACLE_HOME£©ÎªD:\oracle\ora92£¬SIDΪORADB£¬Ö÷»úÃûΪCHANET¡£

¡¡¡¡±ØÒªÐÔ

¡¡¡¡ À©Õ¹ºóµÄPL/SQLÓïÑÔÒѾ­¼¯³ÉÁ˱ê×¼SQL£¬ÔÚЧÂʺͰ²È«ÉϷdz£ÊʺÏÉè¼ÆOracleÊý¾Ý¿â³ÌÐò£¬µ«¶ÔÓÚÓ¦ÓõÄijЩ¹¦ÄÜ£¬ÆäËüµÄ³ÌÐòÉè¼ÆÓïÑÔ±ÈPL/SQL¸üÊʺϣ¬È磺ʹÓòÙ×÷ϵͳ×ÊÔ´£¬CÓïÑÔÔÚ¼ÆËãºÍÒýÓÃϵͳ¶ÔÏó¼°Ê¹ÓÃÉ豸ÉÏÓÅÓÚPL/SQL£¬¶øJavaÓïÑÔÔÚÍøÂçÉϵÄÓ¦ÓÃÓÅÓÚPL/SQL¡£

¡¡¡¡ Èç¹ûÔÚÓ¦ÓÃÉÏÒªÓõ½²»ÊʺÏÓÃPL/SQLÓïÑԵĻ°£¬Õâʱ¾ÍҪʹÓÃÆäËüÓïÑÔ½øÐбàÖÆ£¬È»ºóÓÉPL/SQL×÷ΪÍⲿÀý³Ì½øÐе÷Óá£

¡¡¡¡ ÔÚOracle8֮ǰµÄ°æ±¾£¬PL/SQLºÍÆäËüÓïÑÔµÄΨһͨÐÅÊǽèÖúÓÚDBMS_PIPEºÍDBMS_ALERT°üÀ´ÊµÏÖ£¬ÔÚʹÓÃ֮ǰ±ØÐ轨Á¢Ò»¸öOCI½Ó¿Ú»òÔ¤±àÒëÆ÷±àÖƵļ໤³ÌÐò£¬Ê¹ÓñȽϸ´ÔÓ¡£ÍⲿÀý³ÌµÄ³öÏÖ£¬Ö»ÐèÔÚPL/SQLÖн¨Á¢Ò»¸öº¯ÊýÓ³Éäµ½ÍⲿÀý³Ì¶ÔÓ¦µÄº¯Êý£¬¾ÍÏñÆÕͨµÄPL/SQLº¯ÊýʹÓÃÒ»Ñù£¬¼ò»¯ÁËʹÓùý³Ì¡£

¡¡¡¡»ù±¾Ô­Àí

¡¡¡¡ µ±ÒýÓÃÍⲿCÓïÑÔÀý³Ìʱ£¬Oracle¼àÌýÆ÷½«Æô¶¯extproc½ø³Ì£¬¸Ã½ø³Ì½«»á¶¯Ì¬µØÔØÈë¹²Ïí¿â£¨ÔÚWindowsϳÆΪ¶¯Ì¬Á´½Ó¿â£¬¼´ÊÇDLLÎļþ£©£¬½ø³ÌÆðÁËÒ»¸ö»º³åµÄ×÷Ó㬵±PL/SQL¹ý³Ìµ÷ÓÃÍⲿº¯Êýʱ£¬½ø³Ì°ÑÃüÁî·¢Ë͵½¹²Ïí¿â£¬Ö®ºó°Ñ½á¹û·µ»Ø¸øPL/SQL¹ý³Ì¡£

¡¡¡¡ ½ø³Ì±»µ÷ÓúóËæ׏²Ïí¿âµÄʹÓûỰ£¨session£©¶ø´æÔÚ£¬Èç¹ûµ÷ÓÃÍê±Ï»òÕ߹رÕÊý¾Ý¿âÓû§»á»°£¬extproc½ø³Ì»á×Ô¶¯¹Ø±Õ¡£

¡¡¡¡ ÈçÏÂͼ1-1Ϊµ÷ÓÃÍⲿÀý³ÌµÄÃèÊö¡£


ͼ1 ¼àÌýÆ÷Óëextproc½ø³Ì
¡¡¡¡Ê¹ÓÃÅäÖÃ

¡¡¡¡ ÔÚµ÷ÓÃÍⲿÀý³Ì֮ǰ£¬±ØÐè½øÐÐÈçÏÂÉèÖãº

¡¡¡¡ ?ÅäÖüàÌýÆ÷¡£

¡¡¡¡ ?ÅäÖÃNet×é¼þ·þÎñ¡£

¡¡¡¡ ÅäÖüàÌýÆ÷£¬´ò¿ªD:\oracle\ora92\network\admin\listener.ora Îļþ£¬ÐÞ¸ÄÎļþ²ÎÊý¡£

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)
)
)
¡¡¡¡ ÆäÖÐÓÐÁ½²¿·Ý²ÎÊý¶ÔÓÚʹÓÃÍⲿÀý³ÌÊǺÜÖØÒªµÄ¡£

¡¡¡¡ ?(ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = extproc1))

¡¡¡¡ ÉèÖûùÓÚIPCЭÒéµÄÍⲿÀý³Ì

¡¡¡¡ ?(SID_DESC =(SID_NAME = PLSExtProc) (ORACLE_HOME = D:\oracle\ora92) (PROGRAM = extproc) )

¡¡¡¡ ¼Ç¼Êý¾Ý¿âµÄÏà¹ØÊôÐÔ£¬SID_NAMEÔÚĬÈϵÄÇé¿öÏÂÊÇPLSExtproc¡£

¡¡¡¡ ÅäÖÃNet×é¼þ·þÎñ£¬´ò¿ªD:\oracle\ora92\network\admin\tnsnames.oraÎļþ£¬°ÑÈçÏÂÄÚÈݱ£´æÔÚ¸ÃÎļþÀï¡£

EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
¡¡ (ADDRESS_LIST =
¡¡¡¡ (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
¡¡ )
¡¡ (CONNECT_DATA =
¡¡¡¡ (SID = PLSExtProc)
¡¡¡¡ (PRESENTATION = RO)
¡¡ )
)
¡¡¡¡ ÖØÒª²ÎÊý˵Ã÷£º

¡¡¡¡¡¡ ?(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))

¡¡¡¡¡¡ ?(CONNECT_DATA = (SID = PLSExtProc) (PRESENTATION = RO)

¡¡¡¡ Á½ÉèÖñØÐèÓëlistener.oraÀïµÄÒ»Ö¡£

¡¡¡¡ ÖØÆô¼àÌýÆ÷£¬²¢²âÊÔ·þÎñÊÇ·ñ¿ÉÓá£

¡¡¡¡ Í£Ö¹¼àÌýÆ÷£ºlsnrctl stop

¡¡¡¡ Æô¶¯¼àÌýÆ÷£ºlsnrctl start

¡¡¡¡ ²âÊÔ·þÎñÊÇ·ñ¿ÉÓãº

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.
¡¡¡¡ ÒÑʹÓõIJÎÊýÎļþ:

D:\oracle\ora92\network\admin\sqlnet.ora
¡¡¡¡ ÒÑʹÓà TNSNAMES ÊÊÅäÆ÷À´½âÎö±ðÃû

Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)
(KEY = EXTPROC1))) (CONNECT_DATA = (SID = PLSExtProc) (PRESENTATION = RO)))
OK£¨30ºÁÃ룩
¡¡¡¡ ²âÊÔextproc½ø³ÌÊÇ·ñÕý³££º

D:\oracle\ora92\bin>extproc
Oracle Corporation --- ÐÇÆÚËÄ 4ÔÂ 07 2005 17:37:18.968
Heterogeneous Agent Release 9.2.0.1.0 - Production
¡¡¡¡ 4¡¢Ê¹ÓÃCOM¶ÔÏó˵Ã÷

¡¡¡¡ COM¶ÔÏóÉè¼Æ¶¼»áÌṩÁËÈý¸ö»ù±¾²Ù×÷¸ø¿ª·¢ÈËԱʹÓ㬷ֱðΪ£º»ñÈ¡ÊôÐÔÖµ£¬ÉèÖÃÊôÐÔÖµ£¨Ö»¶ÁÊôÐÔ³ýÍ⣩£¬µ÷Ó÷½·¨¡£OracleÊý¾Ý¿âÔÚWindowsµÄƽ̨ÏÂÌṩÁ˲Ù×÷COM¶ÔÏóµÄ½Ó¿Ú£¬ÊôÓÚCÓïÑÔÍⲿÀý³Ìģʽ¡£

¡¡¡¡ ¹¤×÷Ô­ÀíÈçÏÂͼ£º



ͼ2 ²Ù×÷COM¶ÔÏó¹¤×÷Ô­Àí

¡¡¡¡ ÔÚʹÓøù¦ÄÜ֮ǰ£¬ÐèÃ÷°×ÈçÏÂÄÚÈÝ£º

¡¡¡¡ 1) ´´½¨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;
¡¡¡¡ 2)ÅäÖüàÌýÆ÷¡£

¡¡¡¡ ÔÚlistener.oraÎļþÀÌí¼ÓÈçÏÂÄÚÈÝ£¬²¢ÖØÆô¼àÌýÆ÷¡£

STARTUP_WAIT_TIME_LISTENER = 0
CONNECT_TIMEOUT_LISTENER = 10
TRACE_LEVEL_LISTENER = off
PASSWORDS_LINTENER = (oracle)
¡¡¡¡ 3) PL/SQLÊý¾ÝÀàÐÍÓë¶ÔÓ¦µÄCOM¶ÔÏóÀàÐÍ

¡¡¡¡ ±í1-1Êý¾ÝÀàÐͱȽÏ

PL/SQLÊý¾ÝÀàÐÍ COM API Êý¾ÝÀàÐÍ
VARCHAR2 BSTR
BOOLEAN BOOL
BINARY_INTEGER BYTE£¬INT£¬LONG
DOUBLE PRECISION DOUBLE£¬FLOAT£¬CURRENCY
DATE DATE

¡¡¡¡ 4) º¯Êý˵Ã÷¡£

¡¡¡¡ ±í1-2º¯Êý˵Ã÷

Ãû³Æ ¹¦ÄÜÃèÊö
CreateObject ´´½¨¶ÔÏó
DestroyObject ¹Ø±Õ¶ÔÏó
GetLastError »ñÈ¡´íÎóÐÅÏ¢
GetProperty »ñÈ¡ÊôÐÔÖµ
SetProperty ÉèÖÃÊôÐÔÖµ
InitArg ΪInvokeº¯Êý³õʼ»¯²ÎÊý
InitOutArg ΪGetArg³õʼ»¯Êä³ö²ÎÊý
GetArg »ñÈ¡Êä³ö²ÎÊý
SetArg ΪInvokeº¯ÊýÉèÖòÎÊý
Invoke µ÷ÓÃCOM¶ÔÏóµÄº¯Êý»ò¹ý³Ì



¡¡¡¡ 5) ²Ù×÷ExcelʾÀý

¡¡¡¡ ±¾½Ú½éÉÜÈçºÎ²Ù×÷COM¶ÔÏó£¨ÒÔExcel¶ÔÏóΪÀý£©£¬½«Êý¾Ý¿âÀïµÄ±í¼Ç¼ͨ¹ýPL/SQLÓï¾äÊä³ö³ÉExcelÎļþ¡£Í¨¹ý¶ÔÏóä¯ÀÀÆ÷¿ÉÒԲ鿴Excel¶ÔÏóÌṩµÄÊôÐԺͷ½·¨¡££¨È磺ÔÚExcel²Ëµ¥£¬¹¤¾ß -> ºê -> ´ò¿ªVisual Basic±à¼­Æ÷£¬Ôڱ༭Æ÷ÀÊÓͼ -> ¶ÔÏóä¯ÀÀÆ÷£©¡£

¡¡¡¡ ʹÓÃExcel¶ÔÏóÒ»°ãÕë¶Ôµ¥Ôª¸ñ½øÐвÙ×÷£¨È磬ÉèÖõÚÒ»¸öµ¥Ôª¸ñ×ÖÌå´óСµÄ´úÂëΪ£ºRange("A1").Font.Size = 20£©¡£¶ÔÓ¦µÄCOMÍⲿÀý³Ì²Ù×÷µÄ²½ÖèΪ£º

¡¡¡¡ 1¡¢»ñÈ¡³ÌÐò¾ä±ú£»

¡¡¡¡ 2¡¢»ñÈ¡¹¤×÷²¾¾ä±ú£»

¡¡¡¡ 3¡¢»ñÈ¡¹¤×÷±í¾ä±ú£»

¡¡¡¡ 4¡¢»ñÈ¡RangeÇø¾ä±ú£»

¡¡¡¡ 5¡¢»ñÈ¡FontÀà¾ä±ú£»

¡¡¡¡ 6¡¢ÉèÖÃSizeÊôÐÔ¡£

¡¡¡¡ ÈçÏÂΪ²Ù×÷Excel¶ÔÏóµÄÀý×Ó£¬Ê×ÏȽ¨Á¢Ê¾Àý±í£¬È»ºó½¨Á¢²Ù×÷°ü£¨package£©£¬×îºóÊÇʹÓðüº¯ÊýµÄ¹ý³Ì£¨procedure£©¡£´´½¨Ò»¸ö²âÊÔÓÃ±í£º

¡¡¡¡ -- ÏúÊÛ±í£¨½Å±¾£©

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;
¡¡¡¡ ²Ù×÷Excel¶ÔÏó°ü£¨Package£©¡£

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;
¡¡¡¡ Êý¾Ý°üÌåÄÚÈÝ£¨PACKAGE BODY£©

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;
¡¡¡¡ ³É¹¦´´½¨oraExcel°üºó£¬×îºó´´½¨Ê¹ÓðüµÄ´æ´¢¹ý³Ì£¬ÊµÏÖ°ÑÊý¾Ý±íÀïµÄ×Ö·ûÐÍ¡¢ÈÕÆÚÐͺÍÊýÖµÐͷֱ𴫵½Excel¹¤×÷±íÀ¶ÔÊýÖµÐÍÊý¾Ý½øÐÐͳ¼ÆºÍʹÓÃExcelÖеÄͼ±í¡£¸Ã¹ý³Ì¾ßÌå²Ù×÷²½ÖèΪ£º

¡¡¡¡ ?´´½¨Excel¶ÔÏó¡£

¡¡¡¡ ?½¨Á¢±íÍ·£¬ÉèÖÃÁÐ¿í¡£

¡¡¡¡ ?½«ÓαêÊý¾Ý´«µ½¹¤×÷±í¡£

¡¡¡¡ ?»­±í¸ñ¡£

¡¡¡¡ ?ÉèÖÃ×ÖÌåÊôÐÔ¡£

¡¡¡¡ ?²åÈëͼ±í¡£

¡¡¡¡ ?±£´æΪExcel¸ñʽÎļþ£¬¹Ø±ÕExcel¶ÔÏó¡£

¡¡¡¡ ÈçÏÂΪdp_toExcel´æ´¢¹ý³Ì£º

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;
¡¡¡¡Ð¡½á

¡¡¡¡ ±¾ÎĽéÉÜÈçºÎ´ÓPL/SQLÖÐÖ±½Óµ÷ÓÃC³ÌÐòµÄÍⲿÀý³Ì£¬²¢ÒÔ²Ù×÷Excel¶ÔÏóΪʾÀý¡£ÔÚµ÷ÓøÃÍⲿÀý³Ìʱ£¬ÓÐÈçÏÂÏà¹ØµÄÏÞÖÆ£º

¡¡¡¡ ?²Ù×÷ϵͳ±ØÐèÖ§³Ö¶¯Ì¬Á´½Ó¿â£¨DLL£©ºÍ¹²Ïí¿â¹¦ÄÜ¡£

¡¡¡¡ ?¼àÌýÆ÷ºÍextproc½ø³Ì±ØÐëÔËÐÐÔÚÊý¾Ý¿âËùÔÚµÄͬһ̨»ú×ÓÉÏ£¬²»Ö§³ÖÔ¶³ÌÊý¾Ý¿â¡£

¡¡¡¡ ?extprocΨһ֧³ÖµÄÊÇCÀý³Ì£¬µ«¿ÉÒÔͨ¹ýÏȵ÷ÓÃCÍⲿÀý³ÌȥʹÓÃÆäËü¶ÔÏó£¨È磺COM¶ÔÏ󣩡£

¡¡¡¡ ³ýÁËÏÞÖÆÍ⣬µ÷ÓÃÍⲿÀý³Ì»áÒýÆð¶îÍâµÄϵͳ×ÊÔ´¿ªÏú£¬ÔÚʹÓÃÍⲿÀý³Ì֮ǰҪ¿¼ÂÇÊÇ·ñÒ»¶¨ÒªÓõ½ÍⲿÀý³Ì¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ