SAP_ABAP_OLE_EXCEL批导案例

SAP ABAP顾问能力模型梳理_企业数字化建设者的博客-CSDN博客SAP Abap顾问能力模型https://blog.csdn.net/java_zhong1990/article/details/132469977

一、OLE_EXCEL批导

1.1 下载按钮
1.2  选择EXCEL上传,解析EXCLE数据, Call屏幕。

1.3 实现效果

SAP_ABAP_OLE_EXCEL批导案例_第1张图片
1.4 代码样例

*&---------------------------------------------------------------------*
*& Report ZMMRP010
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zmmrp007.

*******************************************************
*程序名: zmmrp007
*程序描述: 采购订单审批
*----------------------------------------------------------------------
*创建日期     开发顾问     业务顾问
*2019.09.17. terryzhong 
*===============================================
*修改请求号 修改日期 修改人 修改描述
*xxxxxxxx xxxx.xx.xx xxxxxxxx xxxxxxxx
*******************************************************
INCLUDE zmmrp007_top.

INCLUDE zmmrp007_sel.

INCLUDE zmmrp007_frm.

INCLUDE zmmrp007_9000.

INCLUDE zmmrp007_status_9000pbo.

INCLUDE zmmrp007_user_command_9000i01.

INITIALIZATION."第一次打开屏幕或者点击执行代码已经执行完成重新显示屏幕的时候会运行
*(1).只能用于报表程序
*(2).在选择屏幕出现之前执行.
*(3).通常的用法是在这里给选择屏幕中的字段赋值。
 sscrfields-functxt_02 = '下载模板'.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR pa_file.
 PERFORM get_excel.


AT SELECTION-SCREEN."当在选择屏幕上有做操作的时候都会进入到这个事件

 IF sscrfields-ucomm = 'FC02'.
 PERFORM download_template.
 ENDIF.

START-OF-SELECTION."当触发执行的时候屏幕
 IF p_rd2 EQ 'X'."批量修改
 "执行对EXCEL数据的解析
 PERFORM get_data.
 CLEAR gs_cdata.
 CALL SCREEN '9000'.
 ENDIF.


END-OF-SELECTION.
 IF p_rd1 EQ 'X'.
 CALL TRANSACTION 'ZMMRP007A'."单个修改
 ENDIF.



 *&---------------------------------------------------------------------*
*& 包含 ZMMRP007_TOP
*&---------------------------------------------------------------------*

TABLES: sscrfields.

TYPES: BEGIN OF gy_cdata,
 check(2) TYPE c,
 ebeln TYPE ekko-ebeln, "采购订单号
 ebelp TYPE ekpo-ebelp, "采购订单项次
 zindex TYPE etens, "确认序号
 ebtyp TYPE ekes-ebtyp, "确认类型
 eindt TYPE ekes-eindt, "交货日期
 menge TYPE ekpo-menge, "数量

* peinh TYPE ztmm_quotesi-peinh, "价格单位
 bstme TYPE bstme,
* bstme(10) type c,
 suppliercno TYPE xblnr_long, "参考
 exestatus(2) TYPE c,
 exedesc(255) TYPE c,

 END OF gy_cdata.
DATA gs_cdata TYPE gy_cdata.
DATA gt_cdata TYPE TABLE OF gy_cdata.
DATA gv_ok_code LIKE sy-ucomm.

DATA gt_cdata_copy TYPE TABLE OF gy_cdata.
DATA gs_cdata_copy TYPE gy_cdata.

*DATA gt_cdata_update TYPE TABLE OF gy_cdata.
*DATA gs_cdata_update TYPE gy_cdata.

*&---------------------------------------------------------------------*
*& 包含 ZMMRP007_SEL
*&---------------------------------------------------------------------*

SELECTION-SCREEN FUNCTION KEY 2.
SELECTION-SCREEN:BEGIN OF BLOCK bl1 WITH FRAME.
SELECTION-SCREEN SKIP 1.
PARAMETERS:p_rd1 RADIOBUTTON GROUP g1."单个采购订单确定
PARAMETERS:p_rd2 RADIOBUTTON GROUP g1."批导一采购订单确认
SELECTION-SCREEN SKIP 1.
PARAMETERS: pa_file TYPE rlgrap-filename."文件夹上传
SELECTION-SCREEN:END OF BLOCK bl1.

*&---------------------------------------------------------------------*
*& 包含               ZMMRP007_FRM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM download_template .
  DATA:
    lw_objdata     TYPE wwwdatatab,
    l_filename     TYPE string  VALUE '卡片BDC',    "默认下载名称
    l_fullpath     TYPE string  VALUE 'D:\' ,       "文件保存目录
    l_path         TYPE string  VALUE 'D:\' ,       "模板保存路径
    lw_destination LIKE rlgrap-filename,
    l_rc           LIKE sy-subrc,
    l_user_action  TYPE i.

* 保存下载的模板
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      default_extension    = 'xlsx'             "文件格式
      default_file_name    = '采购订单交期确认批导模板'            "默认名称
    CHANGING
      filename             = l_filename             "默认下载名称
      path                 = l_path                 "文件保存目录
      fullpath             = l_fullpath             "获取模板保存路径
      user_action          = l_user_action
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.

  IF l_user_action NE 0.
    MESSAGE e208(00) WITH '取消操作'.
  ENDIF.


  lw_destination = l_fullpath.

* 检查模板是否为空
  IF l_fullpath = '' OR l_path IS  NOT INITIAL.
    SELECT SINGLE *
    INTO CORRESPONDING FIELDS OF lw_objdata
   FROM wwwdata WHERE objid = 'ZMMRP007_TEMPLATE' .

    CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
      EXPORTING
        key         = lw_objdata
        destination = lw_destination
      IMPORTING
        rc          = l_rc.

    IF l_rc NE 0.
      MESSAGE  '模板下载失败'
       TYPE 'E'.
*   模板下载失败!

      RETURN.
    ENDIF.
  ENDIF.
ENDFORM.


FORM get_excel .
  DATA : l_filetab TYPE filetable,
         l_waftab  LIKE LINE OF l_filetab,
         l_rc      TYPE i.
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = '打开文件'
      initial_directory       = 'C:/'
    CHANGING
      file_table              = l_filetab
      rc                      = l_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 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.
    EXIT.
  ELSE.
    READ TABLE l_filetab INTO l_waftab INDEX 1.
    pa_file = l_waftab-filename.
    CLEAR: l_filetab,l_waftab.
  ENDIF.
ENDFORM.

FORM get_data .

  TYPES: BEGIN OF ly_excel_data,
           ebeln       TYPE ekko-ebeln, "采购订单号
           ebelp       TYPE ekpo-ebelp, "采购订单项次
           zindex      TYPE etens, "确认序号
           ebtyp       TYPE ekes-ebtyp, "确认类型
           eindt       TYPE ekes-eindt, "交货日期
           menge       TYPE ekpo-menge, "数量
           bstme       TYPE bstme, "单位
*           peinh       TYPE ztmm_quotesi-peinh, "价格单位
           suppliercno TYPE xblnr_long, "参考
         END OF ly_excel_data.

  DATA ls_excel_data TYPE ly_excel_data.
  DATA lt_excel TYPE TABLE OF alsmex_tabline WITH HEADER LINE.
  DATA l_index LIKE sy-tabix.
  DATA: lv_lines TYPE i.

  FIELD-SYMBOLS: .
  IF pa_file IS NOT INITIAL.
    CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename                = pa_file
        i_begin_col             = '1'
        i_begin_row             = '2'
        i_end_col               = '8'
        i_end_row               = '50000'
      TABLES
        intern                  = lt_excel
      EXCEPTIONS
        inconsistent_parameters = 1
        upload_ole              = 2
        OTHERS                  = 3.
    IF sy-subrc = 0.
*&& 将EXCEL格式中的数据导入到内表TAB_LOAD中
      LOOP AT lt_excel.
        ASSIGN COMPONENT lt_excel-col OF STRUCTURE ls_excel_data TO .
         = lt_excel-value.
        AT END OF row.
          MOVE-CORRESPONDING ls_excel_data TO gs_cdata."跟Excel的列一一对应的内表
          APPEND gs_cdata TO gt_cdata.
          CLEAR: ls_excel_data, gs_cdata.
        ENDAT.
      ENDLOOP.
      lv_lines = lines( gt_cdata ).
      IF lv_lines > 200.
        MESSAGE  '本次导入数据超过200,请拆分数据文件!' TYPE 'S'DISPLAY LIKE 'E'.
        STOP.
      ENDIF.
    ENDIF.

  ELSE.
    MESSAGE  '请输入文件路径!' TYPE 'S'DISPLAY LIKE 'E'.
    STOP.
  ENDIF.

ENDFORM.

你可能感兴趣的:(SAP,Abap实战篇,经验分享)