FM方式实现ALV弹出明细,并下载到excel



用函数REUSE_ALV_POPUP_TO_SELECT来显示ALV明细信息,并通过I_CALLBACK_USER_COMMAND参数实现导出数据到文件


 CALL FUNCTION 'REUSE_ALV_POPUP_TO_SELECT'
    EXPORTING
      i_title               '明细'
     I_SELECTION           'X'
*     I_ALLOW_NO_SELECTION  =
      i_zebra               'X'
      i_screen_start_column 10
      i_screen_start_line   10
      i_screen_end_column   150
      i_screen_end_line     30
*     I_CHECKBOX_FIELDNAME  =
*     I_LINEMARK_FIELDNAME  =
     I_SCROLL_TO_SEL_LINE  'X'
      i_tabname             space
*     I_STRUCTURE_NAME      =
      it_fieldcat           it_fieldcat[]
 "     IT_EXCLUDING          = IT_EXTAB[]
     I_CALLBACK_PROGRAM    ''
     I_CALLBACK_USER_COMMAND       'FRM_ALV_USER_COMMAND_POPUP'
*     IS_PRIVATE            =
*  IMPORTING
*     ES_SELFIELD           =
*     E_EXIT                =
    TABLES
      t_outtab              it_bseg_out[]
*  EXCEPTIONS
*     PROGRAM_ERROR         = 1
*     OTHERS                = 2
    .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      FORM  FRM_ALV_USER_COMMAND_POPUP
*&---------------------------------------------------------------------*
FORM FRM_ALV_USER_COMMAND_POPUP USING PA_UCOMM TYPE SY-UCOMM
                                      PWA_SELFIELD TYPE SLIS_SELFIELD.
  DATAL_FILENAME TYPE STRING,
        L_PATH TYPE STRING,
        L_FULLPATH TYPE STRING,
        L_ACTION TYPE I.
  TYPESBEGIN OF STRUC_TABLE,
           BUKRS TYPE CHAR20,
           BLART TYPE CHAR20,
           BELNR TYPE CHAR20,
           BLDAT TYPE CHAR20,
           BUDAT TYPE CHAR20,
           BSCHL TYPE CHAR20,
           HKONT TYPE CHAR20,
           SAKNR TYPE CHAR30,
           KUNNR TYPE CHAR20,
           K_NAME1  TYPE CHAR30,
            LIFNR TYPE CHAR20,
            L_NAME1 TYPE CHAR30,
            SGTXT TYPE CHAR30,
            HSL TYPE CHAR20,
            RSTGR TYPE CHAR20,
            TXT20 TYPE CHAR30,
            XREF1 TYPE CHAR30,
            ZWLLLMS TYPE CHAR30,
            USNAM TYPE CHAR20,
         END OF STRUC_TABLE.
  DATALS_TAB TYPE STRUC_TABLE,
        LT_TAB TYPE TABLE OF STRUC_TABLE.


  CASE PA_UCOMM.
    WHEN '&ETA'. “第二个按钮
      CLEAR PA_UCOMM.


      CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
        EXPORTING
          FILE_FILTER          'Excel Files (*.xls,*.xlsx)|*.xls;*.xlsx'
        CHANGING
          FILENAME             L_FILENAME
          PATH                 L_PATH
          FULLPATH             L_FULLPATH
          USER_ACTION          L_ACTION
        EXCEPTIONS
          CNTL_ERROR           1
          ERROR_NO_GUI         2
          NOT_SUPPORTED_BY_GUI 39
          OTHERS               4.
      IF SY-SUBRC <> 0.
        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                   WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.

      LS_TAB-BUKRS '公司代码'.
      LS_TAB-BLART '凭证类型'.
      LS_TAB-BELNR '会计凭证编号'.
      LS_TAB-BLDAT '凭证日期'.
      LS_TAB-BUDAT '过账日期'.
      LS_TAB-BSCHL '记帐代码'.
      LS_TAB-HKONT '科目'.
      LS_TAB-SAKNR '科目描述'.
      LS_TAB-KUNNR '客户'.
      LS_TAB-K_NAME1 '客户描述'.
      LS_TAB-LIFNR '供应商'.
      LS_TAB-L_NAME1 '供应商描述'.
      LS_TAB-SGTXT '文本'.
      LS_TAB-HSL '本币金额'.
      LS_TAB-RSTGR '原因代码'.
      LS_TAB-TXT20 '原因代码描述'.
      LS_TAB-XREF1 '参考代码 1'.
      LS_TAB-ZWLLLMS '参考代码 1描述'.
      LS_TAB-USNAM '制单人'.
      INSERT LS_TAB INTO LT_TAB INDEX 1.
      CLEAR LS_TAB.

      loop at it_bseg_out.
         move-corresponding it_bseg_out to ls_tab.
         CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
           CHANGING
             value         ls_tab-hsl
                   .
          condense ls_tab-hsl no-gaps.
         append ls_tab to lt_tab.
      endloop.

      CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
        EXPORTING
          FILENAME                L_FULLPATH
          WRITE_FIELD_SEPARATOR   'X'
        CHANGING
          DATA_TAB                =  LT_TAB
        EXCEPTIONS
          FILE_WRITE_ERROR        1
          NO_BATCH                2
          GUI_REFUSE_FILETRANSFER 3
          INVALID_TYPE            4
          NO_AUTHORITY            5
          UNKNOWN_ERROR           6
          HEADER_NOT_ALLOWED      7
          SEPARATOR_NOT_ALLOWED   8
          FILESIZE_NOT_ALLOWED    9
          HEADER_TOO_LONG         10
          DP_ERROR_CREATE         11
          DP_ERROR_SEND           12
          DP_ERROR_WRITE          13
          UNKNOWN_DP_ERROR        14
          ACCESS_DENIED           15
          DP_OUT_OF_MEMORY        16
          DISK_FULL               17
          DP_TIMEOUT              18
          FILE_NOT_FOUND          19
          DATAPROVIDER_EXCEPTION  20
          CONTROL_FLUSH_ERROR     21
          NOT_SUPPORTED_BY_GUI    22
          ERROR_NO_GUI            23
          OTHERS                  24.
      IF SY-SUBRC <> 0.
        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                   WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
  ENDCASE.




ENDFORM.    " FRM_ALV_USER_COMMAND_POPUP

你可能感兴趣的:(ABAP-ALV)