SAP EXCEL OLE常用方法和属性 附加网上找到的比较好的源代码示例一份
1.ole中如何保存和退出。
call method of sheetname 'saveas'
exporting
#1 = filepath
#2 =1.
call method of applicationname 'quit'.
-------------------------------------------------------------------------
2.给sheet重命名。
call method of sheetname 'name' = 'sheetname'.
-------------------------------------------------------------------------
3.创建application.
call method of XXX 'excel.application'.
-------------------------------------------------------------------------
4.设置XXX的显示模式。
set property of XXX 'visible' = 1.前台运行。为0时表示为后台运行。
-------------------------------------------------------------------------
5.创建workbook.
CALL METHOD OF EXCEL 'WORKBOOKS' = WORKBOOK .
call method of workbook 'ADD'.
-------------------------------------------------------------------------
6.在一个workbook中添加一个worksheet.
CALL METHOD OF applicationname 'sheets' =worksheet.
call method of worksheet 'Add' .
-------------------------------------------------------------------------
7.给单元格赋值。
CaLL METHOD OF EXCEL 'CELLS' = CELL
EXPORTING
#1 = 2
#2 = 2.
Set PROPERTY OF CELL 'value'= xxxx.
-------------------------------------------------------------------------
8.指定要被操作的sheet.
CALL METHOD OF applicationname 'Worksheets' =SHEET
EXPORTING
#1 = 'sheet3'. 这里sheet3为要操作的sheet的名字。
call method of sheet 'Activate '.
-------------------------------------------------------------------------
9.指定操作的单元格的范围。
CALL METHOD OF applicationname 'Range' = range
EXPORTING
#1 = 'B2'
#2 = 'c2'.
-------------------------------------------------------------------------
10. 如何操作范围内的单元格。
call method of range 'select '. range也为已经定义好的ole2_object.
Set PROPERTY OF range 'MergeCells' = 0 . 合并单元格,0时不合并,1则合并。与前面一起使用
Set PROPERTY OF range 'HorizontalAlignment' = 10 .
Set PROPERTY OF range 'VerticalAlignment' = -4108 .
-------------------------------------------------------------------------
11.单元格内部属性的操作.
call method of cell 'INTERIOR' = int.
set property of int 'ColorIndex' = color. 颜色
set property of int 'Pattern' = pattern.
-------------------------------------------------------------------------
12.单元格内字体的操作.
call method of CELL 'FONT' = font.
set property of font 'BOLD' = bold.
set property of font 'SIZE' = size.
-------------------------------------------------------------------------
13.边框的操作.
call method of CELL 'BORDERS' = BORDERS
EXPORTING
#1 = '1'. 1-left 2-right 3-top 4-bottom
set property of borders 'Linestyle' = plinestyle .
set property of borders 'Weight' = pweight .
free object borders.
在此之前应该指定range.
-------------------------------------------------------------------------
14.复制与粘贴.
call method of sheet 'copy'.
call method of sheet 'paste'.
-------------------------------------------------------------------------
15.一点注意。
在操作sheet时,默认为上次操作的sheet.如果想更换,参考8。
-------------------------------------------------------------------------
16。一个问题。有时输入数据如111111111111111111,会显示为1E+17。
解决办法:
CALL METHOD OF h_excel 'COLUMNS' = columnObj
EXPORTING
#1 = 6. "the column number
SET PROPERTY OF columnObj 'ColumnWidth' = 10.
SET PROPERTY OF columnObj 'NumberFormat' = '@'.
-------------------------------------------------------------------------
17。打开一个workbook.
call METHOD OF workbook 'Open' EXPORTING #1 =filename+path.
-------------------------------------------------------------------------
18. 所有的操作方法都可以在sell----表 oleload 中查询到。
-------------------------------------------------------------------------
19. 执行宏。
CALL METHOD OF EXCEL 'RUN' EXPORTING #1 ='ZMACRO2'.
-------------------------------------------------------------------------
20。清除range 内容
CALL METHOD OF EXCEL 'Range' = RANGE
EXPORTING
#1 = tar_cell
#2 = tar_cell.
call METHOD of RANGE 'ClearContents'.
-------------------------------------------------------------------------
21 合并单元格
CALL METHOD OF EXCEL 'Range' = RANGE
EXPORTING
#1 = sor_cell
#2 = tar_cell.
SET PROPERTY OF RANGE 'MergeCells' = 1.
-------------------------------------------------------------------------
22。缩小字体填充
SET PROPERTY OF range 'ShrinkToFit' = 0 .
note: 为0时取消缩小字体填充,为1时设置缩小字体填充。
-------------------------------------------------------------------------
note:
1.与ole相关的关键字存储在表oleload中。
2.如果有些ole方法不熟悉,可以利用vba录制宏来寻找相关的关键字。
-------------------------------------------------------------------------
-------------------------------------------------------------------------
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 .