http://blog.csdn.net/zcywset15/archive/2008/07/29/2727991.aspx
提到OLE&DOI,我们不得不先说说两者这间的关系。
OLE是SAP早期与微软Office集成的工具,而DOI是它的替代技术。在实际项目中使用到OLE&DOI的场合多是EXCEL表格的读入或是填制。有些需要自动画表格及填数据,有的只是要求在原有的模版上填制数据。根据不同的场合可以选用不同的技术。(有些公司用EXCEL的打印来替代FORM)
下面主要讲一下,OLE&DOI关于EXCEL的处理。
OLE实际上是类EXCEL中VBA的一种写法。有一种简单的方法,就是你可以在EXCEL中把要实现的动作录制成宏,然后根据宏的代码把它改成OLE。OLE同时也支持直接调用EXCEL宏程序(没有禁宏的前提下)。
DOI完全用类实现。特点是,你不用再去记那些繁琐的VBA语言了,DOI都给你包装成类的方法了。同时作为一个控件,已经集成在了SAP的GUI中了,你就可以在SAP的屏幕中写Office的东西了,不用再跳出一个窗口来了,减少一些误操作。当然DOI也是支持调用EXCEL宏程序的,前提是宏没有被禁。
两种方法在实际项目中都有使用,有时候就要看实施顾问擅长于那个了,当然本人还是推荐用新的,但也不要丢了老的(有时候要维护一些老程序或许用得着)。因为两种在技术上并没有什么难点,只有熟练度的差别。我可记不住那么多的方法和类名,一般常备两份模版两份手册在身,边写边查,在项目进度之内完工就OK。
我读取EXCEL还是比较简单的,这里不再讲了。既然,EXCEL的填制有自动画表格和填制模版之分,而自动画表格,显然就是什么都要自己写,工程量比较大,需要大量的调试(字体、表格宽度、颜色等),没有什么捷径,捧着手册慢慢写吧!对于填制模版,那自然要将模版上传到服务器,程序执行的时候再下载使用。我使用的一般有3中:
FTP
T-code SMW0
T-code OAOR
FTP顾名思义,就是把文件传到FTP上,然后在运行的时候,直接输入文件服务器的路径,把模版在本地打开填制。
T-code SMW0。实际上这是用于Web的一种文件存储,不过我们也可以借用来存储模版。当然建立之前要维护相应的MIME types(先把文件的类型要注册),方可上传此类型的文件。
DATA:LS_WWWDATATAB TYPE WWWDATATAB,
LS_PATH TYPE RLGRAP-FILENAME.
CONCATENATE 'c:/SapReport/' 'KEVIN' '.xls' INTO LS_PATH.
LS_WWWDATATAB-RELID = 'MI'.
LS_WWWDATATAB-OBJID = 'ZKEVIN'.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
KEY = LS_WWWDATATAB
DESTINATION = LS_PATH.
CALL METHOD OF G_WBOOKS 'Open'
EXPORTING
#1 = LS_PATH.
T-code OAOR是将模版传输到BDS(Business Document Service),然后程序运行时在本地打开进行填制。老白已经在他的blog中已经写了,我这里不再复述。
DOI和OLE的东西在老白的博客中已经发了很多,大家有兴趣可以看看,我添加了一个链接。
下面我放一个能直接跑的OLE。
*----------------------------------------------------------------------*
* Program Name: Z_KEVIN_OLE
* Project : N/A
* Author : Kevin.Zhang
* Date : 2007.1.1
* Module : N/A
* Description : Template of Program
*
*
*
* Special features: N/A
*
*
*----------------------------------------------------------------------*
* Modifications:
* Author Date Commented as Description
*----------- -------- ----------- ----------------------------------*
*
*----------------------------------------------------------------------*
REPORT Z_KEVIN_OLE.
*$*$----------------------------------------------------------------$*$*
*$*$ Global Types, Data Statements, Ranges, Constants $*$*
*$*$----------------------------------------------------------------$*$*
*-----------------------------------------------------------------------
* Type Pools
*-----------------------------------------------------------------------
*--Include for OLE-enabling definitions
INCLUDE OLE2INCL .
*-----------------------------------------------------------------------
* Global GUI Controls Variables / Structures
*-----------------------------------------------------------------------
*--Data Definition of OLE
DATA: GS_EXCEL TYPE OLE2_OBJECT ,
GS_WBOOKLIST TYPE OLE2_OBJECT ,
GS_APPLICATION TYPE OLE2_OBJECT ,
GS_WBOOK TYPE OLE2_OBJECT ,
GS_ACTIVESHEET TYPE OLE2_OBJECT ,
GS_SHEETS TYPE OLE2_OBJECT ,
GS_NEWSHEET TYPE OLE2_OBJECT ,
GS_CELL1 TYPE OLE2_OBJECT ,
GS_CELL2 TYPE OLE2_OBJECT ,
GS_CELLS TYPE OLE2_OBJECT ,
GS_RANGE TYPE OLE2_OBJECT ,
GS_FONT TYPE OLE2_OBJECT ,
GS_INTERIOR TYPE OLE2_OBJECT ,
GS_COLUMNS TYPE OLE2_OBJECT ,
GS_CHARTS TYPE OLE2_OBJECT ,
GS_CHART TYPE OLE2_OBJECT ,
GS_CHARTTITLE TYPE OLE2_OBJECT ,
GS_CHARTTITLECHAR TYPE OLE2_OBJECT ,
GS_CHARTOBJECTS TYPE OLE2_OBJECT .
DATA GV_SHEET_NAME(20) TYPE C .
DATA GV_OUTER_INDEX LIKE SY-INDEX .
DATA GV_INTEX(2) TYPE C .
DATA GV_LINE_CNTR TYPE I . "line counter
DATA GV_LINNO TYPE I . "line number
DATA GV_COLNO TYPE I . "column number
DATA GV_VALUE TYPE I . "data
*$*$----------------------------------------------------------------$*$*
*$*$ Selection Screen $*$*
*$*$----------------------------------------------------------------$*$*
*-----------------------------------------------------------------------
* Selection Screen
*-----------------------------------------------------------------------
PARAMETERS: P_SHEETS TYPE I .
*$*$----------------------------------------------------------------$*$*
*$*$ Main Program $*$*
*$*$----------------------------------------------------------------$*$*
*--------- START-OF-SELECTION ----------
START-OF-SELECTION .
DO P_SHEETS TIMES .
*--Forming sheet name
GV_INTEX = SY-INDEX .
GV_OUTER_INDEX = SY-INDEX .
CONCATENATE 'Excel Sheet #' GV_INTEX INTO GV_SHEET_NAME .
*--For the first loop, Excel is initiated and one new sheet is added
IF SY-INDEX = 1 .
CREATE OBJECT GS_EXCEL 'EXCEL.APPLICATION' .
SET PROPERTY OF GS_EXCEL 'Visible' = 1 .
GET PROPERTY OF GS_EXCEL 'Workbooks' = GS_WBOOKLIST .
GET PROPERTY OF GS_WBOOKLIST 'Application' = GS_APPLICATION .
SET PROPERTY OF GS_APPLICATION 'SheetsInNewWorkbook' = 1 .
CALL METHOD OF GS_WBOOKLIST 'Add' = GS_WBOOK .
GET PROPERTY OF GS_APPLICATION 'ActiveSheet' = GS_ACTIVESHEET .
SET PROPERTY OF GS_ACTIVESHEET 'Name' = GV_SHEET_NAME .
*--For the rest of loops, other sheets are added
ELSE .
GET PROPERTY OF GS_WBOOK 'Sheets' = GS_SHEETS .
CALL METHOD OF GS_SHEETS 'Add' = GS_NEWSHEET .
SET PROPERTY OF GS_NEWSHEET 'Name' = GV_SHEET_NAME .
ENDIF .
GV_LINE_CNTR = 1 . "line counter
*--Title
*--Selecting cell area to be merged.
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL2
EXPORTING
#1 = 1
#2 = 4.
CALL METHOD OF GS_EXCEL 'Range' = GS_CELLS
EXPORTING
#1 = GS_CELL1
#2 = GS_CELL2.
CALL METHOD OF GS_CELLS 'Select' .
*--Merging
CALL METHOD OF GS_CELLS 'Merge' .
*--Setting title data
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = GV_LINE_CNTR
#2 = 1.
SET PROPERTY OF GS_CELL1 'Value' = 'TITLE' .
*--Formatting the title
GET PROPERTY OF GS_CELL1 'Font' = GS_FONT .
SET PROPERTY OF GS_FONT 'Underline' = 2 .
SET PROPERTY OF GS_FONT 'Bold' = 1 .
SET PROPERTY OF GS_CELL1 'HorizontalAlignment' = -4108 .
GET PROPERTY OF GS_CELL1 'Interior' = GS_INTERIOR .
SET PROPERTY OF GS_INTERIOR 'ColorIndex' = 15 .
SET PROPERTY OF GS_INTERIOR 'Pattern' = -4124 .
SET PROPERTY OF GS_INTERIOR 'PatternColorIndex' = -4105 .
GV_LINE_CNTR = GV_LINE_CNTR + 1 .
*--Writing some additional data for the title
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = GV_LINE_CNTR
#2 = 1.
SET PROPERTY OF GS_CELL1 'Value' = 'Sheet No' .
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = GV_LINE_CNTR
#2 = 5.
SET PROPERTY OF GS_CELL1 'Value' = ':' .
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = GV_LINE_CNTR
#2 = 6.
SET PROPERTY OF GS_CELL1 'Value' = GV_INTEX .
*--Formatting the area of additional data 1
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL2
EXPORTING
#1 = GV_LINE_CNTR
#2 = 5.
CALL METHOD OF GS_EXCEL 'Range' = GS_CELLS
EXPORTING
#1 = GS_CELL1
#2 = GS_CELL2.
CALL METHOD OF GS_CELLS 'Select' .
GET PROPERTY OF GS_CELLS 'Font' = GS_FONT .
SET PROPERTY OF GS_FONT 'Bold' = 1 .
*--Formatting the area of additional data 2
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = 1
#2 = 5.
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL2
EXPORTING
#1 = GV_LINE_CNTR
#2 = 5.
CALL METHOD OF GS_EXCEL 'Range' = GS_CELLS
EXPORTING
#1 = GS_CELL1
#2 = GS_CELL2.
CALL METHOD OF GS_CELLS 'Select' .
GET PROPERTY OF GS_CELLS 'Columns' = GS_COLUMNS .
CALL METHOD OF GS_COLUMNS 'AutoFit' .
*--Bordering title data area
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL2
EXPORTING
#1 = GV_LINE_CNTR
#2 = 6.
CALL METHOD OF GS_EXCEL 'Range' = GS_CELLS
EXPORTING
#1 = GS_CELL1
#2 = GS_CELL2.
CALL METHOD OF GS_CELLS 'Select' .
CALL METHOD OF GS_CELLS 'BorderAround'
EXPORTING
#1 = 1 "continuous line
#2 = 4. "thick
*--Putting axis labels
GV_COLNO = 2 .
GV_LINE_CNTR = GV_LINE_CNTR + 5 .
GV_LINNO = GV_LINE_CNTR - 1 .
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = GV_LINNO
#2 = 1.
SET PROPERTY OF GS_CELL1 'Value' = 'X' .
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = GV_LINE_CNTR
#2 = 1.
SET PROPERTY OF GS_CELL1 'Value' = 'Y' .
*--Generating some data
DO 3 TIMES .
GV_VALUE = GV_OUTER_INDEX * SY-INDEX * 10 .
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = GV_LINNO
#2 = GV_COLNO.
SET PROPERTY OF GS_CELL1 'Value' = SY-INDEX .
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = GV_LINE_CNTR
#2 = GV_COLNO.
SET PROPERTY OF GS_CELL1 'Value' = GV_VALUE .
GV_COLNO = GV_COLNO + 1 .
ENDDO .
*--Source data area
GV_COLNO = GV_COLNO - 1 .
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = GV_LINNO
#2 = 1.
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL2
EXPORTING
#1 = GV_LINE_CNTR
#2 = GV_COLNO.
CALL METHOD OF GS_EXCEL 'Range' = GS_CELLS
EXPORTING
#1 = GS_CELL1
#2 = GS_CELL2.
CALL METHOD OF GS_CELLS 'Select' .
GET PROPERTY OF GS_APPLICATION 'Charts' = GS_CHARTS .
CALL METHOD OF GS_CHARTS 'Add' = GS_CHART .
CALL METHOD OF GS_CHART 'Activate' .
SET PROPERTY OF GS_CHART 'ChartType' = '51' . "Vertical bar graph
CALL METHOD OF GS_CHART 'SetSourceData'
EXPORTING
#1 = GS_CELLS
#2 = 1.
SET PROPERTY OF GS_CHART 'HasTitle' = 1 .
GET PROPERTY OF GS_CHART 'ChartTitle' = GS_CHARTTITLE .
GET PROPERTY OF GS_CHARTTITLE 'Characters' = GS_CHARTTITLECHAR .
SET PROPERTY OF GS_CHARTTITLECHAR 'Text' = 'Sample Graph' .
*--Locate the chart onto the current worksheet
*--Activate current sheet
CALL METHOD OF GS_EXCEL 'WorkSheets' = GS_ACTIVESHEET
EXPORTING
#1 = GV_SHEET_NAME.
CALL METHOD OF GS_ACTIVESHEET 'Activate' .
CALL METHOD OF GS_CHART 'Location'
EXPORTING
#1 = 2
#2 = GV_SHEET_NAME.
*--Reposition the chart on the worksheet (cut&paste)
CALL METHOD OF GS_ACTIVESHEET 'ChartObjects' = GS_CHARTOBJECTS .
CALL METHOD OF GS_CHARTOBJECTS 'Select' .
CALL METHOD OF GS_CHARTOBJECTS 'Cut' .
*--Select new area
GV_LINE_CNTR = GV_LINE_CNTR + 2 .
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1
EXPORTING
#1 = GV_LINE_CNTR
#2 = 1.
CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL2
EXPORTING
#1 = GV_LINE_CNTR
#2 = 1.
CALL METHOD OF GS_EXCEL 'Range' = GS_CELLS
EXPORTING
#1 = GS_CELL1
#2 = GS_CELL2.
CALL METHOD OF GS_CELLS 'Select' .
CALL METHOD OF GS_ACTIVESHEET 'Paste' .
ENDDO.
*--Deallocating memory
FREE: GS_EXCEL, GS_WBOOKLIST, GS_APPLICATION, GS_WBOOK,
GS_ACTIVESHEET,GS_SHEETS, GS_NEWSHEET, GS_CELL1,
GS_CELL2, GS_CELLS, GS_RANGE, GS_FONT, GS_INTERIOR,
GS_COLUMNS, GS_CHARTS, GS_CHART, GS_CHARTTITLE,
GS_CHARTTITLECHAR, GS_CHARTOBJECTS .
*--------- END-OF-SELECTION ----------
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zcywset15/archive/2008/07/29/2727991.aspx