SAP批导程序Demo

2023-10-13 17:23:38 浏览数 (1)

下面分享一下具体的把SMW0上载好的文件模板下载到本地的代码,写在选择屏幕按钮的事件中。

代码语言:javascript复制
  CASE sscrfields-ucomm.

    WHEN 'FC01'.
      CLEAR:g_file,l_temp,l_str.
      "获取文件路径,并填充文件名称
      CONCATENATE '预留导入模板-' sy-datum INTO l_str.
      l_temp = '1'.
      PERFORM frm_get_filepath USING l_str CHANGING g_file.
      "下载模板
      CHECK sy-subrc = 0 AND NOT g_file IS INITIAL.
      PERFORM frm_download_excel_fromserver USING g_file l_temp."

  ENDCASE.
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_G_FILE  text
*----------------------------------------------------------------------*
FORM frm_get_filepath USING p_l_str CHANGING p_file.

  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = p_l_str "
      def_path         = p_file
      mask             = ',*.xls,*.xls,*.xlsx,*.xlsx.'
      mode             = 'S'
      title            = '保存路径'
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

ENDFORM. "FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*&      Form  FRM_DOWNLOAD_EXCEL_FROMSERVER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_L_TEMP  text
*      -->P_P_FILE  text
*      <--P_L_ERROR  text
*----------------------------------------------------------------------*
FORM frm_download_excel_fromserver USING p_file LIKE sapb-sappfad
                                           p_temp.

  DATA:l_temp         TYPE wwwdatatab-objid,
       lo_objdata     LIKE wwwdatatab,
       lo_mime        LIKE w3mime,
       ls_destination LIKE rlgrap-filename,
       ls_objnam      TYPE string,
       li_rc          LIKE sy-subrc,
       ls_errtxt      TYPE string,
       l_error        TYPE char255.

  CLEAR:l_temp,l_error,lo_objdata,ls_objnam.

  IF p_temp = '1'.
    l_temp =  'ZMMR095'.
  ENDIF.

  CONCATENATE l_temp '.XLS' INTO ls_objnam.

  SELECT SINGLE relid objid FROM wwwdata
    INTO CORRESPONDING FIELDS OF lo_objdata
    WHERE srtf2    = 0
      AND relid    = 'MI'
      AND objid    = l_temp.

  IF sy-subrc NE 0 OR lo_objdata-objid EQ space.
    CONCATENATE '模板文件:' ls_objnam '不存在,请用TCODE:SMW0进行加载' INTO l_error.
    MESSAGE e000(zre) WITH l_error.
  ENDIF.

  ls_destination   = p_file.
  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      key         = lo_objdata
      destination = ls_destination
    IMPORTING
      rc          = li_rc.

  IF li_rc NE 0.
    CONCATENATE '模板文件:' ls_objnam '下载失败' INTO l_error.
    MESSAGE e000(zre) WITH l_error.
  ENDIF.

ENDFORM. " FRM_DOWNLOAD_EXCEL_FROMSERVER

三、把EXCEL读到SAP中

通过函数ALSM_EXCEL_TO_INTERNAL_TABLE把EXCEL的文件导入到内表中,内表lt_excel的排列是横向的,通过动态语法转换为同EXCEL中一致的格式,并进行数据检查或者处理。

代码语言:javascript复制
START-OF-SELECTION.

  PERFORM frm_upload_data. "导入数据

*&---------------------------------------------------------------------*
*& Form frm_upload_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_upload_data .

  DATA: lt_excel TYPE TABLE OF alsmex_tabline WITH HEADER LINE.
  FIELD-SYMBOLS: <fs_val>.

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_file
      i_begin_col             = '1'     "从第一列开始
      i_begin_row             = '3'     "从第二行开始
      i_end_col               = '50'
      i_end_row               = '50000'
    TABLES
      intern                  = lt_excel
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

  LOOP AT lt_excel.
    CONDENSE lt_excel-value.
    ASSIGN COMPONENT lt_excel-col OF STRUCTURE gs_excel TO <fs_val>.
    <fs_val> = lt_excel-value.
    AT END OF row.
      gs_excel-deng = c_yellow.
      APPEND gs_excel TO gt_excel.
      CLEAR gs_excel.
    ENDAT.
  ENDLOOP.



ENDFORM.

四、处理并展示EXCEL上载的数据

通过ALV把内表gt_excel的数据进行展示。

五、执行导入并返回导入结果

在ALV上增加按钮并进行导入处理,导入结果刷新到ALV屏幕上

六、批导DEMO代码

*–>,包含前面这个符号的位置需要进行代码调整

代码语言:javascript复制
*----------------------------------------------------------------------*
* Type-pools/定义类型池
*----------------------------------------------------------------------*
TYPE-POOLS:slis.


*----------------------------------------------------------------------*
* Tables/声明数据库表
*----------------------------------------------------------------------*
TABLES :sscrfields.

DATA:ok_code  LIKE sy-ucomm.
DATA:functxt  TYPE smp_dyntxt.
DATA:g_file   TYPE sapb-sappfad.

*-->ALV Define
DATA: gs_fieldcat TYPE lvc_s_fcat,
      gt_fieldcat TYPE lvc_t_fcat.
DATA: gs_layout   TYPE lvc_s_layo .
DATA: gs_variant  TYPE disvariant.

CONSTANTS:c_green  LIKE dv70a-statusicon VALUE '@08@',
          c_yellow LIKE dv70a-statusicon VALUE '@09@',
          c_red    LIKE dv70a-statusicon VALUE '@0A@'.

"选择屏幕定义
*-->(需填充代码)
"选择屏幕定义
SELECTION-SCREEN BEGIN OF BLOCK blc_001 WITH FRAME TITLE TEXT-001.
  PARAMETERS : p_file   LIKE rlgrap-filename .
SELECTION-SCREEN END OF BLOCK blc_001.
SELECTION-SCREEN FUNCTION KEY 1.

"内表定义
*-->(需填充代码)

DATA : BEGIN OF gs_excel,

         deng    TYPE char4,     "状态
         messgae TYPE char40, "消息
       END OF gs_excel.
DATA : gt_excel LIKE TABLE OF gs_excel.

INITIALIZATION.

  PERFORM frm_sub_button.  "模板按钮

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.         "data declaration must be front of this statement

  PERFORM select_file.

AT SELECTION-SCREEN.

  PERFORM frm_download_mod."选择屏幕按钮

START-OF-SELECTION.

  PERFORM frm_upload_data. "导入数据

  PERFORM frm_alv_output."ALV输出
*&---------------------------------------------------------------------*
*& Form frm_sub_button
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_sub_button .

  CLEAR functxt.
  functxt-icon_id   = icon_export."icon_view_form
  functxt-icon_text = '下载批导模板'.
  sscrfields-functxt_01 = functxt.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form select_file
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM select_file .
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_path         = ''
      mask             = ',*.*,*.*.'(101)
      title            = 'Select File'(100)
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_download_mod
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_download_mod .

  DATA:l_temp TYPE c,
       l_str  TYPE string.


  CASE sscrfields-ucomm.

    WHEN 'FC01'.
      CLEAR:g_file,l_temp,l_str.
      "获取文件路径
*-->需要修改为自己的描述
      CONCATENATE '预留导入模板-' sy-datum INTO l_str.
      l_temp = '1'.
      PERFORM frm_get_filepath USING l_str CHANGING g_file.
      "下载模板
      CHECK sy-subrc = 0 AND NOT g_file IS INITIAL.
      PERFORM frm_download_excel_fromserver USING g_file l_temp.
  ENDCASE.


ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_G_FILE  text
*----------------------------------------------------------------------*
FORM frm_get_filepath USING p_l_str CHANGING p_file.

  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = p_l_str "
      def_path         = p_file
      mask             = ',*.xls,*.xls,*.xlsx,*.xlsx.'
      mode             = 'S'
      title            = '保存路径'
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

ENDFORM. "FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*&      Form  FRM_DOWNLOAD_EXCEL_FROMSERVER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_L_TEMP  text
*      -->P_P_FILE  text
*      <--P_L_ERROR  text
*----------------------------------------------------------------------*
FORM frm_download_excel_fromserver USING p_file LIKE sapb-sappfad
                                           p_temp.

  DATA:l_temp         TYPE wwwdatatab-objid,
       lo_objdata     LIKE wwwdatatab,
       lo_mime        LIKE w3mime,
       ls_destination LIKE rlgrap-filename,
       ls_objnam      TYPE string,
       li_rc          LIKE sy-subrc,
       ls_errtxt      TYPE string,
       l_error        TYPE char255.

  CLEAR:l_temp,l_error,lo_objdata,ls_objnam.

  IF p_temp = '1'.
  *-->需要修改为自己的SMW0模板名称
    l_temp =  'ZMMR095'.
  ENDIF.

  CONCATENATE l_temp '.XLS' INTO ls_objnam.

  SELECT SINGLE relid objid FROM wwwdata
    INTO CORRESPONDING FIELDS OF lo_objdata
    WHERE srtf2    = 0
      AND relid    = 'MI'
      AND objid    = l_temp.

  IF sy-subrc NE 0 OR lo_objdata-objid EQ space.
    CONCATENATE '模板文件:' ls_objnam '不存在,请用TCODE:SMW0进行加载' INTO l_error.
    MESSAGE e000(zre) WITH l_error.
  ENDIF.

  ls_destination   = p_file.
  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      key         = lo_objdata
      destination = ls_destination
    IMPORTING
      rc          = li_rc.

  IF li_rc NE 0.
    CONCATENATE '模板文件:' ls_objnam '下载失败' INTO l_error.
    MESSAGE e000(zre) WITH l_error.
  ENDIF.

ENDFORM. " FRM_DOWNLOAD_EXCEL_FROMSERVER
*&---------------------------------------------------------------------*
*& Form frm_upload_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_upload_data .

  DATA: lt_excel TYPE TABLE OF alsmex_tabline WITH HEADER LINE.
  FIELD-SYMBOLS: <fs_val>.

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_file
      i_begin_col             = '1'     "从第一列开始
      i_begin_row             = '2'     "从第二行开始
      i_end_col               = '50'
      i_end_row               = '50000'
    TABLES
      intern                  = lt_excel
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

  LOOP AT lt_excel.
    CONDENSE lt_excel-value.
    ASSIGN COMPONENT lt_excel-col OF STRUCTURE gs_excel TO <fs_val>.
    <fs_val> = lt_excel-value.
    AT END OF row.
      gs_excel-deng = c_yellow.
      APPEND gs_excel TO gt_excel.
      CLEAR gs_excel.
    ENDAT.
  ENDLOOP.



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

  PERFORM fill_fieldcat.

  PERFORM fill_layout.

  PERFORM frm_alv_display.

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

  DEFINE add_fieldcat.

    CLEAR gs_fieldcat.
    gs_fieldcat-fieldname    =  &1.
    gs_fieldcat-reptext      =  &2.
    gs_fieldcat-edit         =  &3.
    gs_fieldcat-col_pos      =  &4.
    gs_fieldcat-just         =  &5.
    gs_fieldcat-outputlen    =  &6.
    gs_fieldcat-fix_column   =  &7.

    APPEND gs_fieldcat TO gt_fieldcat.

  END-OF-DEFINITION.

  CLEAR gt_fieldcat.
  REFRESH gt_fieldcat.


  add_fieldcat     'DENG'     '状态'             ''   ''  ''  ''  ''.
  add_fieldcat     'MESSAGE'  '消息'             ''   ''  ''  '10'  ''.
*-->需要添加自己的展示字段


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

* 定义列自动宽度优化
*  gs_layout-cwidth_opt  = 'X'.
  gs_layout-sel_mode    = 'A'.
*  gs_layout-no_rowmark = 'X'.
* 定义条纹显示
  gs_layout-zebra       = 'X'.

  gs_variant-report     = sy-repid.

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

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'STATUS'   "GUI状态
      i_callback_user_command  = 'USER_COMMAND'  "按钮事件
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fieldcat
    TABLES
      t_outtab                 = gt_excel
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.

  IF sy-subrc <> 0.
* Implement suitable error handling here
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE 'E'.
  ENDIF.

ENDFORM.
FORM status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS '100'.
ENDFORM.
FORM user_command USING u_ucomm LIKE sy-ucomm
                         rs_selfield TYPE slis_selfield.

  DATA: lo_grid TYPE REF TO cl_gui_alv_grid,
        lv_code LIKE sy-ucomm.
  DATA: ls_stable TYPE lvc_s_stbl.

  " 刷新alv屏幕数据
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lo_grid.

  CALL METHOD lo_grid->check_changed_data.

  CASE u_ucomm.
    WHEN 'CREATE'.
*-->需要写自己的批导逻辑

  ENDCASE.

  rs_selfield-refresh = 'X' .


ENDFORM.

七、完整代码展示(预留批导为例)

以下是一个玩整的预留批导程序的样例,默认了类型,定义好GUI状态就可以使用了。

代码语言:javascript复制
*&---------------------------------------------------------------------*
*& Report ZMMR095
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zmmr095.
*----------------------------------------------------------------------*
* Type-pools/定义类型池
*----------------------------------------------------------------------*
TYPE-POOLS:slis.


*----------------------------------------------------------------------*
* Tables/声明数据库表
*----------------------------------------------------------------------*
TABLES :sscrfields.

DATA:ok_code  LIKE sy-ucomm.
DATA:functxt  TYPE smp_dyntxt.
DATA:g_file   TYPE sapb-sappfad.

*-->ALV Define
DATA: gs_fieldcat TYPE lvc_s_fcat,
      gt_fieldcat TYPE lvc_t_fcat.
DATA: gs_layout   TYPE lvc_s_layo .
DATA: gs_variant  TYPE disvariant.

CONSTANTS:c_green  LIKE dv70a-statusicon VALUE '@08@',
          c_yellow LIKE dv70a-statusicon VALUE '@09@',
          c_red    LIKE dv70a-statusicon VALUE '@0A@'.

"选择屏幕定义
SELECTION-SCREEN BEGIN OF BLOCK blc_001 WITH FRAME TITLE TEXT-001.
  PARAMETERS : p_file   LIKE rlgrap-filename .
  PARAMETERS : p_bwart  LIKE resb-bwart DEFAULT 'Z41' .
SELECTION-SCREEN END OF BLOCK blc_001.
SELECTION-SCREEN FUNCTION KEY 1.

"内表定义
DATA :BEGIN OF gs_excel,
        werks   TYPE resb-werks, "工厂
        lgort   LIKE resb-lgort,
        lifnr   TYPE resb-lifnr, "供应商
        matnr   LIKE resb-matnr,
        bdmng   TYPE resb-bdmng,
        charg   LIKE resb-charg,

        bwart   TYPE resb-bwart, "移动类型
        umlgo   LIKE resb-umlgo,
        deng    TYPE char4,
        message TYPE char20,
      END OF gs_excel.
DATA : gt_excel LIKE TABLE OF gs_excel.


INITIALIZATION.

  PERFORM frm_scr_out.     "动态屏幕

  PERFORM frm_sub_button.  "模板按钮

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.         "data declaration must be front of this statement

  PERFORM select_file.

AT SELECTION-SCREEN.

  PERFORM frm_download_mod."选择屏幕按钮

START-OF-SELECTION.

  PERFORM frm_upload_data. "导入数据

  PERFORM frm_alv_output."ALV输出
*&---------------------------------------------------------------------*
*& Form frm_sub_button
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_sub_button .

  CLEAR functxt.
  functxt-icon_id   = icon_export."icon_view_form
  functxt-icon_text = '下载批导模板'.
  sscrfields-functxt_01 = functxt.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form select_file
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM select_file .
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_path         = ''
      mask             = ',*.*,*.*.'(101)
      title            = 'Select File'(100)
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_download_mod
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_download_mod .

  DATA:l_temp TYPE c,
       l_str  TYPE string.


  CASE sscrfields-ucomm.

    WHEN 'FC01'.
      CLEAR:g_file,l_temp,l_str.
      "获取文件路径
      CONCATENATE '预留导入模板-' sy-datum INTO l_str.
      l_temp = '1'.
      PERFORM frm_get_filepath USING l_str CHANGING g_file.
      "下载模板
      CHECK sy-subrc = 0 AND NOT g_file IS INITIAL.
      PERFORM frm_download_excel_fromserver USING g_file l_temp.

  ENDCASE.


ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_G_FILE  text
*----------------------------------------------------------------------*
FORM frm_get_filepath USING p_l_str CHANGING p_file.

  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = p_l_str "
      def_path         = p_file
      mask             = ',*.xls,*.xls,*.xlsx,*.xlsx.'
      mode             = 'S'
      title            = '保存路径'
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

ENDFORM. "FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*&      Form  FRM_DOWNLOAD_EXCEL_FROMSERVER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_L_TEMP  text
*      -->P_P_FILE  text
*      <--P_L_ERROR  text
*----------------------------------------------------------------------*
FORM frm_download_excel_fromserver USING p_file LIKE sapb-sappfad
                                           p_temp.

  DATA:l_temp         TYPE wwwdatatab-objid,
       lo_objdata     LIKE wwwdatatab,
       lo_mime        LIKE w3mime,
       ls_destination LIKE rlgrap-filename,
       ls_objnam      TYPE string,
       li_rc          LIKE sy-subrc,
       ls_errtxt      TYPE string,
       l_error        TYPE char255.

  CLEAR:l_temp,l_error,lo_objdata,ls_objnam.

  IF p_temp = '1'.
    l_temp =  'ZMMR095'.
  ENDIF.

  CONCATENATE l_temp '.XLS' INTO ls_objnam.

  SELECT SINGLE relid objid FROM wwwdata
    INTO CORRESPONDING FIELDS OF lo_objdata
    WHERE srtf2    = 0
      AND relid    = 'MI'
      AND objid    = l_temp.

  IF sy-subrc NE 0 OR lo_objdata-objid EQ space.
    CONCATENATE '模板文件:' ls_objnam '不存在,请用TCODE:SMW0进行加载' INTO l_error.
    MESSAGE e000(zre) WITH l_error.
  ENDIF.

  ls_destination   = p_file.
  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      key         = lo_objdata
      destination = ls_destination
    IMPORTING
      rc          = li_rc.

  IF li_rc NE 0.
    CONCATENATE '模板文件:' ls_objnam '下载失败' INTO l_error.
    MESSAGE e000(zre) WITH l_error.
  ENDIF.

ENDFORM. " FRM_DOWNLOAD_EXCEL_FROMSERVER
*&---------------------------------------------------------------------*
*& Form frm_upload_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_upload_data .

  DATA: lt_excel TYPE TABLE OF alsmex_tabline WITH HEADER LINE.
  FIELD-SYMBOLS: <fs_val>.

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_file
      i_begin_col             = '1'     "从第一列开始
      i_begin_row             = '3'     "从第二行开始
      i_end_col               = '50'
      i_end_row               = '50000'
    TABLES
      intern                  = lt_excel
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

  LOOP AT lt_excel.
    CONDENSE lt_excel-value.
    ASSIGN COMPONENT lt_excel-col OF STRUCTURE gs_excel TO <fs_val>.
    <fs_val> = lt_excel-value.
    AT END OF row.
      gs_excel-deng = c_yellow.
      APPEND gs_excel TO gt_excel.
      CLEAR gs_excel.
    ENDAT.
  ENDLOOP.



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

  PERFORM fill_fieldcat.

  PERFORM fill_layout.

  PERFORM frm_alv_display.

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

  DEFINE add_fieldcat.

    CLEAR gs_fieldcat.
    gs_fieldcat-fieldname    =  &1.
    gs_fieldcat-reptext      =  &2.
    gs_fieldcat-edit         =  &3.
    gs_fieldcat-col_pos      =  &4.
    gs_fieldcat-just         =  &5.
    gs_fieldcat-outputlen    =  &6.
    gs_fieldcat-fix_column   =  &7.

    APPEND gs_fieldcat TO gt_fieldcat.

  END-OF-DEFINITION.

  CLEAR gt_fieldcat.
  REFRESH gt_fieldcat.


  add_fieldcat     'DENG'     '状态'             ''   ''  ''  ''  ''.
  add_fieldcat     'MESSAGE'  '消息'             ''   ''  ''  '10'  ''.
  add_fieldcat     'WERKS'    '工厂'             ''   ''  ''  ''  ''.
  add_fieldcat     'LIFNR'    '供应商'           ''   ''  ''  ''  ''.
  add_fieldcat     'MATNR'    '物料'             ''   ''  ''  ''  ''.
  add_fieldcat     'BDMNG'    '数量'             ''   ''  ''  '5'  ''.
*  add_fieldcat     'LGORT'    '库存地点'         ''   ''  ''  ''  ''.
*  add_fieldcat     'CHARG'    '批次'             ''   ''  ''  ''  ''.

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

* 定义列自动宽度优化
*  gs_layout-cwidth_opt  = 'X'.
  gs_layout-sel_mode    = 'A'.
*  gs_layout-no_rowmark = 'X'.
* 定义条纹显示
  gs_layout-zebra       = 'X'.

  gs_variant-report     = sy-repid.

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

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'STATUS'   "GUI状态
      i_callback_user_command  = 'USER_COMMAND'  "按钮事件
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fieldcat
    TABLES
      t_outtab                 = gt_excel
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.

  IF sy-subrc <> 0.
* Implement suitable error handling here
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE 'E'.
  ENDIF.

ENDFORM.
FORM status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS '100'.
ENDFORM.
FORM user_command USING u_ucomm LIKE sy-ucomm
                         rs_selfield TYPE slis_selfield.

  DATA: lo_grid TYPE REF TO cl_gui_alv_grid,
        lv_code LIKE sy-ucomm.
  DATA: ls_stable TYPE lvc_s_stbl.

  " 刷新alv屏幕数据
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lo_grid.

  CALL METHOD lo_grid->check_changed_data.

  CASE u_ucomm.
    WHEN 'CREATE'.
      PERFORM frm_check.
      PERFORM frm_create.
  ENDCASE.

  rs_selfield-refresh = 'X' .


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

  DATA : gs_header TYPE bapirkpfc,
         gt_item   TYPE TABLE OF bapiresbc,
         gs_item   TYPE bapiresbc,
         gt_return TYPE TABLE OF bapireturn,
         gs_return TYPE bapireturn.
  DATA : l_rsnum TYPE bapirkpfc-res_no.

  DATA : lt_alv     LIKE TABLE OF gs_excel.
  DATA : ls_alv     LIKE gs_excel.
  DATA : lv_message TYPE char40.

  APPEND LINES OF gt_excel TO lt_alv.
  DELETE ADJACENT DUPLICATES FROM lt_alv COMPARING lifnr werks.


  LOOP AT lt_alv INTO ls_alv .

    LOOP AT gt_excel INTO gs_excel WHERE lifnr = ls_alv-lifnr AND werks = ls_alv-werks.

      CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
        EXPORTING
          input  = gs_excel-matnr
        IMPORTING
          output = gs_excel-matnr.

      SELECT SINGLE meins FROM mara INTO gs_item-unit WHERE matnr = gs_excel-matnr.


      gs_header-plant      = gs_excel-werks.
*      gs_header-move_stloc = '1001'.
      gs_header-move_plant = gs_excel-werks.
      gs_header-move_stloc = gs_excel-lgort.
      gs_header-move_type  = p_bwart.
      gs_header-res_date   = sy-datum.

      gs_item-material  = gs_excel-matnr.
      gs_item-plant     = gs_excel-werks.
      gs_item-store_loc = gs_excel-lgort.
      gs_item-quantity  = gs_excel-bdmng.

      APPEND gs_item TO gt_item.
      CLEAR gs_item.
    ENDLOOP.

    CALL FUNCTION 'BAPI_RESERVATION_CREATE'
      EXPORTING
        reservation_header = gs_header
*       NO_COMMIT          =
*       MOVEMENT_AUTO      =
      IMPORTING
        reservation        = l_rsnum
      TABLES
        reservation_items  = gt_item
        return             = gt_return.

    LOOP AT gt_return INTO gs_return.
      lv_message = gs_return-message.
    ENDLOOP.
    IF sy-subrc <> 0.
      lv_message = l_rsnum.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
      WAIT UP TO '0.2' SECONDS.
    ENDIF.

    LOOP AT gt_excel ASSIGNING FIELD-SYMBOL(<fs_excel>) WHERE lifnr = ls_alv-lifnr AND werks = ls_alv-werks.
      IF l_rsnum IS INITIAL .
        <fs_excel>-deng = c_red .
      ELSE.
        <fs_excel>-deng = c_green.
        AT END OF lifnr.
          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
            EXPORTING
              input  = ls_alv-lifnr
            IMPORTING
              output = ls_alv-lifnr.

          UPDATE rkpf SET zlifnr = ls_alv-lifnr
                          umlgo = '1001'
                     WHERE rsnum = l_rsnum.
          COMMIT WORK.
        ENDAT.
      ENDIF.
      <fs_excel>-message = lv_message.
    ENDLOOP.
    CLEAR : l_rsnum,lv_message,gs_header,gt_item,gt_return.
  ENDLOOP.


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

  LOOP AT gt_excel INTO gs_excel WHERE deng <> c_yellow.
    MESSAGE '不能重复创建!' TYPE 'E'.
  ENDLOOP.

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

  LOOP AT SCREEN.
    IF screen-name = 'P_BWART'.
      screen-input = '0'.
    ENDIF.
    MODIFY SCREEN.
  ENDLOOP.

ENDFORM.

0 人点赞