• ABAP上载Excel


     

     

    消息弹出框(选择是/否)

    POPUP_TO_CONFIRM_STEP

    选择路径

    WS_FILENAME_GET

    CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG

    SMWO下载

    SAP_OI_LOAD_MIME_DATA

    DOWNLOAD_WEB_OBJECT

    上载Excel

    TEXT_CONVERT_XLS_TO_SAP(先对应导入Excel创建内表)

    ALSM_EXCEL_TO_INTERNAL_TABLE(按行列导入,或不填行列全导入)

    ZEXCEL_TO_INTERNAL_TABLE(自定义,添加sheet选择,增加输出)

     

     

    **&*********************************************************************
    *& PROGRAM NAME        : 导入                             *
    *& Module Name         :                                *
    *& Apply Author        : XXX                              *
    *& Author              :                              *
    *& Started on          : 2018-05-15                                                 *
    *& Transaction         :                                *
    *& Program type        : Report                            *
    *& SAP Release         : 46C/ECC 6.0                         *
    *& Program ID          :                               *
    *& Program Description :                                    *
    *&     导入excel 模板由SMW0导入
    *&*&*******************************************************************
    *& REVISION LOG                                        
    *&                                                *
    *& LOG#    DATE       AUTHOR       DESCRIPTION                *
    *& ----    ----       ------       -----------                *
    *&*********************************************************************
    REPORT ZTEST023.

    TYPE-POOLS: SLIS,VRM,TRUXS.
    TABLES: sscrfields."选择屏幕上的字段
    *----------------------------------------------------------------------
    * types
    *----------------------------------------------------------------------
    TYPES:

    *----------------------------------------------------------------------
    * data
    *----------------------------------------------------------------------
    DATA:
    *----------------------------------------------------------------------
    * screen
    *----------------------------------------------------------------------
    SELECTION-SCREEN FUNCTION KEY 1."下载模板.
    *SELECTION-SCREEN FUNCTION KEY 2."下载数据
    SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME TITLE TEXT-001.
    PARAMETERS: P_FILE LIKE RLGRAP-FILENAME MODIF ID M01. "导入摸版
    SELECTION-SCREEN END OF BLOCK BLK1.

    *----------------------------------------------------------------------
    * INITIALIZATION
    *----------------------------------------------------------------------
    INITIALIZATION.
      sscrfields-functxt_01 = text-t01."'@EZ@下载模板'.
    *  sscrfields-functxt_02 = text-t02."'@49@下载数据'.


    *----------------------------------------------------------------------
    * AT SELECTION-SCREEN ON VALUE-REQUEST
    *----------------------------------------------------------------------
    AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
      PERFORM SELECT_PATH."选择路径

    *----------------------------------------------------------------------
    * AT SELECTION SCREEN
    *----------------------------------------------------------------------
    AT SELECTION-SCREEN.
      IF sscrfields-ucomm EQ 'FC01'.
    * 模板下载按钮
        PERFORM DOWNLOAD_EXCEL_MODEL.
    * 库表数据下载按钮
    *  ELSEIF sy-ucomm EQ 'FC02'.
    *    PERFORM FRM_DOWNLOAD_DATA_EXCEL.

      ELSEIF sy-ucomm EQ 'ONLI'.
       IF P_FILE IS INITIAL.
         "请选择上传文件
         MESSAGE .
       ENDIF.
      ENDIF.

    ************************************************************************
    * START-OF-SELECTION
    ************************************************************************
    START-OF-SELECTION.
      PERFORM UPDATE_EXCEL_DATA.
      PERFORM CHECK_EXCEL_DATA.
      PERFORM ALV_DISPLAY.

    *&---------------------------------------------------------------------*
    *&      Form  SELECT_PATH
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM SELECT_PATH .
      DATA V_NZJMD LIKE RLGRAP-FILENAME.
      CALL FUNCTION 'WS_FILENAME_GET'
        EXPORTING
          MASK             = ',*.* ,*.*.'
          MODE             = '0'
          TITLE            = '请选择要上传的信息文件'
        IMPORTING
          FILENAME         = V_NZJMD
        EXCEPTIONS
          INV_WINSYS       = 1
          NO_BATCH         = 2
          SELECTION_CANCEL = 3
          SELECTION_ERROR  = 4
          OTHERS           = 5.
      IF SY-SUBRC <> 0.
    *    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    *            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.

    *  TRANSLATE  V_NZJMD  TO UPPER CASE .
    *  FIND REGEX '.XLS' IN V_NZJMD.
    *  IF SY-SUBRC <> 0.
    *    MESSAGE '请填写完整路径!' TYPE 'E'.
    *    RETURN.
    *  ENDIF.

      P_FILE = V_NZJMD.
    ENDFORM.                    " SELECT_PATH
    *&---------------------------------------------------------------------*
    *&      Form  DOWNLOAD_EXCEL_MODEL
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM DOWNLOAD_EXCEL_MODEL .
      DATA: L_OBJDATA LIKE WWWDATATAB,
            L_OBJID LIKE WWWDATATAB-OBJID VALUE 'ZTEST023',"SMW0中模板的ID
            L_MINE LIKE W3MINE,
            L_FIELDNAME TYPE STRING VALUE '导入模板.xls',
            L_PATH TYPE STRING,"路径
            L_FULLPATH TYPE STRING,"路径+文件名
            L_ACTION TYPE I,
            L_DESTINATION LIKE RLGRAP-FILENAME,
            L_RC LIKE SY-SUBRC.

      CALL METHOD cl_gui_frontend_services=>file_save_dialog
        EXPORTING
          window_title         = ''"弹出框的标题
          default_extension    = '*.XLS'
          default_file_name    = L_FIELDNAME
          file_filter          = 'Excel Files (*.xls)|*.xls'  "cl_gui_frontend_services=>filetype_excel
        CHANGING
          filename             = L_FIELDNAME
          path                 = L_PATH
          fullpath             = L_FULLPATH
          user_action          = L_ACTION
        EXCEPTIONS
          cntl_error           = 1
          error_no_gui         = 2
          not_supported_by_gui = 3
          OTHERS               = 4.
        IF SY-SUBRC = 0 AND L_ACTION = 0.

          "<----检查模板是否存在
          SELECT
            SINGLE RELID OBJID
            FROM WWWDATA
            INTO CORRESPONDING FIELDS OF L_OBJDATA
            WHERE SRTF2 = 0
            AND RELID = 'MI'
            AND OBJID = L_OBJID.
          IF SY-SUBRC <> 0.
            MESSAGE 'MOBANBUCUNZAI' TYPE 'E'.
          ENDIF.
          L_DESTINATION = L_FULLPATH.
          CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
            EXPORTING
              KEY               = L_OBJDATA
              DESTINATION       = L_DESTINATION
           IMPORTING
              RC                = L_RC
    *       CHANGING
    *         TEMP              =
                    .
          IF L_RC <> 0.
            MESSAGE '下载失败' TYPE 'I'.
          ENDIF.
        ELSE.
          LEAVE TO TRANSACTION SY-TCODE.
        ENDIF.


    ENDFORM.                    " DOWNLOAD_EXCEL_MODEL
    *&---------------------------------------------------------------------*
    *&      Form  UPDATE_EXCEL_DATA
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM UPDATE_EXCEL_DATA .
      REFRESH GT_UPLOAD_DATA.

      DATA:LT_RAW TYPE TRUXS_T_TEXT_DATA.
      CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
        EXPORTING
          I_FIELD_SEPERATOR    = 'X'
          I_LINE_HEADER        = 'X'
          I_TAB_RAW_DATA       = LT_RAW " WORK TABLE
          I_FILENAME           = P_FILES "路径
        TABLES
          I_TAB_CONVERTED_DATA = GT_UPLOAD_DATA[] "先对应导入Excel字段创建内表
        EXCEPTIONS
          CONVERSION_FAILED    = 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.                    " UPDATE_EXCEL_DATA
    *&---------------------------------------------------------------------*
    *&      Form  CHECK_EXCEL_DATA
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM CHECK_EXCEL_DATA .
    "检查导入数据、处理
    ENDFORM.                    " CHECK_EXCEL_DATA
    *&---------------------------------------------------------------------*
    *&      Form  ALV_DISPLAY
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM ALV_DISPLAY .
      PERFORM DEAL_DATA.
      PERFORM BUILD_FIELDCAT.
      PERFORM ALV_GRID_LVC.
    ENDFORM.                    " ALV_DISPLAY
    *&---------------------------------------------------------------------*
    *&      Form  DEAL_DATA
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM DEAL_DATA .

    ENDFORM.                    " DEAL_DATA
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_FIELDCAT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM BUILD_FIELDCAT .

    ENDFORM.                    " BUILD_FIELDCAT
    *&---------------------------------------------------------------------*
    *&      Form  ALV_GRID_LVC
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM ALV_GRID_LVC .
      
      
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
       EXPORTING
         I_CALLBACK_PROGRAM                = SY-REPID
         I_CALLBACK_PF_STATUS_SET          = 'GC_PF_STATUS'
         I_CALLBACK_USER_COMMAND           = 'USER_COMMAND'
         IS_LAYOUT_LVC                     = GW_LAYOUT
         IT_FIELDCAT_LVC                   = GT_FIELDCAT
    *     I_DEFAULT                         = 'X'
         I_SAVE                            = 'A'
        TABLES
          T_OUTTAB                          = GT_ALV[]
    *   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_GRID_LVC
    *&---------------------------------------------------------------------*
    *&      Form  GC_PF_STATUS
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM GC_PF_STATUS USING RT_EXTAB TYPE SLIS_T_EXTAB.
      "设置标题栏
      SET TITLEBAR ''.
      "GUI状态栏
    *  "修改gui按钮是否显示
    *  DATA: FCODE LIKE SY-UCOMM OCCURS 0 WITH HEADER LINE.
    *  APPEND 'INSR' TO FCODE.
    *  APPEND 'DELE' TO FCODE.
      SET PF-STATUS '' EXCLUDING FCODE.
    ENDFORM.                    "GC_PF_STATUS
    *&---------------------------------------------------------------------*
    *&      Form  USER_COMMAND
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *  -->  p1        text
    *  <--  p2        text
    *----------------------------------------------------------------------*
    FORM USER_COMMAND USING P_UCOMM LIKE SY-UCOMM
                            P_SELFIELD TYPE SLIS_SELFIELD.
      P_SELFIELD-REFRESH = 'X'."自动刷新
      CASE P_UCOMM.

        WHEN 'UPDATE'."

      ENDCASE.

    ENDFORM.                    "USER_COMMAND

  • 相关阅读:
    第二次结对作业(陆桂莺+崔亚明)
    第一次结对作业
    第二次作业:代码互改
    markdown详细
    第一次个人编程作业:我的分数我做主
    手动下载transformers的模型
    torch设置GPU
    Python import的搜索路径和不可以import的解决方法 (On Linux)
    Python中windows路径的3种写法
    一台计算机安装多个版本的torch和CUDA的教程
  • 原文地址:https://www.cnblogs.com/CtrlS/p/10488632.html
Copyright © 2020-2023  润新知