其他分享
首页 > 其他分享> > SAP OLE方式自定义EXCEL模板并写入

SAP OLE方式自定义EXCEL模板并写入

作者:互联网

参考说明

内容参考自CSDN博客:https://blog.csdn.net/此处是一行占位符.

效果展示

在这里插入图片描述

需求说明

自定义EXCEL模板,通过OLE的方式写入数据

代码说明

实现原理1:
主要用到以下三步,通过SMW0上传EXCEL模板,调用函数DOWNLOAD_WEB_OBJECT下载模板;定义对象参考OLE2_OBJECT,调用其中’Open’的方法打开EXCEL,最后通过定位单元格将数据写进去

*第一步: 下载Excel到本地
  PERFORM DOWNLOAD_XLS_TEMPLATE.

*第二步:打开Excel文档
  PERFORM OPEN_EXCEL.

*第三步:   写入数据
  PERFORM WRITE_EXCEL.

实现原理2:
SMW0上传模板展示:
在这里插入图片描述

全部代码展示

*&---------------------------------------------------------------------*
*& Report ZLEARN_WZY_004
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZLEARN_WZY_004.

DATA:C_EXPORT_FILENAME_XLS TYPE STRING VALUE 'ZTTEST01.XLSX', "导出模板默认文件名 '数据导入模板'
     C_OBJID_XLS           TYPE WWWDATATAB-OBJID VALUE 'ZTTEST01'.   "存放模板的对象id

DATA: LO_OBJDATA     LIKE WWWDATATAB,                                "Excel模板对象
      LS_DESTINATION LIKE RLGRAP-FILENAME                           , "下载保存的目标路径
      LC_PATH        TYPE STRING,        "存储路径
      LC_FULLPATH    TYPE STRING,       "文件完整路径
      LI_RC          LIKE SY-SUBRC.   "返回值

DATA: EXCEL_OBJ TYPE OLE2_OBJECT,
      BOOK_OBJ  TYPE OLE2_OBJECT,
      SHEET_OBJ TYPE OLE2_OBJECT,
      CELL_OBJ  TYPE OLE2_OBJECT.

START-OF-SELECTION.

*第一步: 下载Excel到本地
  PERFORM DOWNLOAD_XLS_TEMPLATE.

*第二步:打开Excel文档
  PERFORM OPEN_EXCEL.

*第三步:   写入数据
  PERFORM WRITE_EXCEL.

*&---------------------------------------------------------------------*
*&      Form  download_xls_template
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM DOWNLOAD_XLS_TEMPLATE.

* 获取保存路径
  CALL METHOD CL_GUI_FRONTEND_SERVICES=>GET_DESKTOP_DIRECTORY
    CHANGING
      DESKTOP_DIRECTORY = LC_PATH.
  IF LC_PATH IS INITIAL.
    LC_PATH = 'C:\USERS\ADMIN\DESKTOP'.
  ENDIF.
  CONCATENATE LC_PATH '\' C_EXPORT_FILENAME_XLS INTO LC_FULLPATH.

*  检查模板是否存在
  SELECT SINGLE RELID OBJID FROM WWWDATA INTO CORRESPONDING FIELDS OF LO_OBJDATA
  WHERE SRTF2 = 0 AND RELID = 'MI' AND OBJID = C_OBJID_XLS.
  IF SY-SUBRC NE 0 OR LO_OBJDATA-OBJID EQ SPACE.
    MESSAGE E000(ZPP001) WITH C_EXPORT_FILENAME_XLS.
  ENDIF.

*  下载模板
  LS_DESTINATION = LC_FULLPATH.
  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      KEY         = LO_OBJDATA
      DESTINATION = LS_DESTINATION
    IMPORTING
      RC          = LI_RC.
  IF LI_RC NE 0.
    MESSAGE E001(ZPP001) WITH C_EXPORT_FILENAME_XLS.
  ENDIF.
ENDFORM.                    "download_xls_template


*&---------------------------------------------------------------------*
*&      Form  open_excel
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM OPEN_EXCEL.

  CREATE OBJECT EXCEL_OBJ 'excel.APPLICATION'.
  IF SY-SUBRC NE 0.
    MESSAGE 'EXCEL创建错误' TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
  CALL METHOD OF EXCEL_OBJ 'WORKBOOKS' = BOOK_OBJ.
  SET PROPERTY OF EXCEL_OBJ 'VISIBLE' = 1.
  SET PROPERTY OF EXCEL_OBJ 'SheetsInNewWorkbook' = 1.

*  打开excel文件 , (新建使用:CALL METHOD OF book_obj 'Add'   = sheet_obj)
  CALL METHOD OF  BOOK_OBJ 'Open'   = SHEET_OBJ
            EXPORTING  #1       = LS_DESTINATION.
  CALL METHOD OF  SHEET_OBJ 'ACTIVATE'.
  FREE OBJECT SHEET_OBJ.  "OK
ENDFORM.                    "open_excel

*&---------------------------------------------------------------------*
*&      Form  write_excel
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM WRITE_EXCEL.
  DATA: it_EKPO LIKE EKPO OCCURS 10 WITH HEADER LINE.
  DATA: H TYPE I. "行号
*取数据
  SELECT * FROM EKPO INTO TABLE it_EKPO UP TO 10 ROWS.
  SORT IT_EKPO BY EBELN EBELP.
* 输出Excel表头,自定义格式的列等
  PERFORM FILL_CELL USING 1 1 1 '采购订单号'(001).
  PERFORM FILL_CELL USING 1 2 1 '行项目号'(002).
  PERFORM FILL_CELL USING 1 3 1 '工厂'(003).
  PERFORM FILL_CELL USING 1 4 1 '物料编码'(004).
  PERFORM FILL_CELL USING 1 5 1 '数量'(005).
  PERFORM FILL_CELL USING 1 6 1 '单位'(006).
* 复制数据到Excel,针对固定格式
  LOOP AT it_EKPO.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        INPUT  = it_EKPO-MATNR
      IMPORTING
        OUTPUT = it_EKPO-MATNR.

    H = SY-TABIX + 1.
    PERFORM FILL_CELL USING H 1 0 it_EKPO-EBELN.
    PERFORM FILL_CELL USING H 2 0 it_EKPO-EBELP.
    PERFORM FILL_CELL USING H 3 0 it_EKPO-WERKS.
    PERFORM FILL_CELL USING H 4 0 it_EKPO-MATNR.
    PERFORM FILL_CELL USING H 5 0 it_EKPO-MENGE.
    PERFORM FILL_CELL USING H 6 0 it_EKPO-MEINS.
  ENDLOOP.
  FREE OBJECT CELL_OBJ.
ENDFORM.                    "write_excel

*&---------------------------------------------------------------------*
*&      Form  FILL_CELL
*&---------------------------------------------------------------------*
*   row:  行号,
*   col:  列号,
*   bold: 字体是否加粗,0,否,1是.
*   val:  填充值
*----------------------------------------------------------------------*
FORM FILL_CELL  USING ROW COL BOLD VAL.
  CALL METHOD OF  EXCEL_OBJ 'CELLS'   = CELL_OBJ
    EXPORTING  #1        = ROW  #2        = COL.
  SET PROPERTY OF CELL_OBJ 'VALUE' = VAL.
  FREE OBJECT CELL_OBJ.
ENDFORM.                    "FILL_CELL

标签:OBJ,OLE,自定义,PERFORM,EXCEL,CELL,EKPO,FILL
来源: https://blog.csdn.net/weixin_50324186/article/details/117229426