ABAP 程序完成EXCEL文件的导入导出,OLE技术的应用

今天主要学习了使用微软的OLE技术,用ABAP程序完成excel文件的导入导出处理。

(一)常用的OLE函数:

CALL METHOD  ,  CALL OBJECT ,   FREE OBJECT ,  GET PROPERTY  ,  SET   PROPERTY.

设属性:SET PROPERTY OF <对象><属性> = <value>

取属性:GET PROPERTY OF <对象><属性> = 变量

调用方法:CALL METHOD OF <对象><METHOD><value>     EXPORTING #<PARA> = <VALUE>.

输出可以是多个参数。

释放对象是:FREE OBJECT<对象>.

(二)从excel读入文件到abap程序

a: 使用函数将excel读入abap内表

 先要定义读入excel 的内表,abap本身已定义格式

然后要有一个输入文件名参数,改参数输入要读取的excel文件的所在路径

再调用函数直接从已知文件名读入内表

最后测试可以循环读取内表并打印显示出来

SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; } .L1S31 { font-style. italic; color: #808080; } .L1S32 { color: #3399FF; } .L1S33 { color: #4DA619; } .L1S52 { color: #0000FF; }REPORT  YTEST67.
DATA:BEGIN OF IEXCEL OCCURS 0.
     INCLUDE STRUCTURE ALSMEX_TABLINE.
DATA:END OF IEXCEL.

PARAMETERS:FN1 LIKE RLGRAP-FILENAME MEMORY ID M01.

"CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
  EXPORTING
    filename                      = FN1
    i_begin_col                   = 1
    i_begin_row                   = 1
    i_end_col                     = 100
    i_end_row                     = 100
  tables
    intern                        = IEXCEL
 EXCEPTIONS
   INCONSISTENT_PARAMETERS       = 1
   UPLOAD_OLE                    = 2
   OTHERS                        = 3
          .
IF sy-subrc <> 0.
  WRITE: / '读入文件错误!',FN1.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
  LOOP AT IEXCEL.
    WRITE IEXCEL-VALUE(20).
    AT END OF ROW.
      WRITE : /.
    ENDAT.
  ENDLOOP.
ENDIF.

b:直接从excel读取

先要定义OLE变量,要有输入文件名参数(同上)

再启动excel 即要创建excel对象

 SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; } .L1S31 { font-style. italic; color: #808080; } .L1S32 { color: #3399FF; } .L1S33 { color: #4DA619; } .L1S52 { color: #0000FF; }REPORT  YTEST68.

INCLUDE OLE2INCL.
*定义OLE变量
DATA:EXCEL TYPE OLE2_OBJECT,
     SHEET TYPE OLE2_OBJECT,
     CELL TYPE OLE2_OBJECT,
     WORKBOOK TYPE OLE2_OBJECT.

DATA:VAL1(30) TYPE C.
DATA: R1 TYPE I,
      R2 TYPE I.
*输入文件名参数
PARAMETERS:FN1 LIKE RLGRAP-FILENAME MEMORY ID M01.

*启动excel
CREATE OBJECT EXCEL 'EXCEL.APPLICATION'."创建excel对象
*
CALL METHOD OF EXCEL 'WORKBOOKS' = WORKBOOK. "调用excel的方法
IF SY-SUBRC NE 0 .
  WRITE : / 'EXCEL 错误!'.
  LEAVE PROGRAM.
ENDIF.
*使excel 可视
SET PROPERTY OF EXCEL 'VISIBLE' = 1.
*EXCEL 文件打开
CALL METHOD OF WORKBOOK 'OPEN'  EXPORTING  #1 = FN1.

IF SY-SUBRC NE 0 .
  WRITE : / '处理错误!'.
  LEAVE PROGRAM.
ENDIF.
*第一页
CALL METHOD OF EXCEL  'WORKSHEETS' = SHEET  EXPORTING  #1 = 1.
*读取前10行10列
DO 10 TIMES.
  WRITE : / '行:',SY-INDEX.
  R1 = SY-INDEX.
  DO 10 TIMES.
    R2 = SY-INDEX.
*指定单元格,并读取值属性
    CALL METHOD OF EXCEL 'CELLS' = CELL  EXPORTING #1 = R1  #2 = R2.
    GET PROPERTY OF CELL 'VALUE' = VAL1.          "读取excel文件中的内容是用get property of excel
                                                  "如果是写入excel文件则用 set property of excel
    WRITE : VAL1.
  ENDDO.
ENDDO.

(三)将数据传送到excel并保存

 SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; } .L1S31 { font-style. italic; color: #808080; } .L1S32 { color: #3399FF; } .L1S33 { color: #4DA619; } .L1S52 { color: #0000FF; }REPORT  YTEST69.

INCLUDE OLE2INCL.
*定义OLE变量
DATA:EXCEL TYPE OLE2_OBJECT,
     SHEET TYPE OLE2_OBJECT,
     CELL TYPE OLE2_OBJECT,
     WORKBOOK TYPE OLE2_OBJECT.

DATA:VAL1(30) TYPE C.
DATA: R1 TYPE I,
      R2 TYPE I.
*输入文件名参数
PARAMETERS:FN1 LIKE RLGRAP-FILENAME MEMORY ID M01.

*启动excel
CREATE OBJECT EXCEL 'EXCEL.APPLICATION'.

IF SY-SUBRC NE 0 .
  WRITE: / ''.
  STOP.
ENDIF.

CALL METHOD OF EXCEL 'WORKBOOKS' = WORKBOOK.
*使excel 可视
SET PROPERTY OF EXCEL 'VISIBLE' = 1.

*建立worksheet
SET PROPERTY OF EXCEL 'SHEETSINNEWWORKBOOK' = 1.          "如果是读取excel文件中的内容 则是直接打开工作簿第一页
CALL METHOD OF WORKBOOK 'ADD'.                             " 例如:CALL METHOD OF EXCEL 'WORKSHEETS'  = SHEET  EXPORTING #1 = 1.
*写前10行10列
DO 10 TIMES.
  R1 = SY-INDEX.
  VAL1+1(4) = '行:'.
  VAL1+6(4) =  R1.
  DO 10 TIMES.
    R2 = SY-INDEX.
    VAL1+15(4) = '列:'.
    VAL1+19(4) = R2.
*指定单元格,并写入值
   CALL METHOD OF EXCEL 'CELLS' = CELL  EXPORTING #1 = R1  #2 = R2.
   SET PROPERTY OF CELL 'VALUE' = VAL1.
  ENDDO.
ENDDO.


*存入文件
GET PROPERTY OF EXCEL 'ACTIVESHEET' = SHEET.      "激活工作簿
GET PROPERTY OF EXCEL 'ACTIVEWORKBOOK' = WORKBOOK.   "激活工作区
CALL METHOD OF WORKBOOK 'SAVEAS' EXPORTING #1 = FN1  #2 = 1.    "将excel文件保存为 FN1  路径
CALL METHOD OF WORKBOOK 'CLOSE'.           "关闭工作区
CALL METHOD OF EXCEL 'QUIT'.               " 退出excel
WRITE 'DONE'.                              "退出成功,输出done

FREE OBJECT SHEET.                      "释放操作
FREE OBJECT WORKBOOK.
FREE OBJECT EXCEL.

你可能感兴趣的:(ABAP 程序完成EXCEL文件的导入导出,OLE技术的应用)