ABAP 导入Excel表示例程序

这篇具有很好参考价值的文章主要介绍了ABAP 导入Excel表示例程序。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

ABAP 导入excel示例程序

批量导入程序,需要使用到导入模板,首先需要创建程序,之后是需要创建excel导入模板,并且需要将excel导入模板上传到SAP系统里面,之后才可以下载下来进行导入。

创建程序

ABAP 批量导入excel,右击创建程序:
ABAP 导入Excel表示例程序,SAP,ABAP,其他,笔记

*&---------------------------------------------------------------------*
*& Report z_test_import_excel
*&---------------------------------------------------------------------*
*& TYPE:Report
*& Description:批量导入excel测试
*& Creator:刘洪裕
*& Created On:2024219*&---------------------------------------------------------------------*
*                           Change History
* ----------        -------------       -------------      -------------
*   Version          Date                Change By           Description
* ----------        -------------       -------------      -------------
*   N                2024.2.19           Hongyu.Liu          创建
*&---------------------------------------------------------------------*
Report z_test_import_excel NO STANDARD PAGE HEADING MESSAGE-ID zfimf01.

TABLES: sscrfields.

"定义相关变量
*TYPES: BEGIN OF ty_data,
*   werks TYPE zfi_mfwb-werks,  "工厂
*   zmj_matnr TYPE zfi_mfwb-zmj_matnr,  "模具料号
*   zcp_matnr TYPE zfi_mfwb-zcp_matnr,  "挤压产出成品料号
*   zdmczl TYPE zfi_mfwb-zdmczl,  "单套模具预计产出重量
*   zsfty TYPE zfi_mfwb-zsfty,  "是否通用
*   zwhrj TYPE zfi_mfwb-zwhrj,  "维护日期
*   zwhr TYPE zfi_mfwb-zwhr,  "维护人
*END OF ty_data.
*DATA gs_data TYPE  ty_data.  "工作区
*DATA gt_data TYPE TABLE OF ty_data.  "内表


*&---------------------------------------------------------------------*
*&  定义相关变量
*&---------------------------------------------------------------------*
TYPES: BEGIN OF ts_output,  "展示结构
         "INCLUDE STRUCTURE zfi_mfwb.
         werks     TYPE zfi_mfwb-werks,  "工厂
         zxlh      TYPE zfi_mfwb-zxlh,  "序列号
         zmj_matnr TYPE zfi_mfwb-zmj_matnr,  "模具料号
         zcp_matnr TYPE zfi_mfwb-zcp_matnr,  "挤压产出成品料号
         "zdmczl TYPE p DECIMALS 2 ,  "单套模具预计产出重量,两位小数
         zdmczl    TYPE zfi_mfwb-zdmczl,  "单套模具预计产出重量
         "zsfty     TYPE zfi_mfwb-zsfty,  "是否通用
         zsfty     TYPE abap_bool,  "是否通用
         zwhrj     TYPE zfi_mfwb-zwhrj,  "维护日期
         zwhr      TYPE zfi_mfwb-zwhr,  "维护人
         zsfbf     TYPE zfi_mfwb-zsfbf,  "是否报废
         type      TYPE bapiret2-type,
         message   TYPE char100,   "bapiret2-message,
         icon      TYPE text20,
         slbox     TYPE c,
         celltab   TYPE  lvc_t_styl, "设置单元格编辑状态
END OF ts_output.
TYPES: BEGIN OF ty_data,"导入数据结构
         werks     TYPE zfi_mfwb-werks,  "工厂
         zxlh      TYPE zfi_mfwb-zxlh,  "序列号
         zmj_matnr TYPE zfi_mfwb-zmj_matnr,  "模具料号
         zcp_matnr TYPE zfi_mfwb-zcp_matnr,  "挤压产出成品料号
         "zdmczl TYPE p DECIMALS 2 ,  "单套模具预计产出重量,两位小数
         zdmczl    TYPE zfi_mfwb-zdmczl,  "单套模具预计产出重量
         "zdmczl    LIKE vbap-ntgew,  "单套模具预计产出重量
         zsfty     TYPE zfi_mfwb-zsfty,  "是否通用
         zwhrj     TYPE zfi_mfwb-zwhrj,  "维护日期
         zwhr      TYPE zfi_mfwb-zwhr,  "维护人
       END OF ty_data.

*DATA: is_data TYPE ty_data.   "导入结构的工作区
*DATA: it_data TYPE TABLE OF ty_data.  "导入结构的工作表
CONSTANTS:cos_template TYPE w3objid VALUE 'ZFIMFWB01',
          cos_filename TYPE string VALUE '模具分摊维护表导入模板.xlsx'.

DATA: gs_layout TYPE lvc_s_layo,
      gt_fieldcat TYPE lvc_t_fcat,
      gs_fieldcat TYPE lvc_s_fcat,
      gs_variant TYPE disvariant. "屏幕变式

DATA: gt_output TYPE STANDARD TABLE OF ts_output WITH DEFAULT KEY,
      gs_output TYPE ts_output.
"DATA: gt_input TYPE TABLE OF zfi_mfwb.  "内表结构与zfi_mfwb一致
DATA: gt_input TYPE TABLE OF ty_data.  "导入模板数据
DATA: gt_output_old TYPE STANDARD TABLE OF ts_output WITH DEFAULT KEY."被修改前的数据

DATA: gt_bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE. "用来存储屏幕字段参数值,传递录屏参数
DATA: gt_messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE. "记录执行BDC过程中产生的消息

DATA: gv_mode TYPE c VALUE 'N'.

* 用于 修改要提示,删除要提示
DATA:gv_edit_flag  TYPE c, "X 表示修改过
     gv_save_flag  TYPE c, "X 表示已经保存。
     gv_check_flag TYPE c, "X 表示已经保存。
     gv_sref_flag  TYPE c, "X 表示刷新。
     gv_line       TYPE int4. "行数

CLASS lcl_event_receiver DEFINITION.
  PUBLIC SECTION.
    METHODS:
      handle_data_changed
        FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING er_data_changed,
      handle_data_changed_finished
        FOR EVENT data_changed_finished OF cl_gui_alv_grid
        IMPORTING
          e_modified
          et_good_cells.
  ENDCLASS.

CLASS lcl_event_receiver IMPLEMENTATION.
  METHOD handle_data_changed.
*    PERFORM FRM_DATA_CHANGED
*                  USING ER_DATA_CHANGED.
  ENDMETHOD.
  METHOD handle_data_changed_finished.
*    PERFORM FRM_DATA_CHANGED_FINISH
*                  USING E_MODIFIED ET_GOOD_CELLS.
    ENDMETHOD.
  ENDCLASS.
*&---------------------------------------------------------------------*
*&  选择屏幕
*&---------------------------------------------------------------------*
SELECTION-SCREEN FUNCTION KEY 1. "这是在屏幕的执行按钮后的位置添加一个功能按钮

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE TEXT-001.
  PARAMETERS p_file TYPE rlgrap-filename  MODIF ID m1.
SELECTION-SCREEN END OF BLOCK blk1.

*&---------------------------------------------------------------------*
*&  定义相关变量
*&---------------------------------------------------------------------*

FORM frm_init_data.
  DATA: ls_textfield TYPE smp_dyntxt. "菜单制作器:动态文本的程序接口
  ls_textfield-icon_id  = '@49@'.
  ls_textfield-icon_text  = '下载导入模板'.
  sscrfields-functxt_01 = ls_textfield.
  ENDFORM.


*&---------------------------------------------------------------------*
*&  屏幕事件
*&---------------------------------------------------------------------*

INITIALIZATION. "初始化
  "选择屏幕添加按钮
  PERFORM frm_init_data.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR  p_file. "按选择帮助<F4>键时触发该事件
  PERFORM frm_get_file_name CHANGING p_file.

AT SELECTION-SCREEN.  "在此事件里可以进行对用户输入进行完整性进行检查。
* 处理选择屏幕的按钮 下载模板
  PERFORM frm_screen_button_event.

" AT SELECTION SCREEN OUTPUT事件(也就是PBO) ,在这个事件里你可以通过修改系统默认screen内表修改屏幕的某些属性
AT SELECTION-SCREEN OUTPUT.
* 修改选择屏幕的元素的状态
  PERFORM modify_selction_screen_status.

START-OF-SELECTION. "仅在选择屏幕处理结束之后触发,即当用户点击“执行”按钮时
  PERFORM frm_check_authority.
  PERFORM batch_input.
  PERFORM alv_display USING 'X'.
*&---------------------------------------------------------------------*
*& Form frm_get_file_name:获取文件名称
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- P_FILE
*&---------------------------------------------------------------------*
FORM frm_get_file_name CHANGING cv_file.
  DATA: lt_filetable TYPE filetable,  "表类型
        ls_filetable TYPE file_table, "结构类型
        lv_rc TYPE i.
  FREE lt_filetable.
  CLEAR: lv_rc.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
*        window_title            =
        default_extension       = cl_gui_frontend_services=>filetype_excel
*        default_filename        =
        file_filter             = cl_gui_frontend_services=>filetype_excel
*        with_encoding           =
*        initial_directory       =
*        multiselection          =
    CHANGING
        file_table              = lt_filetable
        rc                      = lv_rc
*        user_action             =
*        file_encoding           =
    EXCEPTIONS
        file_open_dialog_failed = 1
        cntl_error              = 2
        error_no_gui            = 3
        not_supported_by_gui    = 4
        OTHERS                  = 5
            .
  READ TABLE lt_filetable INTO ls_filetable INDEX 1.
    IF sy-subrc <> 0.
*     Implement suitable error handling here
    ENDIF.
    IF sy-subrc EQ 0.
      cv_file = ls_filetable-filename.
    ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_screen_button_event:点击下载模板按钮
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_screen_button_event .
  CASE sscrfields-ucomm.
    WHEN 'FC01'.
      PERFORM frm_download_template USING cos_template cos_filename.
    WHEN OTHERS.
  ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form modify_selction_screen_status
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM modify_selction_screen_status .

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_check_authority
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_check_authority .

ENDFORM.
*&---------------------------------------------------------------------*
*& Form batch_input
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM batch_input .
*   EXCEL数据到内表
  PERFORM file_data_input.
*   转换内表
  PERFORM file_data_convert.
*   检查数据
  PERFORM file_data_check.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form alv_display:展示alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> P_
*&---------------------------------------------------------------------*
FORM alv_display  USING  u_edit TYPE abap_bool .
  PERFORM set_alv_layout.
  PERFORM build_fieldcat USING u_edit.
  PERFORM call_alv_func.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_download_template:下载模板
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> COS_TEMPLATE
*&      --> COS_FILENAME
*&---------------------------------------------------------------------*
FORM frm_download_template  USING    u_objid TYPE w3objid
                                     u_file_name TYPE string.
  DATA: ls_key TYPE wwwdatatab,
        lv_filename TYPE string,
        lv_path TYPE string,
        lv_fullpath TYPE string.

  "判断模板是否存在
  SELECT SINGLE * FROM wwwdata
    WHERE relid EQ 'MI'
    AND objid EQ @u_objid
    INTO CORRESPONDING FIELDS OF @ls_key.
  IF sy-subrc NE 0.
*   模板&1不存在
    MESSAGE s007 WITH u_objid.
    RETURN.
  ENDIF.
*   调用函数打开文件选择框

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
*      window_title              =
      default_extension         = cl_gui_frontend_services=>filetype_excel
      default_file_name         =  u_file_name
*      with_encoding             =
*      file_filter               =
*      initial_directory         =
*      prompt_on_overwrite       = 'X'
    CHANGING
      filename                  = lv_filename
      path                      = lv_path
      fullpath                  = lv_fullpath
*      user_action               =
*      file_encoding             =
    EXCEPTIONS
      cntl_error                = 1
      error_no_gui              = 2
      not_supported_by_gui      = 3
      invalid_default_file_name = 4
      OTHERS                    = 5
          .
  IF sy-subrc <> 0.
*   Implement suitable error handling here
  ENDIF.
  CHECK lv_fullpath NE ''.
*   下载SMW0模版
  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      key = ls_key
      destination = CONV localfile( lv_fullpath ).

ENDFORM.
*&---------------------------------------------------------------------*
*& Form file_data_input:EXCEL数据到内表
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM file_data_input .
* 数据EXCEL-》内表
  PERFORM frm_excel_to_table TABLES gt_input USING p_file 2.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form file_data_convert:转换内表
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM file_data_convert .
*   添加前导0
  LOOP AT gt_input ASSIGNING FIELD-SYMBOL(<lfs_input>).
    <lfs_input>-zmj_matnr = |{ <lfs_input>-zmj_matnr ALPHA = IN }|.
    <lfs_input>-zcp_matnr = |{ <lfs_input>-zcp_matnr ALPHA = IN }|.
    ENDLOOP.
  gt_output = CORRESPONDING #( gt_input ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form file_data_check:检查数据
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM file_data_check .
  IF gt_output IS NOT INITIAL.
    LOOP AT gt_output ASSIGNING FIELD-SYMBOL(<ls_output>).
      CLEAR:<ls_output>-type,
           <ls_output>-message.
      IF <ls_output>-werks IS INITIAL.
          <ls_output>-type = 'E'.
          <ls_output>-message = '工厂不能为空;'.
      ENDIF.
      IF <ls_output>-zmj_matnr IS INITIAL.
           <ls_output>-type = 'E'.
           <ls_output>-message = <ls_output>-message && '模具料号不能为空;'.
      ENDIF.
      IF <ls_output>-zcp_matnr IS INITIAL.
            <ls_output>-type = 'E'.
            <ls_output>-message = <ls_output>-message && '挤压产出成品料号不能为空;'.
      ENDIF.
      IF <ls_output>-type <> 'E'. "如果关键值不为空
          <ls_output>-type = 'N'. "将类型设置为N
      ENDIF.
      PERFORM set_light CHANGING <ls_output>. "设置提示灯
    ENDLOOP.

  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_excel_to_table
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> GT_INPUT
*     -->U_FILE       文件
*     -->U_START_ROW  文件内的数据从哪行开始取
*&---------------------------------------------------------------------*
FORM frm_excel_to_table  TABLES   et_input TYPE STANDARD TABLE
                         USING    u_file  TYPE rlgrap-filename
                                  u_start_row TYPE i.
  DATA: lt_intern TYPE STANDARD TABLE OF zalsmex_tabline.
  FREE: lt_intern.

*DATA FILENAME    TYPE RLGRAP-FILENAME.
*DATA I_BEGIN_COL TYPE I.
*DATA I_BEGIN_ROW TYPE I.
*DATA I_END_COL   TYPE I.
*DATA I_END_ROW   TYPE I.
*DATA INTERN      TYPE STANDARD TABLE OF ZALSMEX_TABLINE.

  CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename          = u_file
      i_begin_col       = 1
      i_begin_row       = 2
      i_end_col         = 99
      i_end_row         = 65536
    TABLES
      intern            = lt_intern
    EXCEPTIONS
      inconsistent_parameters   = 1
      upload_ole                = 2
      OTHERS                    = 3
            .
  IF sy-subrc NE 0.
  ENDIF.
  SORT lt_intern BY row col.
  LOOP AT lt_intern INTO DATA(ls_intern).
    "下面检查是否是数字
    AT NEW row. "如果字段row及row的左侧全部字段的数据,与上一行数据不一致,则执行代码。
      APPEND INITIAL LINE TO et_input ASSIGNING FIELD-SYMBOL(<ls_input>). "创建一个新行,并分配指针
    ENDAT.
    ASSIGN COMPONENT ls_intern-col OF STRUCTURE <ls_input> TO FIELD-SYMBOL(<lv_val>)."就是在每次循环中,都会分配一个结构的字段,是根据索引来决定的
    IF <lv_val> IS ASSIGNED.
      IF ls_intern-col EQ 5.  "如果是第5列(是否通用)
        <lv_val> = ls_intern-value.
      ENDIF.
      TRY.
        <lv_val> = ls_intern-value.
      CATCH cx_sy_conversion_no_number INTO DATA(lr_cx_sy_conversion_no_number).
*&1&2&3,&4
        MESSAGE e003 WITH ls_intern-row ls_intern-col ls_intern-value lr_cx_sy_conversion_no_number->get_text( ).
      CATCH cx_sy_conversion_overflow INTO DATA(lr_cx_sy_conversion_overflow).
*&1&2&3,&4
        MESSAGE e003 WITH ls_intern-row ls_intern-col ls_intern-value lr_cx_sy_conversion_overflow->get_text( ).
      CATCH cx_root INTO DATA(lr_cx_root).
*&1&2&3,&4
        MESSAGE e003 WITH ls_intern-row ls_intern-col ls_intern-value lr_cx_root->get_text( ).
      ENDTRY.
      UNASSIGN <lv_val>.  "取消分配
    ENDIF.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form set_alv_layout:设置alv的layout
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM set_alv_layout .
  CLEAR: gs_layout,gs_variant.
  gs_layout-zebra = 'X'.  "斑马线
  gs_layout-cwidth_opt = 'X'.  "自动列宽
  gs_layout-box_fname = 'SLBOX'.  "选择框字段名 "用户在ALV控件的行选中,对应的内存中【SLBOX】字段会被打上X
  gs_variant-report = sy-repid.
  gs_variant-handle = '01'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form build_fieldcat
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> U_EDIT
*&---------------------------------------------------------------------*
FORM build_fieldcat  USING    e_edit TYPE abap_bool.
  IF gt_fieldcat IS INITIAL.  "如果gt_fieldcat是初始状态
    APPEND VALUE  #( fieldname = 'ICON' ref_field  = 'ID' ref_table  = 'ICON'  scrtext_l = '状态' )  TO  gt_fieldcat[].
    APPEND VALUE  #( fieldname = 'MESSAGE'  ref_field = 'MESSAGE' ref_table = 'BAPIRET2' scrtext_l = '' ) TO gt_fieldcat[].
  ENDIF.
* DATA CT_FIELDCAT TYPE LVC_T_FCAT.
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
*     I_BUFFER_ACTIVE        = I_BUFFER_ACTIVE
      i_structure_name       = 'ZFI_MFWB'
*     I_CLIENT_NEVER_DISPLAY = 'X'
*     I_BYPASSING_BUFFER     = I_BYPASSING_BUFFER
*     I_INTERNAL_TABNAME     = I_INTERNAL_TABNAME
    CHANGING
      ct_fieldcat            = gt_fieldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
*     循环字段,修改显示的列名
  CLEAR gs_fieldcat.
  LOOP AT gt_fieldcat INTO gs_fieldcat.
    IF gs_fieldcat-fieldname = 'ZMJ_MATNR'.
      gs_fieldcat-coltext = '模具料号'.
    ELSEIF gs_fieldcat-fieldname = 'ZXLH'.
      gs_fieldcat-coltext = '序列号'.
    ELSEIF gs_fieldcat-fieldname = 'ZCP_MATNR'.
      gs_fieldcat-coltext = '挤出产出成品料号'.
    ELSEIF gs_fieldcat-fieldname = 'ZDMCZL'.
      gs_fieldcat-coltext = '单套模具预计产出重量'.
      gs_fieldcat-edit = 'X'."可编辑
*        gs_fieldcat-edit = ''."不可编辑
*        IF p_file IS INITIAL. "如果不是导入文件的操作,则设置重量为编辑
*          gs_fieldcat-edit = 'X'."可编辑
*        ENDIF.
    ELSEIF gs_fieldcat-fieldname = 'ZSFTY'.
      gs_fieldcat-coltext = '是否通用'.
      gs_fieldcat-edit = 'X'."可编辑
    ELSEIF gs_fieldcat-fieldname = 'ZWHRJ'.
      gs_fieldcat-coltext = '维护日期'.
    ELSEIF gs_fieldcat-fieldname = 'ZWHR'.
      gs_fieldcat-coltext = '维护人'.
    ELSEIF gs_fieldcat-fieldname = 'ZSFBF'.
      gs_fieldcat-coltext = '是否报废'.
      "gs_fieldcat-edit = 'X'."可编辑
    ENDIF.
    MODIFY gt_fieldcat FROM gs_fieldcat.
    CLEAR gs_fieldcat.
  ENDLOOP.
  IF sy-subrc <> 0.
*     Implement suitable error handling here
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form call_alv_func:调用alv显示函数
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM call_alv_func .
  DATA: lt_event TYPE slis_t_event,
        ls_event TYPE slis_alv_event.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
   EXPORTING
     i_callback_program                = sy-repid
     i_callback_pf_status_set          = 'SET_PF_STATUS'
     i_callback_user_command           = 'USER_COMMAND'
*     I_CALLBACK_TOP_OF_PAGE            = ' '
*     i_callback_html_top_of_page       = 'FRM_TOP_OF_PAGE'
     is_layout_lvc                     = gs_layout
     it_fieldcat_lvc                   = gt_fieldcat
     i_save                            = 'U'
     is_variant                        = gs_variant
     it_events                         = lt_event
*   IMPORTING
*     E_EXIT_CAUSED_BY_CALLER           = E_EXIT_CAUSED_BY_CALLER
*     ES_EXIT_CAUSED_BY_USER            = ES_EXIT_CAUSED_BY_USER
    TABLES
      t_outtab                          = gt_output
   EXCEPTIONS
     program_error                     = 1
     OTHERS                            = 2
            .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

ENDFORM.

*&---------------------------------------------------------------------*
*& FORM FRM_TOP_OF_PAGE
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& -->  P1        TEXT
*& <--  P2        TEXT
*&---------------------------------------------------------------------*
FORM frm_top_of_page USING p_cl_dd TYPE REF TO cl_dd_document.

ENDFORM.

*&---------------------------------------------------------------------*
*& FORM SET_PF_STATUS
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& -->  P1        TEXT
*& <--  P2        TEXT
*&---------------------------------------------------------------------*
FORM set_pf_status USING u_t_extab TYPE slis_t_extab.
  SET PF-STATUS 'STANDARD' EXCLUDING u_t_extab.
  SET TITLEBAR 'STANDARD'.
ENDFORM.

*&---------------------------------------------------------------------*
*& FORM USER_COMMAND
*&---------------------------------------------------------------------*
FORM user_command USING u_v_ucomm LIKE sy-ucomm
                        u_s_selfield TYPE slis_selfield.
  DATA: lr_grid TYPE REF TO cl_gui_alv_grid,
        lv_error TYPE char1,
        lv_confirm  TYPE c.
  DATA: lt_mod_data TYPE lvc_t_modi,
        ls_mod_data TYPE lvc_s_modi.
  DATA: lo_data TYPE REF TO cl_alv_changed_data_protocol.

*DATA IR_SALV_FULLSCREEN_ADAPTER TYPE REF TO CL_SALV_FULLSCREEN_ADAPTER.
*DATA ET_EXCLUDING               TYPE SLIS_T_EXTAB.
*DATA E_REPID                    TYPE SY-REPID.
*DATA E_CALLBACK_PROGRAM         TYPE SY-REPID.
*DATA E_CALLBACK_ROUTINE         TYPE SLIS_FORMNAME.
*DATA E_GRID                     TYPE REF TO CL_GUI_ALV_GRID.
*DATA ET_FIELDCAT_LVC            TYPE LVC_T_FCAT.
*DATA ER_TRACE                   TYPE REF TO CL_ALV_TRACE.
*DATA E_FLG_NO_HTML              TYPE C.
*DATA ES_LAYOUT_KKBLO            TYPE KKBLO_LAYOUT.
*DATA ES_SEL_HIDE                TYPE SLIS_SEL_HIDE_ALV.
*DATA ET_EVENT_EXIT              TYPE KKBLO_T_EVENT_EXIT.
*DATA ER_FORM_TOL                TYPE REF TO CL_SALV_FORM.
*DATA ER_FORM_EOL                TYPE REF TO CL_SALV_FORM.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
*   EXPORTING
*     IR_SALV_FULLSCREEN_ADAPTER       = IR_SALV_FULLSCREEN_ADAPTER
   IMPORTING
     e_GRID                          = lr_grid.
  lr_grid->check_changed_data( ).
  CASE u_v_ucomm.
    WHEN 'SAVE'.
      PERFORM file_data_check.
      PERFORM refresh_alv.
      PERFORM user_command_import_check CHANGING lv_error.
      CHECK lv_error  = ''.
      PERFORM check_user_decision USING '确认' '确定保存?' CHANGING lv_confirm.
      CHECK lv_confirm EQ abap_true.
      PERFORM user_command_import.
      gv_save_flag = 'X'.
    WHEN  'EXIT'.
      LEAVE TO SCREEN 0.
      CLEAR: gv_edit_flag,gv_save_flag.
    WHEN OTHERS.
  ENDCASE.
  PERFORM refresh_alv.
  ENDFORM.
*&---------------------------------------------------------------------*
*& Form refresh_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM refresh_alv .
  DATA: lr_grid TYPE REF TO cl_gui_alv_grid,
        ls_stable TYPE lvc_s_stbl.
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_grid.

  lr_grid->get_frontend_layout(
    IMPORTING
      es_layout = DATA(ls_layout)
  ).
  ls_layout-cwidth_opt = abap_true.
  lr_grid->set_frontend_layout( is_layout = ls_layout ).

  ls_stable-row = 'X'.
  ls_stable-col = 'X'.

  lr_grid->refresh_table_display(
    EXPORTING
      is_stable = ls_stable
    EXCEPTIONS
      finished = 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.
*&---------------------------------------------------------------------*
*& Form user_command_import_check
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- LV_ERROR
*&---------------------------------------------------------------------*
FORM user_command_import_check  CHANGING c_error TYPE char1.
  CLEAR c_error.
  IF lines( gt_output ) = 0.
    MESSAGE s000 WITH '导入数据为空' DISPLAY LIKE 'E'.
    c_error = 'X'.
    EXIT.
  ENDIF.
  IF NOT line_exists( gt_output[ slbox = 'X' ] ).
    MESSAGE s000 WITH '至少选中一行数据' DISPLAY LIKE 'E'.
    c_error = 'X'.
    EXIT.
  ENDIF.

  IF line_exists( gt_output[ type = 'E' slbox = 'X' ] ).
    MESSAGE s000 WITH '数据检查不通过,不可导入' DISPLAY LIKE 'E'.
    c_error = 'X'.
    EXIT.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form check_user_decision:确认用户决定
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> P_
*&      --> P_
*&      <-- LV_CONFIRM
*&---------------------------------------------------------------------*
FORM check_user_decision  USING    u_title
                                    u_question
                           CHANGING ch_confirm.
  DATA: lv_answer TYPE char1.
  CLEAR: ch_confirm.

  CLEAR: lv_answer.
  CALL FUNCTION 'POPUP_TO_CONFIRM'
    EXPORTING
      titlebar              = u_title
      text_question         = u_question
      default_button        = '2'
      display_cancel_button = ''
    IMPORTING
      answer                = lv_answer
    EXCEPTIONS
      text_not_found        = 1
      OTHERS                = 2.
  IF sy-subrc NE 0.
  ENDIF.

  IF lv_answer EQ '1'.
    ch_confirm = abap_true.
  ELSE.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form user_command_import
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM user_command_import .
  DATA: gs_input TYPE zfi_mfwb.  "工作区结构与zfi_mfwb一致
   LOOP AT gt_output ASSIGNING FIELD-SYMBOL(<lfs_output>) WHERE slbox = abap_true.
     MOVE-CORRESPONDING <lfs_output> TO gs_input.
     gs_input-mandt = sy-mandt.  "当前集团号
     gs_input-zwhrj = sy-datum. "当前日期
     gs_input-zwhr = sy-uname. "当前登录用户名
     INSERT zfi_mfwb FROM gs_input.
     IF sy-subrc <> 0.
       <lfs_output>-type = 'E'.
       IF sy-subrc = 4.
         <lfs_output>-message = '导入失败!数据库已存在此条数据'.
       ENDIF.
     ENDIF.
     IF sy-subrc = 0.
       <lfs_output>-type = 'S'.
       <lfs_output>-message = '导入成功!'.
     ENDIF.
     PERFORM set_light CHANGING <lfs_output>. "设置提示灯
   ENDLOOP.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form USER_COMMAND_EXIT_CHECK
*&---------------------------------------------------------------------*
*& 如果没保存就退出 弹框提示
*&---------------------------------------------------------------------*
*&      <-- LV_ERROR
*&---------------------------------------------------------------------*
FORM user_command_exit_check CHANGING c_error.
  DATA: lv_confirm TYPE c.

*  IF p_cre = 'X'.
*    IF NOT line_exists( gt_output[ type = 'E' ] ).
*      gv_edit_flag = 'X'.
*    ENDIF.
*  ENDIF.

  IF gv_edit_flag = 'X' AND gv_save_flag = '' .
    PERFORM check_user_decision USING '确认'
                                        '还没保存,确定退出?'
                                  CHANGING lv_confirm.
    IF lv_confirm EQ abap_true.
    ELSE.
      c_error = 'X'.
      EXIT.
    ENDIF.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form set_light:设置提示灯
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- <LS_OUTPUT>
*&---------------------------------------------------------------------*
FORM set_light  CHANGING ch_output TYPE ts_output.
  IF ch_output-type EQ 'E'.   "失败状态
    ch_output-icon = 'ICON_RED_LIGHT'.
  ELSEIF ch_output-type EQ 'S'.   "成功状态
    ch_output-icon = 'ICON_GREEN_LIGHT'.
  ELSEIF ch_output-type EQ 'N'.   "空状态
    ch_output-icon = 'ICON_YELLOW_LIGHT'.
  ENDIF.
  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name                  = ch_output-icon
    IMPORTING
      result                = ch_output-icon
    EXCEPTIONS
      icon_not_found        = 1
      outputfield_too_short = 2
      OTHERS                = 3.
  IF sy-subrc <> 0.
  ENDIF.
ENDFORM.

使用的结构

双击结构名称,结构的字段如下:
ABAP 导入Excel表示例程序,SAP,ABAP,其他,笔记

上传下载模板

ABAP 导入Excel表示例程序,SAP,ABAP,其他,笔记文章来源地址https://www.toymoban.com/news/detail-834032.html

到了这里,关于ABAP 导入Excel表示例程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【SAP ABAP】SAP Webservice & RESTful 接口服务发布教程

    关于 WebService 概念,这篇文章讲解的非常全面,可以移步阅读《SAP Web service》。 本想通过 RFC 来发布 ODATA 服务,奈何当前 SAP ECC 版本过低不支持,只好采用其他方式来发布服务,于是就尝试了下面这两种方法。 SE37,创建以下测试用 RFC 测试执行 RFC,得到的数据结果如下图:

    2024年02月02日
    浏览(40)
  • SAP ABAP技术文章合集_微信公众号:ABAP猿

    序号 文章标题 01 ABAP OOALV-基本显示 02 ABAP OOALV-排序、过滤 03 ABAP OOALV-合计、小计 04 ABAP OOALV-选择模式、触发事件 05 ABAP OOALV-隐藏列、修改列标题、添加图标列、调整列位置 06 ABAP OOALV-固定列、鼠标悬停文本 07 ABAP OOALV-颜色(列、行、单元格) 08 ABAP OOALV-样式(列、行、单元格

    2024年02月03日
    浏览(48)
  • SAP ABAP 用户状态锁定案例

    一、前言 项目需求是根据当天及前两天的离职员工信息(假设这是一个定时器任务每天下午5点执行程序,计算前两天的员工工号是为了将5点之后办理离职的员工工号找出来),将这些员工在用户表 USR02 中的锁定状态设置为 “64”,以保证离职员工无法继续使用系统账号。

    2024年02月15日
    浏览(49)
  • SAP ABAP 基础语法超详细

    1.表声明 当你声明了一个数据表的同时,系统也同时自动生成了一个和数据表同名的结构,结构的变量集等于数据表里面的字段。 2.定义变量 v1 是变量名。 (l) 是变量的长度。 t 是数据类型。 d 是小数位。 ‘xxx’ 是缺省值。 如:data num(10) type p decimals 3 value ‘1.12’. 数据

    2024年02月15日
    浏览(59)
  • SAP,ABAP:ALV变式复制

    应用场景:程序优化,需要新开一个程序优化,优化完成后原程序ALV变式无法带到新的程序, ALV变式是很好用的功能 REPORT zbsc. 事务代码:ZBSC-ALV变式复制 程序名称:ZBSC-ALV变式复制 程序目的: ZBSC-ALV变式复制 开发人员: *(修改日志)----------------------------------------------------

    2024年02月09日
    浏览(43)
  • sap abap,forms,smartforms 导出pdf

    4种方法: 1.安装pdf程序,Foxit Reader,先敲回车 自动带出,如下图: 直接打印就会弹出保存pdf文档路径,点保存。这种方式是最简单的,可 forms 和 smartforms 。 2. forms 和 smartforms 打印到spool 中,首先要设置个人参数文件  输出设备 ,默认输出设备LP01    。 T-CODE: SP02 或如下图

    2024年02月12日
    浏览(83)
  • SAP ABAP增强 BADI的增强全解析

            BADI的全称是Business Add-in,它的主要技术是基于ABAP的对象来实现增强。SAP中BADI的维护事务代码是SE18和SE19,SE18主要是创建及维护BADI对象,而SE19用于维护BADI的实例,即如何来实现BADI对象的功能。         SAP的BADI因系统版本的差别可能会有不同,R/3中的BADI被称为Cl

    2023年04月23日
    浏览(38)
  • SAP ABAP ChatGPT 初体验 我会失业吗?

    简介: ChatGPT 最近很火,听说我可能要失业!就在今天我历经千辛万苦,终于把 ChatGPT 注册好了。话不多说,我先问两个 ABAP 开发问题压压惊。 : SAP ABAP ChatGPT OpenAI 失业 下面是我在 ChatGPT 的聊天截图, 我对这个回答满意度 90% ,此时我内心还不慌,虽然内容很完整逻

    2023年04月15日
    浏览(41)
  • SAP ABAP调用Http/Https方式实例

    注意: 1.url必须带\\\"http://\\\"或者“https://”。 2. http_client-propertytype_redirect = http_client-co_disabled .这一行是设置是否允许重定向,是为“co_enabled”,否为“co_disabled”。 3. 可能出现的错误 404 Hostname Unknow,是因为服务器未配置该url的dns 4. 可能出现的错误 401 refused,可能是被防火墙

    2024年02月07日
    浏览(55)
  • 【SAP Abap】记录一次SAP长文本内容通过Web页面完整显示的应用

    业务在销售订单中,通过长文本描述,记录了一些生产备注信息,如生产标准、客户要求等等。要求开发一个报表,能够方便查看和导出长文本信息。 由于ALV列表长文本显示不友好,且长度有限制,仅能显示128位,故考虑双击展示长文本详细内容,最终实现效果如下: ALV列

    2024年02月15日
    浏览(51)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包