ABAP:接口文档下载

*&---------------------------------------------------------------------*
*& Report ZTEST_RFC
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_rfc LINE-SIZE 255..

*&---------------------------------------------------------------------*
*& Report  YLB011
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*


INCLUDE yfunc_excel.


TYPE-POOLS : rmdi.
**********************************************************************
*Define Types
**********************************************************************
TYPES: BEGIN OF ts_func,
         parameter  TYPE fupararef-parameter,  "参数名称
         paramtype  TYPE fupararef-paramtype,  "参数类型
         structure  TYPE fupararef-structure,  "接口参数的参考类型
         defaultval TYPE fupararef-defaultval, "输入参数的缺省值
         optional   TYPE fupararef-optional,   "可选参数
         pposition  TYPE fupararef-pposition, "表格行
         stext      TYPE funct-stext,          "短文本
       END OF ts_func,
       tt_func TYPE TABLE OF ts_func.

**********************************************************************
*Global Data
**********************************************************************
DATA: gt_func  TYPE tt_func,
      gt_imp   TYPE tt_func,
      gt_exp   TYPE tt_func,
      gt_cha   TYPE tt_func,
      gt_tab   TYPE tt_func,
      gt_struc TYPE tt_func.

DATA: gt_dd07v TYPE TABLE OF dd07v.

DATA: go_excel TYPE REF TO lcl_excel.
**********************************************************************
* SELECTION-SCREEN
**********************************************************************
PARAMETERS: p_fname LIKE fupararef-funcname.

SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
  PARAMETER p_xls AS CHECKBOX.
  PARAMETER p_dir TYPE string.

  SELECTION-SCREEN SKIP 1.
  PARAMETER p_yh AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK b1.


INITIALIZATION.
  p_xls = 'X'.
  CALL METHOD cl_gui_frontend_services=>get_desktop_directory
    CHANGING
      desktop_directory    = p_dir
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  CALL METHOD cl_gui_cfw=>update_view.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_dir.
  CALL METHOD cl_gui_frontend_services=>directory_browse
*    EXPORTING
*      window_title         =
*      initial_folder       =
    CHANGING
      selected_folder      = p_dir
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.



START-OF-SELECTION.
  PERFORM get_ddic_datatype.

  PERFORM get_func_para USING p_fname gt_func.
  PERFORM classify_func USING gt_func
                                gt_imp
                                gt_exp
                                gt_cha
                                gt_tab.

  PERFORM write_list.

  IF p_xls = 'X'.
    PERFORM download_template.
    LEAVE LIST-PROCESSING.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  write_list
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM write_list.
  PERFORM write_head.
  PERFORM write_para USING '输入'  gt_imp gt_struc.
  PERFORM write_para USING '输出'  gt_exp gt_struc.
  PERFORM write_para USING '修改'  gt_cha gt_struc.
  PERFORM write_para USING ''    gt_tab gt_struc.

  PERFORM write_sturc USING gt_struc.
ENDFORM.                    "write_list

*&---------------------------------------------------------------------*
*&      Form  download_template
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM download_template.
  DATA: ld_row TYPE i.


  DATA: ld_xls_file TYPE string.
  CONCATENATE p_dir '\' p_fname '_' sy-datum '.xls' INTO ld_xls_file.
  CREATE OBJECT go_excel
    EXPORTING
      id_xlsfile = ld_xls_file.

  PERFORM write_head_xls.

  ld_row = 2.
  PERFORM write_para_xls USING '输入' ld_row gt_imp.
  PERFORM write_para_xls USING '输出' ld_row gt_exp.
  PERFORM write_para_xls USING '修改' ld_row gt_cha.
  PERFORM write_para_xls USING ''  ld_row gt_tab.

  PERFORM write_sturc_xls USING ld_row gt_struc.
  DO 8 TIMES.
    go_excel->set_col_width( id_col_no = sy-index ).
  ENDDO.

  IF p_yh = 'X'.
    go_excel->select_range( id_range = 'A:H' ).
    go_excel->set_range_format( id_font_name = '微软雅黑' ).
  ENDIF.


  go_excel->frozen_window( id_row_no = 3 ).
  go_excel->save( ).
ENDFORM.                    "download_template


*&---------------------------------------------------------------------*
*&      Form  write_head_xls
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM write_head_xls.
  go_excel->rename_sheet( id_pos = 1 id_sheetname = p_fname ).
  go_excel->set_cell_data( id_col = 1 id_row = 1 id_value = '函数名' ).
  go_excel->set_cell_format( id_col = 1 id_row = 1 id_bold = 1  id_bg_color = 37 ).
  go_excel->set_cell_data( id_col = 2 id_row = 1 id_value = p_fname ).

  DATA: ld_stext TYPE tftit-stext.
  SELECT SINGLE stext INTO ld_stext FROM tftit WHERE funcname = p_fname.
  IF sy-subrc = 0.
    go_excel->set_cell_data( id_col = 5 id_row = 1 id_value = ld_stext ).
  ENDIF.

  DATA: ld_date TYPE char20,
        ld_time TYPE char20.

  WRITE sy-datum TO ld_date.
  WRITE sy-uzeit TO ld_time.
  CONCATENATE ld_date ld_time INTO ld_date SEPARATED BY space.

  go_excel->set_cell_data( id_col = 8 id_row = 1 id_value = ld_date ).

  go_excel->set_cell_data( id_col = 1 id_row = 2 id_value = '参数名' ).
  go_excel->set_cell_data( id_col = 2 id_row = 2 id_value = '类型' ).
  go_excel->set_cell_data( id_col = 3 id_row = 2 id_value = '长度' ).
  go_excel->set_cell_data( id_col = 4 id_row = 2 id_value = '小数' ).
  go_excel->set_cell_data( id_col = 5 id_row = 2 id_value = '描述文本' ).
  go_excel->set_cell_data( id_col = 6 id_row = 2 id_value = '可选' ).
  go_excel->set_cell_data( id_col = 7 id_row = 2 id_value = '默认值' ).
  go_excel->set_cell_data( id_col = 8 id_row = 2 id_value = '备注' ).

  go_excel->select_range( id_range = 'A2:H2' ).
  go_excel->set_range_format( id_bg_color = 37 ).
ENDFORM.                    "write_head_xls
*&---------------------------------------------------------------------*
*&      Form  write_para_xls
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_PTYPE   text
*      -->CD_ROW     text
*      -->IT_PARA    text
*----------------------------------------------------------------------*
FORM write_para_xls USING id_ptype
                          cd_row
                          it_para TYPE tt_func.
  CHECK it_para IS NOT INITIAL.
  cd_row = cd_row + 2.

  go_excel->set_cell_data(  id_col = 1 id_row = cd_row id_value = id_ptype ).
  go_excel->set_cell_format( id_col = 1 id_row = cd_row id_bold = 1  id_bg_color = 37 ).

  DATA: ls_para TYPE ts_func.
  LOOP AT it_para INTO ls_para.
    cd_row = cd_row + 1.
    go_excel->set_cell_data( id_col = 1 id_row = cd_row id_value = ls_para-parameter ).

    PERFORM write_ddic_info_xls USING cd_row ls_para-structure.

    go_excel->set_cell_data( id_col = 5 id_row = cd_row id_value = ls_para-stext ).
    go_excel->set_cell_data( id_col = 6 id_row = cd_row id_value = ls_para-optional ).
    go_excel->set_cell_data( id_col = 7 id_row = cd_row id_value = ls_para-defaultval ).

  ENDLOOP.
ENDFORM.                    "write_para_xls
*&---------------------------------------------------------------------*
*&      Form  get_ddic_datatype
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_ddic_datatype.
  CALL FUNCTION 'DDUT_DOMVALUES_GET'
    EXPORTING
      name          = 'DATATYPE'
*     LANGU         = SY-LANGU
*     TEXTS_ONLY    = ' '
    TABLES
      dd07v_tab     = gt_dd07v
    EXCEPTIONS
      illegal_input = 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.

  SORT gt_dd07v BY domvalue_l.
ENDFORM.                    "get_ddic_datatype
*&---------------------------------------------------------------------*
*&      Form  get_func_para
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_FUNCNAME  text
*      -->ET_FUNC      text
*----------------------------------------------------------------------*
FORM get_func_para USING id_funcname
                         et_func TYPE tt_func.
  SELECT
      fupararef~parameter         "参数名称
      fupararef~paramtype         "参数类型
      fupararef~structure         "接口参数的参考类型
      fupararef~defaultval        "输入参数的缺省值
      fupararef~optional          "可选参数
      fupararef~pposition
      funct~stext                 "短文本
  FROM fupararef INNER JOIN funct
  ON funct~funcname  = fupararef~funcname AND
         funct~parameter = fupararef~parameter AND
         funct~spras     = sy-langu
  INTO TABLE et_func
  WHERE fupararef~funcname = id_funcname AND
          r3state = 'A'.
  IF sy-subrc <> 0.
    MESSAGE '无法识别的函数名' TYPE 'S' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.

ENDFORM.                    "get_func_para

*&---------------------------------------------------------------------*
*&      Form  classify_func
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->IT_FUNC    text
*      -->ET_IMP     text
*      -->ET_EXP     text
*      -->ET_CHA     text
*      -->ET_TAB     text
*----------------------------------------------------------------------*
FORM classify_func USING it_func TYPE tt_func
                         et_imp TYPE tt_func
                         et_exp TYPE tt_func
                         et_cha TYPE tt_func
                         et_tab TYPE tt_func.
  DATA: ls_func TYPE ts_func.

  LOOP AT it_func INTO ls_func.
    CASE ls_func-paramtype .
      WHEN 'I'.
        APPEND ls_func TO et_imp.
      WHEN 'E'.
        APPEND ls_func TO et_exp.
      WHEN 'C'.
        APPEND ls_func TO et_cha.
      WHEN 'T'.
        APPEND ls_func TO et_tab.
    ENDCASE.
  ENDLOOP.

  SORT et_imp BY pposition.
  SORT et_exp BY pposition.
  SORT et_cha BY pposition.
  SORT et_tab BY pposition.
ENDFORM.                    "classify_func
*&---------------------------------------------------------------------*
*&      Form  write_head
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_PTYPE   text
*      -->IT_PARA    text
*      -->CT_STRUC   text
*----------------------------------------------------------------------*
FORM write_head .
  WRITE: /(30) '函数名',
  (40) p_fname ,
  (10)  ''  ,
  (10)  '',
  (40) '',
  (5)  '',
  (20) '',
  (20) ''.

  SKIP 1.
  WRITE: /(30) '参数名',
  (20) '类型'  ,
  (10)  '长度'  ,
  (10)  '小数'  ,
  (40) '描述文本',
  (5)  '可选',
  (20) '默认值',
  (20) '备注'.
  WRITE sy-uline.
ENDFORM.                    "write_head
*&---------------------------------------------------------------------*
*&      Form  write_para
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_PTYPE   text
*      -->IT_PARA    text
*      -->CT_STRUC   text
*----------------------------------------------------------------------*
FORM write_para USING id_ptype
                      it_para TYPE tt_func
                      ct_struc TYPE tt_func.
  CHECK it_para IS NOT INITIAL.
  FORMAT INTENSIFIED COLOR = 5.
  WRITE :/(30) id_ptype.
  FORMAT INTENSIFIED COLOR = 0.

  DATA: ls_para       TYPE ts_func,
        ld_x_struc(1).
  LOOP AT it_para INTO ls_para.
    WRITE: /(30) ls_para-parameter     .

    CLEAR ld_x_struc.
    PERFORM write_ddic_info USING ls_para-structure
                                      ld_x_struc.
    IF ld_x_struc = 'X'.
      APPEND ls_para TO ct_struc.
    ENDIF.

    WRITE : (40) ls_para-stext         ,
    (5)  ls_para-optional      ,
    (20) ls_para-defaultval  .
  ENDLOOP.
  SKIP 2 .
ENDFORM.                    "write_para

*&---------------------------------------------------------------------*
*&      Form  write_ddic_info
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_DATA    text
*      -->ED_X_STRUC text
*----------------------------------------------------------------------*
FORM write_ddic_info USING id_data
                           ed_x_struc.
  DATA: ld_match(1).
  CHECK id_data IS NOT INITIAL.
  IF id_data CS '-'.
    "参考字段
    PERFORM get_ddic_ref_field USING id_data.
  ELSE.
    "按照数据元素处理
    PERFORM get_ddic_data_element USING id_data ld_match.

    "不是field  默认按照structure 处理
    CHECK ld_match = ''.
    ed_x_struc = 'X'.
    WRITE: (20) id_data,
    (10) '',
    (10) ''.
  ENDIF.
ENDFORM.                    "write_ddic_info

*&---------------------------------------------------------------------*
*&      Form  write_ddic_info_xls
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_ROW     text
*      -->ID_DATA    text
*----------------------------------------------------------------------*
FORM write_ddic_info_xls USING id_row TYPE i
                               id_data.
  DATA: ld_match(1).
  CHECK id_data IS NOT INITIAL.
  IF id_data CS '-'.
    "参考字段
    PERFORM get_ddic_ref_field_xls USING id_row
                                             id_data.
  ELSE.
    "按照数据元素处理
    PERFORM get_ddic_data_element_xls USING id_row id_data ld_match.

    "不是field  默认按照structure 处理
    CHECK ld_match = ''.

    go_excel->set_cell_data( id_col = 2 id_row = id_row id_value = id_data ).





  ENDIF.
ENDFORM.                    "write_ddic_info_xls


*&---------------------------------------------------------------------*
*&      Form  get_ddic_ref_field
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_DATA    text
*----------------------------------------------------------------------*
FORM get_ddic_ref_field USING id_data.
  DATA: ld_tabname   TYPE rmdi_name,
        ld_fieldname TYPE rmdi_fienm,
        ls_field     TYPE rmdi_tabfld.
  SPLIT id_data AT '-' INTO ld_tabname ld_fieldname.
  CALL FUNCTION 'RM_TABLE_INFO_GET'
    EXPORTING
      i_tabname    = ld_tabname
      i_fieldname  = ld_fieldname
      i_langu      = sy-langu
      i_read_texts = 'X'
    IMPORTING
*     E_TABHEADER  =
      e_tabfield_w = ls_field
*     E_TABFIELDS_T       =
    EXCEPTIONS
      not_found    = 1
      ddic_error   = 2
      OTHERS       = 3.
  IF sy-subrc = 0.
    WRITE:
    (20) ls_field-datatype,
    (10)  ls_field-leng.
    IF ls_field-decimals IS INITIAL.
      WRITE (10)  ''.
    ELSE.
      WRITE (10)  ls_field-decimals.
    ENDIF.
  ENDIF.


ENDFORM.                    "get_ddic_ref_field


*&---------------------------------------------------------------------*
*&      Form  get_ddic_ref_field_xls
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_ROW     text
*      -->ID_DATA    text
*----------------------------------------------------------------------*
FORM get_ddic_ref_field_xls USING id_row id_data.
  DATA: ld_tabname   TYPE rmdi_name,
        ld_fieldname TYPE rmdi_fienm,
        ls_field     TYPE rmdi_tabfld.
  SPLIT id_data AT '-' INTO ld_tabname ld_fieldname.
  CALL FUNCTION 'RM_TABLE_INFO_GET'
    EXPORTING
      i_tabname    = ld_tabname
      i_fieldname  = ld_fieldname
      i_langu      = sy-langu
      i_read_texts = 'X'
    IMPORTING
*     E_TABHEADER  =
      e_tabfield_w = ls_field
*     E_TABFIELDS_T       =
    EXCEPTIONS
      not_found    = 1
      ddic_error   = 2
      OTHERS       = 3.
  IF sy-subrc = 0.
    go_excel->set_cell_data( id_col = 2 id_row = id_row id_value = ls_field-datatype ).
    go_excel->set_cell_data( id_col = 3 id_row = id_row id_value = ls_field-leng ).

    IF ls_field-decimals IS NOT INITIAL.
      go_excel->set_cell_data( id_col = 4 id_row = id_row id_value = ls_field-decimals ).
    ENDIF.
  ENDIF.

ENDFORM.                    "get_ddic_ref_field_xls


*&---------------------------------------------------------------------*
*&      Form  get_ddic_data_element
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_DATA    text
*      -->ED_MATCH   text
*----------------------------------------------------------------------*
FORM get_ddic_data_element USING id_data
                                 ed_match.

  DATA: ls_dd04v TYPE dd04v.
  DATA: ld_name TYPE ddobjname.
  ld_name = id_data.
  CALL FUNCTION 'DDIF_DTEL_GET'
    EXPORTING
      name          = ld_name
*     STATE         = 'A'
*     LANGU         = ' '
    IMPORTING
*     GOTSTATE      =
      dd04v_wa      = ls_dd04v
*     TPARA_WA      =
    EXCEPTIONS
      illegal_input = 1
      OTHERS        = 2.
  IF ls_dd04v-datatype IS NOT INITIAL.
    WRITE:
    (20) ls_dd04v-datatype,
    (10)  ls_dd04v-leng.
    IF ls_dd04v-decimals IS INITIAL.
      WRITE (10)  ''.
    ELSE.
      WRITE (10)  ls_dd04v-decimals.
    ENDIF.
    ed_match = 'X'.
  ELSE.
    READ TABLE gt_dd07v TRANSPORTING NO FIELDS WITH KEY domvalue_l = id_data
    BINARY SEARCH.
    IF sy-subrc = 0.
      ed_match = 'X'.
    ENDIF.
  ENDIF.

ENDFORM.                    "get_ddic_data_element


*&---------------------------------------------------------------------*
*&      Form  get_ddic_data_element_xls
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_ROW     text
*      -->ID_DATA    text
*      -->ED_MATCH   text
*----------------------------------------------------------------------*
FORM get_ddic_data_element_xls USING id_row TYPE i
                                     id_data
                                     ed_match.
  DATA: ls_dd04v TYPE dd04v.
  DATA: ld_name TYPE ddobjname.
  ld_name = id_data.
  CALL FUNCTION 'DDIF_DTEL_GET'
    EXPORTING
      name          = ld_name
*     STATE         = 'A'
*     LANGU         = ' '
    IMPORTING
*     GOTSTATE      =
      dd04v_wa      = ls_dd04v
*     TPARA_WA      =
    EXCEPTIONS
      illegal_input = 1
      OTHERS        = 2.
  IF ls_dd04v-datatype IS NOT INITIAL.
    go_excel->set_cell_data( id_col = 2 id_row = id_row id_value = ls_dd04v-datatype ).
    go_excel->set_cell_data( id_col = 3 id_row = id_row id_value = ls_dd04v-leng ).
    IF ls_dd04v-decimals IS NOT INITIAL.
      go_excel->set_cell_data( id_col = 4 id_row = id_row id_value = ls_dd04v-decimals ).
    ENDIF.
    ed_match = 'X'.
  ELSE.
    READ TABLE gt_dd07v TRANSPORTING NO FIELDS WITH KEY domvalue_l = id_data
    BINARY SEARCH.
    IF sy-subrc = 0.
      ed_match = 'X'.
    ENDIF.
  ENDIF.

ENDFORM.                    "get_ddic_data_element_xls
*&---------------------------------------------------------------------*
*&      Form  write_sturc
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->IT_STRUC   text
*----------------------------------------------------------------------*
FORM write_sturc USING it_struc TYPE tt_func.
  DATA: ls_struc   TYPE ts_func,
        ld_tabname TYPE rmdi_name,
        lt_field   TYPE rmdi_tabfld_t,
        ls_field   TYPE rmdi_tabfld.
  CHECK it_struc IS NOT INITIAL.

  SORT it_struc BY structure.
  DELETE ADJACENT DUPLICATES FROM it_struc COMPARING structure.

  WRITE /(30) '数据结构定义'.
  SKIP 1.
  LOOP AT it_struc INTO ls_struc.
*   校验是否是表类型
    DATA: ls_dd40l TYPE dd40l.
    SELECT SINGLE * FROM dd40l INTO ls_dd40l
    WHERE   typename = ls_struc-structure.
    IF sy-subrc = 0.
      ld_tabname = ls_dd40l-rowtype.
    ELSE.
      ld_tabname = ls_struc-structure.
    ENDIF.

    FORMAT INTENSIFIED COLOR = 4.
    WRITE :/(30) ls_struc-structure.
    "获取描述
    DATA: ld_ddtext TYPE dd02t-ddtext.
    SELECT SINGLE ddtext FROM  dd02t INTO ld_ddtext
    WHERE tabname = ld_tabname.
    IF sy-subrc = 0.
      WRITE (40) ld_ddtext.
    ENDIF.
    FORMAT INTENSIFIED COLOR = 0.
*    WRITE sy-uline.

    CALL FUNCTION 'RM_TABLE_INFO_GET'
      EXPORTING
        i_tabname     = ld_tabname
*       I_FIELDNAME   =
*       I_LANGU       =
        i_read_texts  = 'X'
      IMPORTING
*       E_TABHEADER   =
*       E_TABFIELD_W  =
        e_tabfields_t = lt_field
      EXCEPTIONS
        not_found     = 1
        ddic_error    = 2
        OTHERS        = 3.
    IF sy-subrc = 0.
      LOOP AT lt_field INTO ls_field.
        WRITE: /(30) ls_field-fieldname ,
        (20) ls_field-datatype,
        (10) ls_field-leng.
        IF ls_field-decimals IS INITIAL.
          WRITE (10)  ''.
        ELSE.
          WRITE (10)  ls_field-decimals.
        ENDIF.
        WRITE : (40) ls_field-fieldtext.
      ENDLOOP.
    ENDIF.
    SKIP 2.
  ENDLOOP.

ENDFORM.                    "write_sturc


*&---------------------------------------------------------------------*
*&      Form  write_sturc_xls
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->CD_ROW     text
*      -->IT_STRUC   text
*----------------------------------------------------------------------*
FORM write_sturc_xls USING cd_row TYPE i
                           it_struc TYPE tt_func.
  DATA: ls_struc   TYPE ts_func,
        ld_tabname TYPE rmdi_name,
        lt_field   TYPE rmdi_tabfld_t,
        ls_field   TYPE rmdi_tabfld.
  CHECK it_struc IS NOT INITIAL.

  SORT it_struc BY structure.
  DELETE ADJACENT DUPLICATES FROM it_struc COMPARING structure.

  cd_row = cd_row + 2.
  go_excel->set_cell_data( id_col = 1 id_row = cd_row id_value = '数据结构定义' ).
  go_excel->set_cell_format( id_col = 1 id_row = cd_row id_bold = 1  id_bg_color = 36 ).


  LOOP AT it_struc INTO ls_struc.
    cd_row = cd_row + 2.
*   校验是否是表类型
    DATA: ls_dd40l TYPE dd40l.
    SELECT SINGLE * FROM dd40l INTO ls_dd40l
    WHERE   typename = ls_struc-structure.
    IF sy-subrc = 0.
      ld_tabname = ls_dd40l-rowtype.
    ELSE.
      ld_tabname = ls_struc-structure.
    ENDIF.

    go_excel->set_cell_data( id_col = 1 id_row = cd_row id_value = ls_struc-structure ).
    go_excel->set_cell_format( id_col = 1 id_row = cd_row id_bold = 1 id_bg_color = 36 ).

    "获取描述
    DATA: ld_ddtext TYPE dd02t-ddtext.
    SELECT SINGLE ddtext FROM  dd02t INTO ld_ddtext
    WHERE tabname = ld_tabname.
    IF sy-subrc = 0.
      go_excel->set_cell_data( id_col = 5 id_row = cd_row id_value = ld_ddtext ).
    ENDIF.

    CALL FUNCTION 'RM_TABLE_INFO_GET'
      EXPORTING
        i_tabname     = ld_tabname
*       I_FIELDNAME   =
*       I_LANGU       =
        i_read_texts  = 'X'
      IMPORTING
*       E_TABHEADER   =
*       E_TABFIELD_W  =
        e_tabfields_t = lt_field
      EXCEPTIONS
        not_found     = 1
        ddic_error    = 2
        OTHERS        = 3.
    IF sy-subrc = 0.
      LOOP AT lt_field INTO ls_field.
        cd_row = cd_row + 1.

        go_excel->set_cell_data( id_col = 1 id_row = cd_row id_value = ls_field-fieldname ).
        go_excel->set_cell_data( id_col = 2 id_row = cd_row id_value = ls_field-datatype ).
        go_excel->set_cell_data( id_col = 3 id_row = cd_row id_value = ls_field-leng ).

        IF ls_field-decimals IS NOT INITIAL.
          go_excel->set_cell_data( id_col = 4 id_row = cd_row id_value = ls_field-decimals ).
        ENDIF.

        go_excel->set_cell_data( id_col = 5 id_row = cd_row id_value = ls_field-fieldtext ).
      ENDLOOP.
    ENDIF.
  ENDLOOP.

ENDFORM.                    "write_sturc_xls

*&---------------------------------------------------------------------*
*& 包含               YFUNC_EXCEL
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&  包含                YFUNC_EXCEL
*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*&  包括                YFUNC_EXCEL
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&  包含                ZUTIL_EXCEL_V2
*&  公用模块
*&  &! 多程序共用  谨慎修改
*& [MOD-01]      LUBIN        08.09.2016 15:08:34  取消自动回行 影响输出效率
*                                                  所有cell操作增加缓存选项
*                                                  1W个CELL操作 自动输出一次
*  [MOD-02]       lubin       08.12.2017 16:51:26  所有的OLE操作都参与缓存统计
*                                                  而不仅仅是MOD-01中的CELL操作
*&--------------------------------------------------------------- ------*

DEFINE m_action.
  md_action = md_action + 1.

IF md_action = 10000.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
        cntl_system_error = 1
        cntl_error        = 2.
CLEAR md_action.
ENDIF.
END-OF-DEFINITION.
*----------------------------------------------------------------------*
*       CLASS lcl_concatenator DEFINITION
*----------------------------------------------------------------------*
* 由于SAP版本问题 不支持&& 操作符 引入一个concatenator类
*----------------------------------------------------------------------*
CLASS lcl_concatenator DEFINITION.
  PUBLIC SECTION.
    METHODS:
      refresh,
      append IMPORTING id_value TYPE any,
      output IMPORTING id_sperator      TYPE any OPTIONAL
             RETURNING VALUE(rd_result) TYPE string.

  PRIVATE SECTION.
    DATA: ms_text TYPE string,
          mt_text TYPE TABLE OF string.
ENDCLASS.                    "lcl_concatenator DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_concatenator IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_concatenator IMPLEMENTATION.
  METHOD refresh.
    CLEAR mt_text.
  ENDMETHOD.                    "refresh

  METHOD append.
    ms_text = id_value.
    CONDENSE ms_text.
    APPEND ms_text TO mt_text.
  ENDMETHOD.                    "append

  METHOD output.
    LOOP AT mt_text INTO ms_text.
      IF rd_result IS INITIAL.
        rd_result = ms_text.
      ELSE.
        CONCATENATE  rd_result id_sperator ms_text INTO rd_result .
      ENDIF.
    ENDLOOP.
  ENDMETHOD.                    "output
ENDCLASS.                    "lcl_concatenator IMPLEMENTATION


TYPE-POOLS:ole2.

*----------------------------------------------------------------------*
*       CLASS lcl_excel DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_excel DEFINITION.
  PUBLIC SECTION.
    METHODS:
      constructor     IMPORTING id_xlsfile  TYPE any OPTIONAL"excel文件 绝对路径
                                id_visiable TYPE n DEFAULT '0',"可见性: 0 不可见 1 可见
      get_ole_obj     IMPORTING id_obj_name TYPE any
                      EXPORTING eo_obj      TYPE ole2_object,
*<!--  sheet  -->
      select_sheet    IMPORTING   id_pos       TYPE i OPTIONAL ,"sheet number


      add_sheet       IMPORTING id_pos       TYPE i OPTIONAL
                                id_after     TYPE char1 OPTIONAL"X 位置在指定sheet之后 '
                                id_sheetname TYPE any OPTIONAL,

      rename_sheet    IMPORTING id_pos       TYPE i OPTIONAL
                                id_sheetname TYPE any OPTIONAL,

      copy_sheet      IMPORTING id_pos           TYPE i OPTIONAL
                                id_new_sheetname TYPE any OPTIONAL,

*<!--  cell  -->
      select_cell   IMPORTING id_row TYPE any
                              id_col TYPE any,

      set_cell_data IMPORTING id_row                  TYPE any OPTIONAL
                              id_col                  TYPE any OPTIONAL
                              id_value                TYPE any
                              id_just                 TYPE any OPTIONAL "对齐方式
*                              id_wrap   TYPE any DEFAULT  1
                              id_wrap                 TYPE any OPTIONAL

                              id_output_initial_value TYPE any DEFAULT '',


      set_cell_format IMPORTING id_row       TYPE any OPTIONAL
                                id_col       TYPE any OPTIONAL

                                id_just      TYPE any OPTIONAL "1:右 2:左 3:中心
                                id_wrap      TYPE any OPTIONAL "1:自动回行
                                id_font_name TYPE any OPTIONAL"字体名
                                id_bold      TYPE any OPTIONAL"1: 加粗
                                id_italic    TYPE any OPTIONAL"1: 斜体
                                id_color     TYPE any OPTIONAL"颜色
                                id_size      TYPE any OPTIONAL"字号
                                id_bg_color  TYPE any OPTIONAL,


*<!--  range  -->
      select_range IMPORTING id_start_row TYPE any OPTIONAL
                             id_start_col TYPE any OPTIONAL
                             id_end_row   TYPE any OPTIONAL
                             id_end_col   TYPE any OPTIONAL
                             id_range     TYPE any OPTIONAL"range (A1:A1)
                             id_num_2_en  TYPE char1 DEFAULT 'X', "是否进行数字到字母的转换

      set_range_format IMPORTING
                         id_just      TYPE any OPTIONAL
                         id_wrap      TYPE any OPTIONAL
                         id_font_name TYPE any OPTIONAL
                         id_bold      TYPE any OPTIONAL
                         id_italic    TYPE any OPTIONAL
                         id_color     TYPE any OPTIONAL
                         id_size      TYPE any OPTIONAL
                         id_bg_color  TYPE any OPTIONAL,

      "合并
      merge_range,

      "边框
      set_range_border IMPORTING id_all   TYPE any  DEFAULT 'X'
                                 id_left  TYPE any OPTIONAL
                                 id_right TYPE any OPTIONAL
                                 id_top   TYPE any OPTIONAL
                                 id_down  TYPE any OPTIONAL,
      "清空
      clear_range_data,

      "复制
      copy_range,
      paste_range,

*<!--  row  -->
      set_row_height IMPORTING id_row_no TYPE any
                               id_height TYPE any ,

      insert_rows   IMPORTING id_row_no    TYPE any
                              id_row_count TYPE i DEFAULT 1,
      frozen_window IMPORTING id_row_no TYPE any,


*<!--  column  -->
      set_col_width IMPORTING id_col_no TYPE any
                              id_width  TYPE any OPTIONAL,

*<!--  save & exit -->
      save IMPORTING id_open TYPE char1 DEFAULT 'X',
      terminate.

    METHODS:
      create_excel
        IMPORTING
          id_xlsfile TYPE any
          id_visible TYPE n,

*    CLASS-METHODS:
*     excel 列由数值->字母
      transform_num_to_en IMPORTING id_col_num       TYPE any
                          RETURNING VALUE(rd_col_en) TYPE char10,
*     产生range格式数据
      generate_range IMPORTING id_start_row    TYPE any
                               id_start_col    TYPE any
                               id_end_row      TYPE any
                               id_end_col      TYPE any
                     RETURNING VALUE(rd_range) TYPE char50.

    CLASS-DATA:
          md_en_sequence TYPE char26 VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

  PRIVATE SECTION.

    DATA:
      md_xlsfile       TYPE string,
      md_file_exist(1),
      md_cell_count    TYPE i.

    DATA: md_action TYPE i. "[MOD-02] 缓存的操作数

    DATA: mo_concatenator TYPE REF TO lcl_concatenator.

    DATA: excel    TYPE ole2_object,
          window   TYPE ole2_object,
          book     TYPE ole2_object,
          sheets   TYPE ole2_object,
          sheet    TYPE ole2_object,
          cell     TYPE ole2_object,
          row      TYPE ole2_object,
          col      TYPE ole2_object,
          range    TYPE ole2_object,
          borders  TYPE ole2_object,
          font     TYPE ole2_object,
          interior TYPE ole2_object.
ENDCLASS.                    "lcl_excel DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_excel IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_excel IMPLEMENTATION.
  METHOD constructor.
    IF id_xlsfile IS NOT INITIAL.
      md_xlsfile = id_xlsfile.  "文件名赋给私有变量

      "-检查文件是否存在
      DATA: ld_fname TYPE string,
            ld_rst   TYPE c.
      ld_fname = md_xlsfile.
      md_file_exist = 'X'.
      CALL METHOD cl_gui_frontend_services=>file_exist
        EXPORTING
          file                 = ld_fname
        RECEIVING
          result               = ld_rst
        EXCEPTIONS
          cntl_error           = 1
          error_no_gui         = 2
          wrong_parameter      = 3
          not_supported_by_gui = 4
          OTHERS               = 5.
      IF sy-subrc <> 0 OR ld_rst NE 'X' .
        md_file_exist = space.       "文件不存在
      ENDIF.
    ENDIF.

    "新建或打开Excel文件
    CALL METHOD create_excel
      EXPORTING
        id_xlsfile = md_xlsfile
        id_visible = id_visiable.

    CREATE OBJECT mo_concatenator.
  ENDMETHOD.                    "constructor



  METHOD create_excel.
    CREATE OBJECT excel 'EXCEL.APPLICATION'.
    IF sy-subrc NE 0.
      MESSAGE 'EXCEL创建错误' TYPE 'S' DISPLAY LIKE 'E'.
      EXIT.
    ENDIF.
    SET PROPERTY OF excel 'VISIBLE' = id_visible.
    CALL METHOD OF
      excel
        'WORKBOOKS' = book.
    IF md_file_exist = 'X'.
      CALL METHOD OF
        book
        'OPEN'
        EXPORTING
          #1 = id_xlsfile.

      select_sheet(  ).

    ELSE.
      SET PROPERTY OF excel 'SheetsInNewWorkbook' = 1.
      CALL METHOD OF
        book
          'ADD' = sheet.
      CALL METHOD OF
        sheet
        'ACTIVATE'.
*      FREE OBJECT sheet.
    ENDIF.

  ENDMETHOD.                    "create_excel

  METHOD get_ole_obj.
    CASE id_obj_name.
      WHEN 'EXCEL'.
        eo_obj = excel.
      WHEN 'BOOK'.
        eo_obj = book.
      WHEN 'SHEETS'.
        eo_obj = sheets.
      WHEN 'SHEET'.
        eo_obj = sheet.
      WHEN 'CELL'.
        eo_obj = cell.
      WHEN 'ROW'.
        eo_obj = row.
      WHEN 'COL'.
        eo_obj = col.
      WHEN 'RANGE'.
        eo_obj = range.
    ENDCASE.
  ENDMETHOD.                    "get_ole_obj

  METHOD select_sheet.
    FREE OBJECT sheet.

    m_action.

    IF sheets IS INITIAL.
      GET PROPERTY OF excel 'ActiveWorkbook' = book.
      CALL METHOD OF
        book
          'WORKSHEETS' = sheets.

    ENDIF.

    IF id_pos IS NOT INITIAL.

      CALL METHOD OF
              sheets
      'Item' = sheet
      EXPORTING
              #1 = id_pos.

      CALL METHOD OF
        sheet
        'ACTIVATE'.
    ELSE.
      CALL METHOD OF
        excel
          'ActiveSheet' = sheet.
    ENDIF.
  ENDMETHOD.                    "select_sheet

  METHOD add_sheet.
    DATA: lo_sheet_pointed TYPE ole2_object,
          lo_sheet_add     TYPE ole2_object.


    select_sheet( id_pos ).

    lo_sheet_pointed = sheet.

    CALL METHOD OF
          sheets
    'Add' = lo_sheet_add
    EXPORTING
          #1 = lo_sheet_pointed.

    IF id_sheetname IS NOT INITIAL.
      SET PROPERTY OF lo_sheet_add 'NAME' = id_sheetname NO FLUSH.
      m_action.
    ENDIF.

    IF id_after = 'X'.
      CALL METHOD OF
        lo_sheet_pointed
        'MOVE'
        NO FLUSH
        EXPORTING
          #1 = lo_sheet_add.
      m_action.
    ENDIF.

    CALL METHOD OF
      lo_sheet_add
      'ACTIVATE' NO FLUSH.
    m_action.

    FREE OBJECT lo_sheet_pointed NO FLUSH.
    m_action.

    sheet    = lo_sheet_add.

  ENDMETHOD.                    "add_sheet


  METHOD rename_sheet.

    select_sheet( id_pos ).

    SET PROPERTY OF sheet 'NAME' = id_sheetname NO FLUSH.
    m_action.

  ENDMETHOD.                    "rename_sheet

  METHOD copy_sheet.


  ENDMETHOD.                    "copy_sheet


  METHOD select_cell .
    CHECK id_row IS NOT INITIAL AND
              id_col IS NOT INITIAL.


    IF cell IS NOT INITIAL.
*     处理新的cell之前 先释放旧的cell的句柄
      FREE OBJECT cell NO FLUSH.
      m_action.
    ENDIF.

    CALL METHOD OF
          excel
    'Cells' = cell
    NO
          FLUSH
    EXPORTING
          #1 = id_row
          #2 = id_col.
    m_action.

  ENDMETHOD.                    "select_cell

  METHOD set_cell_data.
    IF id_output_initial_value = ''.
*     是否输出初始值
      CHECK id_value IS NOT INITIAL.
    ENDIF.

    select_cell( id_row = id_row id_col = id_col ).

    SET PROPERTY OF cell 'value' = id_value NO FLUSH.
    m_action.

    IF id_just IS NOT INITIAL.
      SET PROPERTY OF cell 'HorizontalAlignment' = id_just NO FLUSH.
      m_action.
    ENDIF.

    IF id_wrap IS NOT INITIAL.
      SET PROPERTY OF cell 'WrapText' = id_wrap NO FLUSH."设置自动换行 如果在模板中已经设置了自动换行 则该属性不起效
      m_action.
    ENDIF.

    m_action.

    "[MOD-01]  "[MOD-02]
***    md_cell_count = md_cell_count + 1.
***    IF md_cell_count = 10000.
***      CALL METHOD cl_gui_cfw=>flush
***        EXCEPTIONS
***          cntl_system_error = 1
***          cntl_error        = 2.
***      CLEAR md_cell_count.
***    ENDIF.
  ENDMETHOD.                    "set_cell_data

  METHOD set_cell_format.
    select_cell( id_row = id_row id_col = id_col ).

    IF id_just IS NOT INITIAL.
      SET PROPERTY OF cell 'HORIZONTALALIGNMENT' = id_just NO FLUSH.
      m_action.
    ENDIF.

    IF id_wrap IS NOT INITIAL.
      SET PROPERTY OF cell 'WrapText' = id_wrap NO FLUSH.
      m_action.
    ENDIF.

    CALL METHOD OF
      cell
        'FONT' = font
                 NO
                 FLUSH.
    m_action.


    IF id_bold IS NOT INITIAL.
      SET PROPERTY OF font 'BOLD' = id_bold NO FLUSH.
      m_action.
    ENDIF.
    IF id_italic IS NOT INITIAL.
      SET PROPERTY OF font 'Italic' = id_italic NO FLUSH.
      m_action.
    ENDIF.

    IF id_size IS NOT INITIAL.
      SET PROPERTY OF font 'SIZE' = id_size NO FLUSH.
      m_action.
    ENDIF.

    IF id_font_name IS NOT  INITIAL.
      SET PROPERTY OF font 'NAME' = id_font_name NO FLUSH.
      m_action.
    ENDIF.

    IF id_color IS NOT INITIAL.
      SET PROPERTY OF font 'COLORINDEX' = id_color NO FLUSH.
      m_action.
    ENDIF.
    FREE OBJECT font NO FLUSH.
    m_action.

    IF id_bg_color IS NOT INITIAL.
      CALL METHOD OF
        cell
          'Interior' = interior
                       NO FLUSH.
      m_action.

      SET PROPERTY OF interior 'COLORINDEX' = id_bg_color NO FLUSH.
      m_action.
      FREE OBJECT interior NO FLUSH.
      m_action.
    ENDIF.


  ENDMETHOD.                    "set_cell_format


  METHOD select_range.
    DATA: ld_range TYPE string.
    IF id_range IS NOT INITIAL.
      ld_range = id_range.
    ELSE.

      IF id_num_2_en = 'X'.
        ld_range = generate_range(
                      id_start_row = id_start_row
                      id_start_col = id_start_col
                      id_end_row = id_end_row
                      id_end_col = id_end_col ).
      ELSE.
*        ld_range = id_start_col && id_start_row && ':' &&
*                   id_end_col && id_end_row .

        mo_concatenator->refresh( ).
        mo_concatenator->append( id_start_col ).
        mo_concatenator->append( id_start_row ).
        mo_concatenator->append( ':' ).
        mo_concatenator->append( id_end_col ).
        mo_concatenator->append( id_end_row ).
        ld_range = mo_concatenator->output( ).
      ENDIF.
    ENDIF.


    IF range IS NOT INITIAL.
      FREE OBJECT range NO FLUSH.
      m_action.
    ENDIF.

    CALL METHOD OF
          excel
    'RANGE' = range
    NO FLUSH
    EXPORTING
          #1 = ld_range .
    m_action.

  ENDMETHOD.                    "select_range

  METHOD set_range_format .

    IF id_just IS NOT INITIAL.
      SET PROPERTY OF range 'HORIZONTALALIGNMENT' = id_just NO FLUSH.
      m_action.
    ENDIF.

    IF id_wrap IS NOT INITIAL.
      SET PROPERTY OF range 'WrapText' = id_wrap NO FLUSH.
      m_action.
    ENDIF.

    CALL METHOD OF
      range
        'FONT' = font NO FLUSH.
    m_action.


    IF id_bold IS NOT INITIAL.
      SET PROPERTY OF font 'BOLD' = id_bold NO FLUSH.
      m_action.
    ENDIF.
    IF id_italic IS NOT INITIAL.
      SET PROPERTY OF font 'Italic' = id_italic NO FLUSH.
      m_action.
    ENDIF.

    IF id_size IS NOT INITIAL.
      SET PROPERTY OF font 'SIZE' = id_size NO FLUSH.
      m_action.
    ENDIF.

    IF id_font_name IS NOT  INITIAL.
      SET PROPERTY OF font 'NAME' = id_font_name NO FLUSH.
      m_action.
    ENDIF.

    IF id_color IS NOT INITIAL.
      SET PROPERTY OF font 'COLORINDEX' = id_color NO FLUSH.
      m_action.
    ENDIF.
    FREE OBJECT font NO FLUSH.


    IF id_bg_color IS NOT INITIAL.
      CALL METHOD OF
        range
          'Interior' = interior
                       NO FLUSH.
      m_action.

      SET PROPERTY OF interior 'COLORINDEX' = id_bg_color NO FLUSH.
      m_action.
      FREE OBJECT interior NO FLUSH.
      m_action.
    ENDIF.

  ENDMETHOD.                    "set_range_format

  METHOD merge_range.
    SET PROPERTY OF range 'Merge' = 1 NO FLUSH.
    m_action.

  ENDMETHOD.                    "merge_range


  METHOD set_range_border.
    DATA:ld_line_no TYPE n.

    IF id_all = 'X'.
      CALL METHOD OF
        range
          'BORDERS' = borders NO FLUSH.
      m_action.
    ELSE.
      CASE 'X'.
        WHEN id_left.
          ld_line_no = 1.
        WHEN id_right.
          ld_line_no = 2.
        WHEN id_top.
          ld_line_no = 3.
        WHEN id_down.
          ld_line_no = 4.
      ENDCASE..

      CALL METHOD OF
              range
      'BORDERS' = borders
      NO FLUSH
      EXPORTING
              #1 = ld_line_no.
      m_action.
    ENDIF.

    SET PROPERTY OF borders 'LINESTYLE' = 1 NO FLUSH.
    m_action.

    SET PROPERTY OF borders 'WEIGHT' = 2 NO FLUSH.
    m_action.

    FREE OBJECT borders NO FLUSH.
  ENDMETHOD.                    "set_range_border

  METHOD clear_range_data.
    CALL METHOD OF
      range
      'ClearContents' NO FLUSH.
    m_action.
  ENDMETHOD.                    "clear_range_data

  METHOD copy_range.
    CALL METHOD OF
      range
      'copy' NO FLUSH.
    m_action.
  ENDMETHOD.                    "copy_range

  METHOD paste_range.
    CALL METHOD OF
      range
      'PasteSpecial' NO FLUSH.
    m_action.
  ENDMETHOD.                    "paste_range

  METHOD set_row_height.
    CALL METHOD OF
          excel
    'ROWS' = row
    NO FLUSH
    EXPORTING
          #1 = id_row_no.
    m_action.
    SET PROPERTY OF row  'RowHeight' = id_height NO FLUSH.
    m_action.

    FREE OBJECT row NO FLUSH.
    m_action.

  ENDMETHOD.                    "set_row_height

  METHOD insert_rows.
    CALL METHOD OF
          excel
    'ROWS' = row
    NO FLUSH
    EXPORTING
          #1 = id_row_no.
    m_action.

    DO id_row_count TIMES.
      CALL METHOD OF
        row
        'INSERT' NO FLUSH.
      m_action.

    ENDDO.

    FREE OBJECT row NO FLUSH.
    m_action.
  ENDMETHOD.                    "insert_rows

  METHOD frozen_window.
    CALL METHOD OF
          excel
    'ROWS' = row
    NO FLUSH
    EXPORTING
          #1 = id_row_no.

    CALL METHOD OF
      row
      'SELECT'
      NO FLUSH.

    m_action.

    CALL METHOD OF
      excel
        'ActiveWindow' = window
                         NO FLUSH.

    SET PROPERTY OF  window 'FreezePanes' = 'True' NO FLUSH.
    m_action.

    FREE OBJECT row NO FLUSH.
    FREE OBJECT window NO FLUSH.
    m_action.
  ENDMETHOD.                    "frozen_window

  METHOD set_col_width.
    CALL METHOD OF
          excel
    'COLUMNS' = col NO FLUSH
    EXPORTING
          #1 = id_col_no.
    m_action.
    IF id_width IS INITIAL.
      CALL METHOD OF
        col
        'AutoFit' NO FLUSH.
      m_action.
    ELSE.
      SET PROPERTY OF col  'columnwidth' = id_width NO FLUSH.
      m_action.
    ENDIF.
    FREE OBJECT col NO FLUSH.
    m_action.
  ENDMETHOD.                    "set_colwidth


  METHOD save.
    GET PROPERTY OF excel 'ActiveWorkbook' = book.
    SET PROPERTY OF excel 'DisplayAlerts' = 0.

    IF md_file_exist = 'X'.
      "打开已有文件情况下
      CALL METHOD OF
        book
        'SAVE'.
    ELSE.
      "新建文件情况下
      CALL METHOD OF
        book
        'SAVEAS'
        EXPORTING
          #1 = md_xlsfile  "文件完整路径
          #2 = 1. "0:不覆盖不提示,进程不结束 1:提示,若是则进程结束
    ENDIF.

    CALL METHOD OF
      book
      'CLOSE'.
    CALL METHOD OF
      excel
      'QUIT'.
    FREE OBJECT: excel, book, sheet.


* 自动打开导出文件
    IF id_open = 'X'.
      CALL METHOD cl_gui_frontend_services=>execute
        EXPORTING
          document               = md_xlsfile
*         application            =
*         parameter              =
*         default_directory      =
*         maximized              =
*         minimized              =
*         synchronous            =
*         operation              = 'OPEN'
        EXCEPTIONS
          cntl_error             = 1
          error_no_gui           = 2
          bad_parameter          = 3
          file_not_found         = 4
          path_not_found         = 5
          file_extension_unknown = 6
          error_execute_failed   = 7
          synchronous_failed     = 8
          not_supported_by_gui   = 9
          OTHERS                 = 10.
    ENDIF.

  ENDMETHOD.                    "save_excel

  METHOD terminate.
*   适用于用代码自动填充一个模板后
*   一个未保存的excel的场景

*   excel可见
    SET PROPERTY OF excel 'VISIBLE' = 1.

*   清空相关句柄
    FREE OBJECT: excel, book, sheet.
  ENDMETHOD.                    "terminate

  METHOD transform_num_to_en.
    DATA: ld_offset   TYPE i,
          ld_quotient TYPE i,
          ld_input    TYPE i.
    ld_input = id_col_num.
*   余数
    ld_offset = ld_input MOD 26.
*   商数
    ld_quotient = ( ld_input - ld_offset ) / 26.
    IF ld_quotient > 0.
*     商数 > 0 递归
*      rd_col_en = rd_col_en && transform_num_to_en( ld_quotient ).

      mo_concatenator->refresh( ).
      mo_concatenator->append( rd_col_en ).
      DATA: ld_en TYPE string.
      ld_en = transform_num_to_en( ld_quotient ).
      mo_concatenator->append( ld_en ).
      rd_col_en = mo_concatenator->output( ).

    ENDIF.

    ld_offset = ld_offset - 1.

*    rd_col_en = rd_col_en && md_en_sequence+ld_offset(1).

    mo_concatenator->refresh( ).
    mo_concatenator->append( rd_col_en ).
    mo_concatenator->append( md_en_sequence+ld_offset(1) ).
    rd_col_en = mo_concatenator->output( ).
  ENDMETHOD.                    "transform_num_to_en

  METHOD generate_range.
*    rd_range = transform_num_to_en( id_start_col ) &&
*               id_start_row && ':' &&
*               transform_num_to_en( id_end_col ) &&
*               id_end_row.

    mo_concatenator->refresh( ).
    DATA: ld_col TYPE string.
    ld_col = transform_num_to_en( id_start_col ).
    mo_concatenator->append( ld_col ).
    mo_concatenator->append( id_start_row  ).
    mo_concatenator->append( ':'  ).

    ld_col = transform_num_to_en( id_end_col ).
    mo_concatenator->append( ld_col ).
    mo_concatenator->append( id_end_row ).
    rd_range = mo_concatenator->output( ).

  ENDMETHOD.                    "generate_range

ENDCLASS.                    "lcl_excel IMPLEMENTATION

 

posted @ 2024-01-15 14:13  阿胖的阿多  阅读(66)  评论(0编辑  收藏  举报