ABAP DOI 下载SMW0的EXCEL和WORD模板

Posted ctrls

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABAP DOI 下载SMW0的EXCEL和WORD模板相关的知识,希望对你有一定的参考价值。

FUNCTION ‘SAP_OI_LOAD_MIME_DATA‘ 下载SMW0的模板,用METHOD LR_PROXY->OPEN_DOCUMENT_FROM_TABLE 打开模板

没找到和ole一样先下载,在打开的方法

SMWO上载模板用二进制,注意先维护模板类型

 

技术分享图片

REPORT  ZTEST035.

TABLES:MARA.

TYPE-POOLS:SLIS.

DATA: GW_LAYOUT TYPE LVC_S_LAYO,
      GW_FIELD TYPE LVC_S_FCAT,
      GT_FIELD TYPE LVC_T_FCAT.

TYPES:BEGIN OF GS_PERNR,
      MARK(1),
      PERNR           TYPE PERSNO,
      NAME1           TYPE NAME1,
      CARDTYPE(50),
      CARDNUM(50),
      COUNTRY(50),
      SEX(2),
      BIRTHDAY        TYPE DATUM,
      END OF GS_PERNR.

TYPES:BEGIN OF GS_MATNR,
      MARK(1),
      MATNR           TYPE MATNR,
      ERSDA           TYPE ERSDA,
      ERNAM           TYPE ERNAM,
      LAEDA           TYPE LAEDA,
      AENAM           TYPE AENAM,
      VPSTA           TYPE VPSTA,
      PSTAT           TYPE PSTAT_D,
      END OF GS_MATNR.

DATA: GW_PERNR TYPE GS_PERNR,
      GT_PERNR TYPE GS_PERNR OCCURS 0,
      GT_PERNR2 TYPE GS_PERNR OCCURS 0,
      GW_MATNR TYPE GS_MATNR,
      GT_MATNR TYPE GS_MATNR OCCURS 0.

DEFINE ALV_FIELD.
  GW_FIELD-key = &1.
  GW_FIELD-FIELDNAME = &2.
  GW_FIELD-COLTEXT = &3.
  IF GW_FIELD-FIELDNAME = MARK.

  ENDIF.

  IF GW_FIELD-FIELDNAME = PERNR.
    GW_FIELD-LZERO = X.
  ENDIF.
  APPEND GW_FIELD TO GT_FIELD.
  CLEAR GW_FIELD.
END-OF-DEFINITION.

  "路径
  DATA: L_FILENAME   TYPE STRING,
        L_PATH       TYPE STRING,
        L_FULLPATH   TYPE STRING.
  "模板
  DATA: LS_WWWDATA   LIKE WWWDATATAB,
        P_OBJID      LIKE WWWDATATAB-OBJID VALUE ZTEST035_1,
*        L_DES        LIKE RLGRAP-FILENAME,
*        L_RC         TYPE SY-SUBRC.
        L_MESSAGE    TYPE STRING.
"<----开始DOI
  DATA: LR_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
        LR_CONTROL   TYPE REF TO I_OI_CONTAINER_CONTROL,
        LR_ERROR     TYPE REF TO I_OI_ERROR,
        L_RETCODE    TYPE SOI_RET_STRING,
        LR_PROXY     TYPE REF TO I_OI_DOCUMENT_PROXY,
        G_DOCUMENT_TYPE(50) TYPE C VALUE Word.Document.8.
"<--------下载模板
  DATA: LT_MIMETAB   TYPE W3MIME OCCURS 0,
        L_LENGTH     TYPE I,
        L_DOC_FORMAT TYPE C,
        L_DOC_TYPE   TYPE C.

SELECTION-SCREEN BEGIN OF SCREEN 500 AS SUBSCREEN .
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
  SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
SELECTION-SCREEN END OF BLOCK B1 .
SELECTION-SCREEN END OF SCREEN 500.

SELECTION-SCREEN BEGIN OF SCREEN 600 AS SUBSCREEN .
SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.
  SELECT-OPTIONS: S_PERNR FOR MARA-MATNR.
SELECTION-SCREEN END OF BLOCK B2 .
SELECTION-SCREEN END OF SCREEN 600.

SELECTION-SCREEN: BEGIN OF TABBED BLOCK MYTAB FOR 30 LINES,
                  TAB (18) TEXT1 USER-COMMAND BUT1 DEFAULT SCREEN 500,
                  TAB (18) TEXT2 USER-COMMAND BUT2 DEFAULT SCREEN 600,
                  END OF BLOCK MYTAB.





INITIALIZATION.
  TEXT1 = 物料查询FOR WORD.
  TEXT2 = FOR EXCEL.

*&-------------------------------------------------------------------------------------------*
* AT SELECTION-SCREEN
*&-------------------------------------------------------------------------------------------*
AT SELECTION-SCREEN.
  CASE  SY-DYNNR .
    WHEN 1000 .
      CASE SY-UCOMM .
        WHEN BUT1.
          MYTAB-DYNNR = 500 .
          P_OBJID = ZTEST035_1.
          G_DOCUMENT_TYPE = Word.Document.8.
        WHEN BUT2.
          MYTAB-DYNNR = 600.
          P_OBJID = ZTEST035_2.
          G_DOCUMENT_TYPE = Excel.Sheet.
        WHEN OTHERS .
      ENDCASE.

  ENDCASE.

START-OF-SELECTION.
  CASE  MYTAB-DYNNR .
    WHEN 500.
      PERFORM GET_MATNR.
      PERFORM MATNR_ALV_DISPLAY.
    WHEN 600.
      PERFORM GET_PERNR.
      PERFORM PERNR_ALV_DISPLAY.
    WHEN OTHERS.
  ENDCASE.
*&---------------------------------------------------------------------*
*&      Form  GET_MATNR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_MATNR .
  SELECT
    MATNR
    ERSDA
    ERNAM
    LAEDA
    AENAM
    VPSTA
    PSTAT
    FROM MARA
    INTO CORRESPONDING FIELDS OF TABLE GT_MATNR
    WHERE MATNR IN S_MATNR.
ENDFORM.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  GET_PERNR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_PERNR .
  GW_PERNR-MARK     = ‘‘.
  GW_PERNR-PERNR     = 00012345.
  GW_PERNR-NAME1     = 王一.
  GW_PERNR-CARDTYPE  = 居民身份证.
  GW_PERNR-CARDNUM   = 1111111.
  GW_PERNR-COUNTRY   = 波黑.
  GW_PERNR-SEX       = .
  GW_PERNR-BIRTHDAY  = 19900101.
  APPEND GW_PERNR TO GT_PERNR.
  GW_PERNR-MARK     = ‘‘.
  GW_PERNR-PERNR     = 00067890.
  GW_PERNR-NAME1     = 李三.
  GW_PERNR-CARDTYPE  = 港澳居民来往内地通行证.
  GW_PERNR-CARDNUM   = 2222222.
  GW_PERNR-COUNTRY   = 波兰.
  GW_PERNR-SEX       = .
  GW_PERNR-BIRTHDAY  = 19900202.
  APPEND GW_PERNR TO GT_PERNR.
ENDFORM.                    " GET_PERNR
*&---------------------------------------------------------------------*
*&      Form  MATNR_ALV_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM MATNR_ALV_DISPLAY .
  PERFORM GET_FIELD.
  CALL FUNCTION REUSE_ALV_GRID_DISPLAY_LVC
    EXPORTING
      I_CALLBACK_PROGRAM                = SY-REPID
      I_CALLBACK_PF_STATUS_SET          = FRM_PF_STATUS_SET
      I_CALLBACK_USER_COMMAND           = FRM_USER_COMMAND
      IS_LAYOUT_LVC                     = GW_LAYOUT
      IT_FIELDCAT_LVC                   = GT_FIELD[]
    TABLES
      T_OUTTAB                          = GT_MATNR[]
    EXCEPTIONS
      PROGRAM_ERROR                     = 1
      OTHERS                            = 2
            .
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.                    " ALV_DISPLAY
*&---------------------------------------------------------------------*
*&      Form  PERNR_ALV_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM PERNR_ALV_DISPLAY .
  PERFORM GET_FIELD.
  CALL FUNCTION REUSE_ALV_GRID_DISPLAY_LVC
    EXPORTING
      I_CALLBACK_PROGRAM                = SY-REPID
      I_CALLBACK_PF_STATUS_SET          = FRM_PF_STATUS_SET
      I_CALLBACK_USER_COMMAND           = FRM_USER_COMMAND
      IS_LAYOUT_LVC                     = GW_LAYOUT
      IT_FIELDCAT_LVC                   = GT_FIELD[]
    TABLES
      T_OUTTAB                          = GT_PERNR[]
    EXCEPTIONS
      PROGRAM_ERROR                     = 1
      OTHERS                            = 2
            .
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
ENDFORM.                    " PERNR_ALV_DISPLAY
*&---------------------------------------------------------------------*
*&      Form  GET_FIELD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_FIELD .

  IF MYTAB-DYNNR = 500.
*    ALV_FIELD ‘MARK‘ ‘选择‘.
    ALV_FIELD ‘‘ MATNR 物料号.
    ALV_FIELD ‘‘ ERSDA 选择1.
    ALV_FIELD ‘‘ ERNAM 选择2.
    ALV_FIELD ‘‘ LAEDA 选择4.
    ALV_FIELD ‘‘ AENAM 选择5.
    ALV_FIELD ‘‘ VPSTA 选择6.
    ALV_FIELD ‘‘ PSTAT 选择7.
  ELSEIF MYTAB-DYNNR = 600.
*    ALV_FIELD ‘‘ ‘MARK‘ ‘选择‘.
    ALV_FIELD ‘‘ PERNR 员工号.
    ALV_FIELD ‘‘ NAME1 姓名.
    ALV_FIELD ‘‘ CARDTYPE 证件类型.
    ALV_FIELD ‘‘ CARDNUM 证件号码.
    ALV_FIELD ‘‘ SEX 性别.
    ALV_FIELD ‘‘ BIRTHDAY 生日.
  ENDIF.
  GW_LAYOUT-SEL_MODE     = D.
  GW_LAYOUT-BOX_FNAME     = MARK.
"默认设置  等同于B
"‘A‘行和列的选择,无法选择单元格  用户可以使用最左边的选择按钮来选择多行
"‘B‘单选,不可以多选行,不可以多选单元格
"‘C‘多选,可以多选行,不可以多选单元格
"‘D‘单元格的选择,可以多选单元格   用户可以使用最左边的选择按钮来选择多行
  GW_LAYOUT-CWIDTH_OPT   = X.

ENDFORM.                    " GET_FIELD
*&---------------------------------------------------------------------*
*&      Form  SET_PF_STATUS
*&---------------------------------------------------------------------*
*       button define
*----------------------------------------------------------------------*
*      -->RT_EXTAB   text
*----------------------------------------------------------------------*
FORM FRM_PF_STATUS_SET USING RT_EXTAB TYPE SLIS_T_EXTAB.
  SET PF-STATUS STATUS035  EXCLUDING RT_EXTAB.
ENDFORM.                    "SET_PF_STATUS
*&---------------------------------------------------------------------*
*&      Form  FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_USER_COMMAND USING    L_UCOMM    TYPE SY-UCOMM"未生成发票号
                      CHANGING is_selfield TYPE slis_selfield.
  DATA: LV_ANSWER.
*is_selfield-refresh    = ‘X‘.
*
*  DATA: g_grid TYPE REF TO cl_gui_alv_grid.
*  CALL FUNCTION ‘GET_GLOBALS_FROM_SLVC_FULLSCR‘
*    IMPORTING
*      e_grid = g_grid.
*  CALL METHOD g_grid->refresh_table_display.

  CASE L_UCOMM .
    WHEN ZOUT.
      IF MYTAB-DYNNR = 500.

        READ TABLE GT_MATNR INTO GW_MATNR WITH KEY MARK = X.
        IF SY-SUBRC <> 0.
          MESSAGE 请选择其中一行! TYPE E.
        ENDIF.
        "保存前弹出提示消息
        CALL FUNCTION POPUP_TO_CONFIRM_STEP
          EXPORTING
            TEXTLINE1 = TEXT-T01
            TITEL     = TEXT-T02
          IMPORTING
            ANSWER    = LV_ANSWER.
        CHECK LV_ANSWER = J.

        "<----选择路径
        PERFORM SELECT_PATH.

        LOOP AT GT_MATNR INTO GW_MATNR.
          CLEAR L_FULLPATH.
          CONCATENATE L_PATH GW_MATNR-MATNR .DOC INTO L_FULLPATH.
          perform FRM_DOWNLOAD_WORD.
          CLEAR:GW_MATNR.
        ENDLOOP.

      ELSEIF MYTAB-DYNNR = 600.

        READ TABLE GT_PERNR INTO GW_PERNR WITH KEY MARK = X.
        IF SY-SUBRC <> 0.
          MESSAGE 请选择其中一行! TYPE E.
        ENDIF.

                "保存前弹出提示消息
        CALL FUNCTION POPUP_TO_CONFIRM_STEP
          EXPORTING
            TEXTLINE1 = TEXT-T01
            TITEL     = TEXT-T02
          IMPORTING
            ANSWER    = LV_ANSWER.
        CHECK LV_ANSWER = J.

        "<----选择路径
        PERFORM SELECT_PATH.

        perform FRM_DOWNLOAD_EXCEL.

      ENDIF.

  ENDCASE.
ENDFORM.                    "frm_user_command
*&---------------------------------------------------------------------*
*&      Form  FRM_DOWNLOAD_MATNR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_DOWNLOAD_WORD .

  DATA: LR_WORD TYPE REF TO I_OI_WORD_PROCESSOR_DOCUMENT.
  "<---------替换 宏
  DATA: ROW_INDEX    TYPE I.
  DATA: L_WORD(250),
        L_FILE_NAME(250).
*"<----选择路径
*  PERFORM SELECT_PATH.

"<----检查模板是否存在
  PERFORM CHECK_MODEL.

"<----模板下载;如果还要编辑模板,则不先下载,先创建实例和代理,
"<----用‘SAP_OI_LOAD_MIME_DATA‘获取服务器模板信息,打开编辑
*  L_DES = L_FULLPATH.
*  CALL FUNCTION ‘DOWNLOAD_WEB_OBJECT‘
*    EXPORTING
*      KEY               = LS_WWWDATA
*      DESTINATION       = L_DES
*    IMPORTING
*      RC                = L_RC
*            .
*  IF L_RC <> 0.
*    CLEAR:L_MESSAGE.
*    CONCATENATE ‘模板‘ P_OBJID ‘下载失败!‘ INTO L_MESSAGE.
*    MESSAGE L_MESSAGE TYPE ‘E‘.
*  ENDIF.

  CALL FUNCTION SAPGUI_PROGRESS_INDICATOR
     EXPORTING
       TEXT = 正在初始化 OI 控制, 请稍候.......

"<----开始DOI
  PERFORM GET_DOI.


  CALL FUNCTION SAPGUI_PROGRESS_INDICATOR
     EXPORTING
       TEXT = 正在下载, 请稍候.......

"<--------下载模板并用代理代开
  PERFORM DOWNLOAD_MODEL.

  "打开模板
  CLEAR: LR_ERROR,L_RETCODE.
  CALL METHOD LR_PROXY->OPEN_DOCUMENT_FROM_TABLE
    EXPORTING
      DOCUMENT_SIZE  = L_LENGTH
      DOCUMENT_TABLE = LT_MIMETAB
      OPEN_INPLACE   = X
    IMPORTING
      ERROR           = LR_ERROR
      RETCODE         = L_RETCODE.
  CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
    EXPORTING
      TYPE = E.
    "获取WORD
  CLEAR: LR_ERROR,L_RETCODE.
  CALL METHOD LR_PROXY->GET_WORDPROCESSOR_INTERFACE
    EXPORTING
      NO_FLUSH        = X
    IMPORTING
      WP_INTERFACE    = LR_WORD
      ERROR           = LR_ERROR
      RETCODE         = L_RETCODE.
  CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
    EXPORTING
      TYPE = E.
  "替换的宏
  DEFINE REPLACE_WORD.
    CLEAR: LR_ERROR,L_RETCODE,L_WORD.
    L_WORD = &1.
    CALL METHOD LR_WORD->REPLACE
      EXPORTING
        FLAG           = ‘‘
*        FLAG           = ‘WORD‘
        POS            = global
        REPLACE_STRING = L_WORD
        SEARCH_STRING  = &2
      IMPORTING
        ERROR           = LR_ERROR
        RETCODE         = L_RETCODE.
    CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
      EXPORTING
        TYPE = E.
  END-OF-DEFINITION.

  "调用宏替换
  REPLACE_WORD GW_MATNR-MATNR &01.
  REPLACE_WORD GW_MATNR-ERSDA &02.
  REPLACE_WORD GW_MATNR-ERNAM &03.
  REPLACE_WORD GW_MATNR-LAEDA &04.
  REPLACE_WORD GW_MATNR-AENAM &05.
  REPLACE_WORD GW_MATNR-VPSTA &06.
  REPLACE_WORD GW_MATNR-PSTAT &07.

  "保存
  L_FILE_NAME = L_FULLPATH.
  CALL METHOD LR_PROXY->SAVE_COPY_AS
    EXPORTING
      FILE_NAME = L_FILE_NAME.
  "释放
  CALL METHOD LR_PROXY->CLOSE_DOCUMENT.
  CALL METHOD LR_PROXY->RELEASE_DOCUMENT.
  CALL METHOD LR_CONTROL->DESTROY_CONTROL.
  FREE LR_CONTROL.
  IF LR_CONTAINER IS NOT INITIAL.
    CALL METHOD LR_CONTAINER->FREE.
  ENDIF.
  FREE LR_CONTAINER .
  FREE LR_CONTROL   .
  FREE LR_ERROR     .
  FREE L_RETCODE    .
  FREE LR_PROXY     .
ENDFORM.                    " FRM_DOWNLOAD_MATNR
*&---------------------------------------------------------------------*
*&      Form  FRM_DOWNLOAD_PERNR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_DOWNLOAD_EXCEL .
"<--------EXCEL相关
  DATA: LR_SHEET     TYPE REF TO I_OI_SPREADSHEET,
        LT_SHEETS    TYPE SOI_SHEETS_TABLE.

"<---------填数 宏
  DATA: ROW_INDEX    TYPE I.
  DATA: LT_RANGES    TYPE SOI_RANGE_LIST,
        LS_RANGE     TYPE SOI_RANGE_ITEM,
        LT_CONTENTS  TYPE SOI_GENERIC_TABLE,
        LS_CONTENT   TYPE SOI_GENERIC_ITEM,
        L_FILE_NAME(250).

"<----检查模板是否存在
  PERFORM CHECK_MODEL.

"<----模板下载;如果还要编辑模板,则不先下载,先创建实例和代理,
"<----用‘SAP_OI_LOAD_MIME_DATA‘获取服务器模板信息,打开编辑
*  L_DES = L_FULLPATH.
*  CALL FUNCTION ‘DOWNLOAD_WEB_OBJECT‘
*    EXPORTING
*      KEY               = LS_WWWDATA
*      DESTINATION       = L_DES
*    IMPORTING
*      RC                = L_RC
*            .
*  IF L_RC <> 0.
*    CLEAR:L_MESSAGE.
*    CONCATENATE ‘模板‘ P_OBJID ‘下载失败!‘ INTO L_MESSAGE.
*    MESSAGE L_MESSAGE TYPE ‘E‘.
*  ENDIF.

  CALL FUNCTION SAPGUI_PROGRESS_INDICATOR
     EXPORTING
       TEXT = 正在初始化 OI 控制, 请稍候.......

"<----开始DOI
  PERFORM GET_DOI.


  CALL FUNCTION SAPGUI_PROGRESS_INDICATOR
     EXPORTING
       TEXT = 正在下载, 请稍候.......

"<--------下载模板并用代理代开
  PERFORM DOWNLOAD_MODEL.

  "打开模板
  CLEAR: LR_ERROR,L_RETCODE.
  CALL METHOD LR_PROXY->OPEN_DOCUMENT_FROM_TABLE
    EXPORTING
      DOCUMENT_SIZE  = L_LENGTH
      DOCUMENT_TABLE = LT_MIMETAB
      OPEN_INPLACE   = X
    IMPORTING
      ERROR           = LR_ERROR
      RETCODE         = L_RETCODE.
  CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
    EXPORTING
      TYPE = E.

  "获取sheet
  CLEAR: LR_ERROR,L_RETCODE.
  CALL METHOD LR_PROXY->GET_SPREADSHEET_INTERFACE
    EXPORTING
      NO_FLUSH        = X
    IMPORTING
      SHEET_INTERFACE = LR_SHEET
      ERROR           = LR_ERROR
      RETCODE         = L_RETCODE.
  CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
    EXPORTING
      TYPE = E.
"获取sheet名
*  CALL METHOD LR_SHEET->GET_SHEETS
*    EXPORTING
*      NO_FLUSH        = ‘X‘
*    IMPORTING
*      SHEETS          = LT_SHEETS
*      ERROR           = LR_ERROR
*      RETCODE         = L_RETCODE.
  "选择sheet
  CLEAR: LR_ERROR,L_RETCODE.
  CALL METHOD LR_SHEET->SELECT_SHEET
    EXPORTING
      NAME            = 人员信息
      NO_FLUSH        = X
    IMPORTING
      ERROR           = LR_ERROR
      RETCODE         = L_RETCODE.
  CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
    EXPORTING
      TYPE = E.
*执行EXCEL里的宏
*  CALL METHOD DOCUMENT->EXECUTE_MACRO
*    EXPORTING
*      g_macro = g_macro
**    SCRIPT_NAME  = ‘Open_text‘
**    NO_FLUSH     = ‘ ‘
*    IMPORTING
*      cl_error        = cl_errorS.
**  CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
**    EXPORTING
**      TYPE = ‘E‘.
"<---------填数 宏
  DEFINE SET_CELL."一次选中一个单元格

    CLEAR:LT_RANGES[],LS_RANGE ,LT_CONTENTS[],LS_CONTENT.
    LS_RANGE-NAME = CELL.
    LS_RANGE-ROWS = 2.
    LS_RANGE-COLUMNS = 2.
    LS_RANGE-CODE = 4.
    APPEND LS_RANGE TO LT_RANGES.

    LS_CONTENT-ROW = &1.
    LS_CONTENT-COLUMN = &2.
    LS_CONTENT-VALUE = &3.
    APPEND LS_CONTENT TO LT_CONTENTS.

   "选择插入区域
   CLEAR: LR_ERROR,L_RETCODE.
    CALL METHOD LR_SHEET->INSERT_RANGE_DIM
      EXPORTING
        NAME    = cell
        LEFT    = &2
        TOP     = &1
        ROWS    = 1
        COLUMNS = 1
      IMPORTING
        ERROR           = LR_ERROR
        RETCODE         = L_RETCODE.
    CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
      EXPORTING
        TYPE = E.

    "为选择区域赋值
    CLEAR: LR_ERROR,L_RETCODE.
    CALL METHOD LR_SHEET->SET_RANGES_DATA
      EXPORTING
        NO_FLUSH        = X
        RANGES          = LT_RANGES[]
        CONTENTS        = LT_CONTENTS[]
      IMPORTING
        ERROR           = LR_ERROR
        RETCODE         = L_RETCODE.
    CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
      EXPORTING
        TYPE = E.

  END-OF-DEFINITION.

  "循环调用宏填数据
  LOOP AT GT_PERNR INTO GW_PERNR WHERE MARK = X.
    APPEND GW_PERNR TO GT_PERNR2.
    CLEAR GW_PERNR.
  ENDLOOP.
  LOOP AT GT_PERNR2 INTO GW_PERNR WHERE MARK = X.
    CLEAR ROW_INDEX.
    ROW_INDEX = SY-TABIX + 1.
    SET_CELL ROW_INDEX 1 GW_PERNR-PERNR.
    SET_CELL ROW_INDEX 2 GW_PERNR-NAME1.
    SET_CELL ROW_INDEX 3 GW_PERNR-CARDTYPE.
    SET_CELL ROW_INDEX 4 GW_PERNR-CARDNUM.
    SET_CELL ROW_INDEX 5 GW_PERNR-COUNTRY.
    SET_CELL ROW_INDEX 6 GW_PERNR-SEX.
    SET_CELL ROW_INDEX 7 GW_PERNR-BIRTHDAY.
  ENDLOOP.

  "保存
  L_FILE_NAME = L_FULLPATH.
  CALL METHOD LR_PROXY->SAVE_COPY_AS
    EXPORTING
      FILE_NAME = L_FILE_NAME.
  "释放
  CALL METHOD LR_PROXY->CLOSE_DOCUMENT.
  CALL METHOD LR_PROXY->RELEASE_DOCUMENT.
  CALL METHOD LR_CONTROL->DESTROY_CONTROL.
  FREE LR_CONTROL.
  IF LR_CONTAINER IS NOT INITIAL.
    CALL METHOD LR_CONTAINER->FREE.
  ENDIF.
  FREE LR_CONTAINER .
  FREE LR_CONTROL   .
  FREE LR_ERROR     .
  FREE L_RETCODE    .
  FREE LR_PROXY     .
  FREE LR_SHEET     .
  FREE LT_SHEETS    .
ENDFORM.                    " FRM_DOWNLOAD_PERNR
*&---------------------------------------------------------------------*
*&      Form  SELECT_PATH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SELECT_PATH .
  "<----选择路径
"FUNCTION:GUI_FILE_SAVE_DIALOG
  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
    EXPORTING
      WINDOW_TITLE         = 下载文件
      FILE_FILTER          = ‘‘
    CHANGING
      FILENAME             = L_FILENAME
      PATH                 = L_PATH
      FULLPATH             = L_FULLPATH
    EXCEPTIONS
      CNTL_ERROR           = 1
      ERROR_NO_GUI         = 2
      NOT_SUPPORTED_BY_GUI = 3
      OTHERS               = 4.





  IF L_PATH IS INITIAL OR L_FILENAME IS INITIAL."未选择路径,则返回
    RETURN.
  ELSE.
    IF MYTAB-DYNNR = 500."重写文件名,不需要检查
*      TRANSLATE  L_FILENAME  TO UPPER CASE .
*      FIND REGEX ‘.DOC‘ IN L_FILENAME.
*      IF SY-SUBRC <> 0.
*        MESSAGE ‘请填写完整路径!‘ TYPE ‘E‘.
*        RETURN.
*      ENDIF.
    ELSEIF MYTAB-DYNNR = 600.
      TRANSLATE  L_FILENAME  TO UPPER CASE .
      FIND REGEX .XLS IN L_FILENAME.
      IF SY-SUBRC <> 0.
        MESSAGE 请填写完整路径! TYPE E.
        RETURN.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    " SELECT_PATH
*&---------------------------------------------------------------------*
*&      Form  GET_DOI
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_DOI .
"<--------control实例和document代理
*"获取容器
*skip 1."skip 1必须,否则不能创建屏幕。
*LR_CONTAINER = CL_GUI_CONTAINER=>SCREEN0.
*PERFORM GET_CONTAINER.
"自己创建屏幕0100和容器 ‘CONTAINER‘
  "实例化容器实例
  CREATE OBJECT LR_CONTAINER
    EXPORTING
      CONTAINER_NAME = CONTAINER.

"创建container control对象实例并初始化
*PERFORM CREATE_C0NTAINER_CONTROL.
  "创建container control对象实例
  CALL METHOD C_OI_CONTAINER_CONTROL_CREATOR=>GET_CONTAINER_CONTROL
    IMPORTING
      CONTROL = LR_CONTROL.
  "initialize control
  CALL METHOD LR_CONTROL->INIT_CONTROL
    EXPORTING
      R3_APPLICATION_NAME      = ZTEST035
      INPLACE_ENABLED          = X
      INPLACE_SCROLL_DOCUMENTS = X
      PARENT                   = LR_CONTAINER
      REGISTER_ON_CLOSE_EVENT  = X
      REGISTER_ON_CUSTOM_EVENT = X
      NO_FLUSH                 = X
      IMPORTING
      RETCODE                  = L_RETCODE.

  CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
      EXPORTING
        TYPE = E.
  CLEAR:L_RETCODE,LR_ERROR.
*创建document proxy对象的实例,告诉SAP DOI 开辟一个.XLS策略
  CALL METHOD LR_CONTROL->GET_DOCUMENT_PROXY
    EXPORTING
*      DOCUMENT_FORMAT =
      DOCUMENT_TYPE   = G_DOCUMENT_TYPE
      NO_FLUSH        = X
*      REGISTER_CONTAINER =
    IMPORTING
      DOCUMENT_PROXY  = LR_PROXY
      ERROR           = LR_ERROR
      RETCODE         = L_RETCODE.
  CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE
    EXPORTING
      TYPE = E.

ENDFORM.                    " GET_DOI
*&---------------------------------------------------------------------*
*&      Form  CHECK_MODEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CHECK_MODEL .
"<----检查模板是否存在
  SELECT
    SINGLE RELID OBJID
    FROM WWWDATA
    INTO CORRESPONDING FIELDS OF LS_WWWDATA
    WHERE SRTF2 = 0
    AND RELID = MI
    AND OBJID = P_OBJID.
  IF SY-SUBRC <> 0.
    CLEAR:L_MESSAGE.
    CONCATENATE 模板 P_OBJID 不存在! INTO L_MESSAGE.
    MESSAGE L_MESSAGE TYPE E.
  ENDIF.
ENDFORM.                    " CHECK_MODEL
*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_MODEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DOWNLOAD_MODEL .
  "<--------下载模板并用代理代开
  REFRESH LT_MIMETAB.
  CALL FUNCTION SAP_OI_LOAD_MIME_DATA
    EXPORTING
      OBJECT_ID              = P_OBJID
    IMPORTING
      DATA_SIZE              = L_LENGTH
      DOCUMENT_TYPE          = L_DOC_TYPE
      DOCUMENT_FORMAT        = L_DOC_FORMAT
    TABLES
      DATA_TABLE             = LT_MIMETAB
    EXCEPTIONS
      OBJECT_NOT_FOUND       = 1
      INTERNAL_ERROR         = 2
      OTHERS                 = 3 .
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
ENDFORM.                    " DOWNLOAD_MODEL

 

 

以上是关于ABAP DOI 下载SMW0的EXCEL和WORD模板的主要内容,如果未能解决你的问题,请参考以下文章

ABAP WEBRFC

SAP 下载EXCEL DOI使用OLE功能(DOI获取OLE对象)

SAP文件的上传下载 SMW0,二进制文件

ABAP 对内表数据下载到EXCEL的几种方法

(原创)带模板的DOI输出EXCEL

SMW0 上传问题?