首页 > 其他分享 >abap 报表开发 一些功能汇总

abap 报表开发 一些功能汇总

时间:2022-09-20 13:23:12浏览次数:68  
标签:SELECTION 报表 bdc text SCREEN 汇总 frm abap &----------------------------------

以一个test报表 记录开发遇到的情况以及处理的问题 只是一个知识点的记录,主要是为了自己方便查找,如果能帮到其他学习的人,十分荣幸
报表名称

*&---------------------------------------------------------------------*`
*& Report test
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT test.

数据库声明

*&---------------------------------------------------------------------*
*& Database table declaration/数据库表声明
*&---------------------------------------------------------------------*
TABLES:.

类型结构

*&---------------------------------------------------------------------*
*& Structure type declaration/类型结构声明
*&---------------------------------------------------------------------*
TYPES: BEGIN OF ty_alv,
         matnr       TYPE mara-matnr,  " 参考类型可为标准类型 或者 参考表字段 或者 参考数据元素
       END OF ty_alv,
       tty_alv TYPE STANDARD TABLE OF ty_alv.

定义数据对象

*&---------------------------------------------------------------------*
*& Data Object declaration/数据对象定义
*&---------------------------------------------------------------------*
DATA: gt_alv   TYPE STANDARD TABLE OF ty_alv.

*&---ALV Object 定义alv所需要的数据对象
DATA: gt_fieldcat TYPE lvc_t_fcat,
      gs_layout   TYPE lvc_s_layo.

选择屏幕处理

*&---------------------------------------------------------------------*
*& Selection Screen/选择屏幕
*&---------------------------------------------------------------------*
"设置按钮 至多5个
SELECTION-SCREEN: FUNCTION KEY 1,
FUNCTION KEY 2.

" 基本的选择屏幕类型
SELECTION-SCREEN BEGIN OF BLOCK blk1.
  PARAMETERS:
    p_test TYPE test  DEFAULT sy-datum(4) OBLIGATORY.   " 单选  OBLIGATORY必填 DEFAULT默认
  SELECT-OPTIONS:
    s_test FOR test DEFAULT sy-datum+4(2) OBLIGATORY,  " 范围选择 
  PARAMETERS:
    s_test1 RADIOBUTTON GROUP sel USER-COMMAND is_cur  DEFAULT 'X', " 单选框 USER-COMMAND:指定用户事件 DEFAULT 'X':默认选中
    s_test2 RADIOBUTTON GROUP sel . 
  PARAMETERS:
    s_test AS CHECKBOX USER-COMMAND is_sel. " 多选框 USER-COMMAND:指定用户事件
  SELECT-OPTIONS:
    s_test FOR test MODIF ID v. " MODIF ID:第二屏判断标识
SELECTION-SCREEN END OF BLOCK blk1.

"限定在一行 多于单选框需要在一行展示的情况
SELECTION-SCREEN BEGIN OF LINE.
*第一个RADIOBUTTON
    SELECTION-SCREEN POSITION 1.            "1是RADIOBUTTON横坐标
    PARAMETERS:s_test1 TYPE c RADIOBUTTON GROUP g1 USER-COMMAND is_cur DEFAULT 'X'.
    SELECTION-SCREEN COMMENT 2(7) TEXT-003 FOR FIELD s_test1. "2是文本横坐标,7是文本长度
*第二个RADIOBUTTON
    SELECTION-SCREEN POSITION 11.           "11是RADIOBUTTON横坐标
    PARAMETERS:s_test2 TYPE c RADIOBUTTON GROUP g1.
    SELECTION-SCREEN COMMENT 12(7) TEXT-004 FOR FIELD s_test2."12是文本横坐标,7是文本长度
SELECTION-SCREEN END OF LINE.

"构建文字帮助描述
SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME TITLE TEXT-001.
  SELECTION-SCREEN COMMENT /3(79) TEXT-002.  " 注意 / 不加会报错的
  SELECTION-SCREEN COMMENT /3(79) TEXT-003.
  SELECTION-SCREEN COMMENT /3(79) TEXT-004.
SELECTION-SCREEN END OF BLOCK blk2.

"按钮
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME.
  SELECTION-SCREEN PUSHBUTTON 7(20) but1 USER-COMMAND download.  "but1 按钮标识
SELECTION-SCREEN END OF BLOCK blk1.
"文件框
SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME.
  PARAMETERS: p_file TYPE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK blk2.

"构建自定义帮助F4
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_test. " p_test 是单选  多选需要 p_test-low
  PERFORM frm_create_mdnum_help. " 创建自定义帮助的例程

屏幕初始化

*&---------------------------------------------------------------------*
*&INITIALIZATION/选择屏幕前初始化(选择屏幕展示之前执行)
*&---------------------------------------------------------------------*
INITIALIZATION .
  PERFORM frm_init_func_key.  " 初始化功能键(具体逻辑看 下面例程 初始化功能键) 若没有 可注释

  " 给按钮添加图标和文本   按钮较多 可形成流例程
  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name   = icon_export " 按钮的图片和文字
      text   = TEXT-001  " 按钮的文本
      info   = TEXT-001
    IMPORTING
      result = but1  " 哪一个按钮 
    EXCEPTIONS
      OTHERS = 0.

控制第二屏输出

*&---------------------------------------------------------------------*
*& AT SELECTION-SCREEN OUTPUT/选择屏幕控制(第二屏输出)
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT .
  PERFORM frm_dynamic_screen. " 处理第二屏逻辑(具体逻辑看 下面例程 处理第二屏逻辑) 若没有 可注释

选择屏幕执行

*&---------------------------------------------------------------------*
*& AT SELECTION-SCREEN/选择屏幕执行(参数输入检查)
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN .
  PERFORM frm_check_data. " 检查数据 具体逻辑看 (下面例程 检查数据) 若没有 可注释
  PERFORM frm_set_ucomm_event.  " 设置功能键事件  (具体逻辑看 下面例程 设置功能键事件) 若没有 可注释

批导功能一般用以下的 选择屏幕执行

*----------------------------------------------------------------------*
*                  选 择 屏 幕 字 段 处 理 块
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  " 本地文件搜索帮助
  PERFORM frm_file_open.   " 用于上传文件

AT SELECTION-SCREEN.
  IF sscrfields-ucomm = 'DOWNLOAD'.
    " 下载模板
    PERFORM frm_excel_export.   " 用于下载文件
  ENDIF.

执行数据查询

*&---------------------------------------------------------------------*
*& START-OF-SELECTION/开始选择屏幕(点击执行按钮之后执行的语块)
*&---------------------------------------------------------------------*
START-OF-SELECTION .
  PERFORM frm_deal_data.  " 处理数据 

  IF sy-batch EQ abap_on.  " 后台传输 一般用于 定时传输时的配置
    PERFORM frm_sel_all_data. " 默认全选
    PERFORM frm_trans. " 传输
  ENDIF.

批导一般使用以下的屏幕查询

*----------------------------------------------------------------------*
*                  逻 辑 处 理 块                                      *
*----------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM frm_getdata. " 获取上载的excel数据 见例程
  PERFORM frm_dealdata. " 处理数据,校验数据是否正确 不写例程了
  PERFORM frm_layout. " 展现形式  不写例程了
  PERFORM frm_fieldcat. " 展示字段以及内表字段对应关系 不写例程了
  PERFORM frm_output. "展示 不写例程了

结束屏幕,屏幕展现

*&---------------------------------------------------------------------*
*& END-OF-SELECTION/结束选择屏幕(程序结束处理,输出等)
*&---------------------------------------------------------------------*
END-OF-SELECTION .
  PERFORM frm_display_alv. " 屏幕展现

初始化功能按键例程

*&---------------------------------------------------------------------*
*& Form frm_init_func_key
*&---------------------------------------------------------------------*
*& 初始化功能键
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_init_func_key .

  sscrfields-functxt_01 = VALUE smp_dyntxt( icon_id = icon_create_text
                                            icon_text = TEXT-s01  " 标准写法 在文本元素里填写按钮名称
                                           ).
  sscrfields-functxt_01 = VALUE smp_dyntxt( icon_id = icon_create_text
                                            icon_text = TEXT-s02  " 
                                           ).
ENDFORM.

控制第二屏输出

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

  LOOP AT SCREEN.
    IF screen-group1 EQ 'V' AND s_test IS NOT INITIAL. " 一般来说 通过 modif id标识 以及 单选框来控制展现 
      screen-active = 0. " 1:显示;0:隐藏
    ENDIF.
    MODIFY SCREEN.
  ENDLOOP.

ENDFORM.

自定义帮助f4例程

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

  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = 'PLNUM'     " 参考的表字段
      dynpprog        = sy-repid
      dynpnr          = sy-dynnr
      dynprofield     = 'P_MDNUM'  " 用于选择屏幕上的哪一个
      value_org       = 'S'
    TABLES
      value_tab       = gt_mdnum   " 自定义的表 需要全局变量 一般来说也是通过例程处理的 frm_get_plnum_f4 
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.

ENDFORM.

自定义帮助f4例程 用于查出 填入f4的数据

*&---------------------------------------------------------------------*
*& Form frm_get_mdnum_f4
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_plnum_f4 .

  SELECT
    FROM 
  FIELDS
    
  INTO CORRESPONDING FIELDS OF TABLE @gt_mdnum.

ENDFORM.

检查数据的例程

*&---------------------------------------------------------------------*
*& Form frm_check_data 检查数据
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_check_data .

  "数据检查 无法查询早于上线时间2021的数据
  IF s_gjahr < '2021' .
    MESSAGE TEXT-002 TYPE 'E'.
  ENDIF.

ENDFORM.

用于查出报表数据 以及报表数据处理

*&---------------------------------------------------------------------*
*& Form frm_get_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_data .
  在这里写下主体sql填入内表
  根据余下空的数据 在查出来后填入
ENDFORM.

展示屏幕的例程

*&---------------------------------------------------------------------*
*& Form frm_display_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_display_alv .
  PERFORM frm_set_layout.  "设置ALV输出格式
  PERFORM frm_set_fieldcat.  "设置ALV输出字段
  PERFORM frm_call_alv TABLES gt_alv.  "调用ALV函数
ENDFORM.

设置ALV输出格式

*&---------------------------------------------------------------------*
*& Form frm_set_layout
*&---------------------------------------------------------------------*
*& 设置ALV输出格式
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_set_layout .

  gs_layout = VALUE #( zebra = abap_on       " 斑马行
                       cwidth_opt = abap_on  
                       box_fname = |CBOX| ).  " 选择列

ENDFORM.

设置ALV输出字段

 DEFINE _set_fieldcat.
    APPEND VALUE #(
      fieldname = &1    "
      ref_table = &2
      ref_field = &3
      coltext = &4
      scrtext_l = &4
      scrtext_m = &4
      scrtext_s = &4
      edit = SWITCH #( &1 WHEN 'TEST' THEN abap_on )    " 是否可编辑
      no_zero = SWITCH #( &1 WHEN 'TEST' THEN abap_on )  " 是否不为0
    ) TO gt_fieldcat.
  END-OF-DEFINITION.


  _set_fieldcat 'TEST' 'TEST' 'TEST' TEXT-f01.  "TEST  " 多个字段拷贝多行即可

调用ALV函数

*&---------------------------------------------------------------------*
*& Form frm_call_alv
*&---------------------------------------------------------------------*
*& 调用ALV函数
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_call_alv
  TABLES
    pt_outtab TYPE table.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_user_command  = 'USER_COMMAND'   " 按钮事件
      i_callback_pf_status_set = 'SET_PF_STATUS'  " 自定义状态栏 se41拷贝状态栏
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fieldcat
      i_save                   = 'A'
    TABLES
      t_outtab                 = gt_alv
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.

ENDFORM.

设置状态栏
&---------------------------------------------------------------------
*& Form SET_PF_STATUS
&---------------------------------------------------------------------

  •   设置GUI状态
    

----------------------------------------------------------------------
FORM set_pf_status
USING
pt_extab TYPE slis_t_extab.

SET PF-STATUS 'STANDARD' EXCLUDING pt_extab. " 状态栏

  • SET TITLEBAR 'ALV_TITLE' WITH gv_tgsys. " 状态标题 可以 通过with 填写参数
    ENDFORM.

用户事件
&---------------------------------------------------------------------
*& Form USER_COMMAND
&---------------------------------------------------------------------

  •   text  用户事件
    

----------------------------------------------------------------------
FORM user_command
USING
pv_ucomm LIKE sy-ucomm
ps_selfield TYPE slis_selfield.

DATA: lo_grid TYPE REF TO cl_gui_alv_grid.

CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lo_grid.

CALL METHOD lo_grid->check_changed_data. "检查ALV更改数据

DEFINE _refresh.
ps_selfield-refresh = abap_on.
ps_selfield-col_stable = abap_on.
ps_selfield-row_stable = abap_on.
lo_grid->get_frontend_layout( IMPORTING es_layout = DATA(ls_layout) ).
ls_layout-cwidth_opt = abap_on.
lo_grid->set_frontend_layout( is_layout = ls_layout ).
END-OF-DEFINITION.

CASE pv_ucomm.
WHEN '&SPL'.
PERFORM frm_trans. " 传输功能
PERFORM frm_dbc. " DBC功能
PERFORM frm_pd. " 批导使用函数功能
_refresh.
WHEN OTHERS.
ENDCASE.

CALL METHOD cl_gui_control=>set_focus
EXPORTING
control = lo_grid.

ENDFORM.

设置功能按键

*&---------------------------------------------------------------------*
*& Form frm_set_ucomm_event
*&---------------------------------------------------------------------*
*& 设置功能键事件
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_set_ucomm_event .

  CASE sscrfields-ucomm.
    WHEN 'FC01'.
      PERFORM frm_maintenance_view USING 'ZTFI012'. " 引用自建表
  ENDCASE.

ENDFORM.

维护自建表

*&---------------------------------------------------------------------*
*& Form frm_MAINTENANCE_view
*&---------------------------------------------------------------------*
*& 维护扩充视图配置
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_maintenance_view
  USING
    pv_view TYPE dd02v-tabname.

  DATA: lt_sellist TYPE STANDARD TABLE OF vimsellist.

  lt_sellist = VALUE #(
  ( viewfield = 'BUKRS' operator = 'EQ' value = p_bukrs tabix = 1 and_or = 'AND' )
  ( viewfield = 'SPRAS' operator = 'EQ' value = sy-langu tabix = 2 )
  ).

  CALL FUNCTION 'VIEW_MAINTENANCE_CALL'
    EXPORTING
      action                       = 'S'
      view_name                    = pv_view  " 表名
    TABLES
      dba_sellist                  = lt_sellist  " 参数填写
    EXCEPTIONS
      client_reference             = 1
      foreign_lock                 = 2
      invalid_action               = 3
      no_clientindependent_auth    = 4
      no_database_function         = 5
      no_editor_function           = 6
      no_show_auth                 = 7
      no_tvdir_entry               = 8
      no_upd_auth                  = 9
      only_show_allowed            = 10
      system_failure               = 11
      unknown_field_in_dba_sellist = 12
      view_not_found               = 13
      maintenance_prohibited       = 14
      OTHERS                       = 15.

  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 检查数据
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM user_command
  USING
      i_ucomm TYPE sy-ucomm
      i_wa_selfield TYPE slis_selfield.

  READ TABLE gt_alv INTO DATA(ls_alv) INDEX i_wa_selfield-tabindex.

  CASE i_ucomm.
    WHEN '&IC1'.  "Double click
      IF i_wa_selfield-fieldname = 'ANLN1' AND ls_alv-anln1 IS NOT INITIAL.
        SET PARAMETER ID 'AN1' FIELD ls_alv-anln1.    " 对应事务码 F1对应元素 技术名称 参数ID 
        SET PARAMETER ID 'BUK' FIELD ls_alv-bukrs.
        CALL TRANSACTION 'AW01N' AND SKIP FIRST SCREEN. " 调用相关事务码 并跳过第一屏幕
      ENDIF.
    WHEN OTHERS.
  ENDCASE.
ENDFORM.

常见功能:传输数据

*&---------------------------------------------------------------------*
*& Form frm_transfer_deliveryorder
*&---------------------------------------------------------------------*
*& 调用ALV函数
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_transfer_deliveryorder.
  DATA: is_para TYPE zsxx001,
        es_para TYPE zsxx002,
        it_head TYPE TABLE OF zsmm018,
        is_head TYPE zsmm018,
        it_item TYPE TABLE OF zsmm019,
        is_item TYPE zsmm019.

  DATA: lt_selected LIKE gt_alv,
        ls_selected LIKE LINE OF gt_alv.


  MOVE gt_alv TO lt_selected.
  DELETE lt_selected WHERE zbox <> 'X'.
  DELETE ADJACENT DUPLICATES FROM lt_selected COMPARING vbeln.  " 去重

*  按交货单号逐条发送交货单
  LOOP AT lt_selected INTO ls_selected.
    REFRESH:it_head, it_item.

    MOVE-CORRESPONDING ls_selected TO is_head.
    APPEND is_head TO it_head.

    LOOP AT gt_alv ASSIGNING <fs_alv> WHERE vbeln = ls_selected-vbeln.
      APPEND INITIAL LINE TO it_item ASSIGNING FIELD-SYMBOL(<fs_item>).
      MOVE-CORRESPONDING <fs_alv> TO <fs_item>.
    ENDLOOP.

    _get_uuid: is_para-msgid. " 获取uuid
    is_para-tgsys = gv_tgsys. " 传输系统

    CALL FUNCTION 'ZFM_SEND_DN'   " 一般通过自己编写的函数传输
      EXPORTING
        is_para = is_para
      IMPORTING
        es_para = es_para
      TABLES
        it_head = it_head
        it_item = it_item.

    LOOP AT gt_alv ASSIGNING <fs_alv> WHERE vbeln = <fs_alv>-vbeln.   " 回填结果
      IF es_para-msg_typ = 'S'.
        <fs_alv>-icon = gc_icon_green.
        <fs_alv>-zsend = abap_on.
        UPDATE likp SET zsend = abap_on WHERE vbeln = <fs_alv>-vbeln.  " 业务要求 更新表中用户新增的字段
        IF sy-subrc EQ 0.
          COMMIT WORK.
        ENDIF.
      ELSE.
        <fs_alv>-icon = gc_icon_red.
      ENDIF.
      <fs_alv>-msg = es_para-message.
    ENDLOOP.
  ENDLOOP.

ENDFORM.

用于上传文件

*&---------------------------------------------------------------------*
*& Form frm_file_open 上传文件
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_file_open .

  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      mask             = ',EXCEL FILE,*.XLS;*XLSX;'
      mode             = '0' "S是保存,0是打开
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

ENDFORM.

下载文件

*&---------------------------------------------------------------------*
*& Form frm_excel_export 下载模板
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_excel_export .
  " 实例化所需对象
  DATA: ls_objdata     LIKE wwwdatatab, " 引用数据类型
        ls_mime        LIKE w3mime,
        lv_filename    TYPE string,
        lv_fullpath    TYPE string VALUE 'D:\TEST\',
        lv_path        TYPE string VALUE 'D:\TEST\',
        lv_destination TYPE rlgrap-filename,  "用于装载/卸载的局部文件
        lv_objnam      TYPE string,
        ls_rc          LIKE sy-subrc, " 返回码
        lv_errtxt      TYPE string.  " 定义个字符串,存放错误提示
  DATA: lv_objid TYPE wwwdatatab-objid,
        lv_dest  TYPE sapb-sappfad. " SAP存档文件路径
  DATA: lv_fname TYPE string.
  DATA: lv_row TYPE i.

  lv_objid = 'ZMMB005_01'. "此处是excel模板名称  模板通过 swm0 二进制上传
  lv_filename = TEXT-002. "此处时下载下来的文件名称

  " 调用保存对话框
  CALL METHOD cl_gui_frontend_services=>file_save_dialog   "
    EXPORTING
      default_extension    = 'XLSX'
      default_file_name    = lv_filename
    CHANGING
      filename             = lv_filename
      path                 = lv_path
      fullpath             = lv_fullpath
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.

  IF lv_fullpath = ''.
    MESSAGE TEXT-003 TYPE 'E'.
  ENDIF.

  IF sy-subrc = 0. " 如果成功执行
    lv_dest = lv_fullpath.
    CONDENSE lv_objnam NO-GAPS. " 去除空格

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

    " 检查表wwwdata中是否存在所指定的模板文件
    IF sy-subrc NE 0 OR ls_objdata-objid EQ space. " 如果不存在,则告知错误情况
      CONCATENATE TEXT-004 lv_objnam TEXT-005 INTO lv_errtxt.
      MESSAGE lv_errtxt TYPE 'I'.
    ENDIF.
    lv_destination = lv_dest. "保存路径
    " 如果存在,调用DOWNLOAD_WEB_OBJECT 函数下载模板到路径下
    CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
      EXPORTING
        key         = ls_objdata
        destination = lv_destination
      IMPORTING
        rc          = ls_rc.  " 传输是否成功

    IF ls_rc NE 0.  " 不为0 即失败了
      CONCATENATE TEXT-004 lv_objnam TEXT-006 INTO lv_errtxt.
      MESSAGE lv_errtxt TYPE 'E'.
    ENDIF.

    lv_fname = lv_destination.
  ENDIF.

  "保存excel数据
  PERFORM frm_save_excel.
ENDFORM.

保存excel数据

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

  GET PROPERTY OF gs_excel 'ACTIVESHEET' = gs_sheet. "获取活动sheet
  FREE OBJECT gs_sheet.
  FREE OBJECT gs_workbook.
  GET PROPERTY OF gs_excel 'ACTIVEWORKBOOK' = gs_workbook. "获取工作簿
  CALL METHOD OF gs_workbook 'SAVE'.
  SET PROPERTY OF gs_excel 'VISIBLE' = 1. " 是否显示EXCEL 此处显示不退出
  CALL METHOD OF gs_workbook 'CLOSE'.
  CALL METHOD OF gs_excel 'QUIT'. " 关闭excel
  FREE OBJECT gs_sheet.
  FREE OBJECT gs_workbook.
  CALL METHOD OF gs_workbook 'CLOSE'. "? 为什么这里关了两次 是释放空间的时候自动打开了吗
  FREE OBJECT gs_excel.

获取excel数据

*&---------------------------------------------------------------------*
*& Form frm_getdata  获取excel中的数据
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_getdata .
  CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE' " 读取文件到内表
    EXPORTING
      filename                = p_file  " 读取的文件
      i_begin_col             = 1    " 从哪一列开始读取数据 根据模板来定
      i_begin_row             = 3    " 从哪一行开始读取数据 根据模板来定
      i_end_col               = 9999
      i_end_row               = 9999
    TABLES
      intern                  = gt_excels
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

*  DELETE gt_data WHERE name_org1 = '' AND bu_group = ''.  " 删除必填项为空的数据
  IF gt_excels IS INITIAL.
    MESSAGE TEXT-007 TYPE 'S' DISPLAY LIKE 'E'. " 结果是S 但是展示想过为 E
  ENDIF.

  "内外码转换
  LOOP AT gt_excels ASSIGNING FIELD-SYMBOL(<fs_excels>).
    IF <fs_excels> IS INITIAL .
      CONTINUE.
    ENDIF.

    _conv_alpha 'IN'  <fs_excels>-lifnr.

    APPEND INITIAL LINE TO gt_data ASSIGNING FIELD-SYMBOL(<fs_data>).
    MOVE-CORRESPONDING <fs_excels> TO <fs_data>.


  ENDLOOP.

ENDFORM.

功能 函数批导

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

  " 用于处理长文本
  TYPES: BEGIN OF ty_line,
           tdline TYPE tdline,
         END OF  ty_line.

  DATA prheader     TYPE bapimereqheader.
  DATA prheaderx    TYPE bapimereqheaderx.
  DATA number       TYPE bapimereqheader-preq_no.
  DATA return       TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE.
  DATA pritem       TYPE STANDARD TABLE OF bapimereqitemimp.
  DATA pritemx      TYPE STANDARD TABLE OF bapimereqitemx.
  DATA pritemexp    TYPE STANDARD TABLE OF bapimereqitem.
  DATA extensionin  TYPE STANDARD TABLE OF bapiparex.
  DATA praccount    TYPE STANDARD TABLE OF bapimereqaccount.
  DATA praccountx   TYPE STANDARD TABLE OF bapimereqaccountx.
  DATA prheadertext TYPE STANDARD TABLE OF bapimereqheadtext.
  DATA lv_str       TYPE string.
  DATA lines        TYPE TABLE OF ty_line.
  DATA flines       TYPE TABLE OF tline WITH HEADER LINE.
  DATA fname        TYPE tdobname.
  DATA lv_msg       TYPE string.



  IF line_exists( gt_alv[ type = 'E' ] ).
    MESSAGE e002(zpp).  " 请先处理错误数据再进行批导操作
    EXIT.
  ENDIF.

  SORT gt_alv BY serial.


  LOOP AT gt_alv ASSIGNING FIELD-SYMBOL(<fs_import>).

    prheader-pr_type = <fs_import>-bsart.
    prheaderx-pr_type = 'X'.

    pritem = VALUE #( BASE pritem
  (
    preq_item = <fs_import>-bnfpo
    pur_group = <fs_import>-ekgrp
    preq_name = <fs_import>-afnam
    short_text = <fs_import>-txz01
    material = <fs_import>-matnr
    plant = <fs_import>-werks
    store_loc = <fs_import>-lgort
    matl_group = <fs_import>-matkl
    quantity = <fs_import>-menge
    unit = <fs_import>-meins
    deliv_date = <fs_import>-lfdat
    item_cat = <fs_import>-pstyp
    acctasscat = <fs_import>-knttp
   )
  ).

    pritemx = VALUE #( BASE pritemx
    (
    preq_item = <fs_import>-bnfpo
    pur_group = abap_on
    preq_name = abap_on
    short_text = abap_on
    material = abap_on
    plant = abap_on
    store_loc = abap_on
    matl_group = abap_on
    quantity = abap_on
    unit = abap_on
    deliv_date = abap_on
    item_cat = abap_on
    acctasscat = abap_on
    )
    ).

    praccount = VALUE #( BASE praccount
    (
    preq_item = <fs_import>-bnfpo
    serial_no = '1'
    costcenter = <fs_import>-kostl
    asset_no = <fs_import>-anln1
    gl_account = <fs_import>-sakto
    )
    ).

    praccountx = VALUE #( BASE praccountx
    (
    preq_item = <fs_import>-bnfpo
    serial_no = '1'
    costcenter = abap_on
    asset_no = abap_on
    gl_account = abap_on
    )
    ).

    extensionin = VALUE #( BASE extensionin
    (
      structure = 'BAPI_TE_MEREQITEM'
      valuepart1 = VALUE bapi_te_mereqitem(
                         preq_item = <fs_import>-bnfpo
                         verid = <fs_import>-verid
                         )
    )
    ).
    extensionin = VALUE #( BASE extensionin
    (
      structure = 'BAPI_TE_MEREQITEMX'
      valuepart1 = VALUE bapi_te_mereqitemx(
                         preq_item = <fs_import>-bnfpo
                         verid = abap_on
                         )
    )
    ).

    prheadertext = VALUE #( BASE prheadertext
    (
    text_id = 'B01'
    text_line = <fs_import>-ltext
    text_form = '*'
    )
    ).

    AT END OF serial.

      CALL FUNCTION 'BAPI_PR_CREATE'
        EXPORTING
          prheader     = prheader
          prheaderx    = prheaderx
        IMPORTING
          number       = number
        TABLES
          return       = return[]
          pritem       = pritem
          pritemx      = pritemx
          pritemexp    = pritemexp
          praccount    = praccount
          praccountx   = praccountx
          prheadertext = prheadertext
          extensionin  = extensionin.

      LOOP AT return[] INTO DATA(ls_return) WHERE type CA 'AEX'.
        IF ls_return-id NE 'BAPI' AND ls_return-number NE '001'.
          lv_msg = |{ lv_msg };{ ls_return-message }|.
        ENDIF.
      ENDLOOP.
      IF sy-subrc = 0.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        <fs_import>-icon = gc_icon_red.
        <fs_import>-msg = lv_msg.
      ELSE.
        READ TABLE return[] INTO ls_return WITH KEY type = 'S'.
        IF sy-subrc = 0.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.
          <fs_import>-icon = gc_icon_green.
          <fs_import>-msg = ls_return-message.
        ENDIF.
      ENDIF.

      LOOP AT gt_alv ASSIGNING FIELD-SYMBOL(<fs_res>) WHERE serial = <fs_import>-serial.
        <fs_res>-icon = <fs_import>-icon.
        <fs_res>-msg = <fs_import>-msg.
      ENDLOOP.

      CLEAR: prheader,prheaderx,return,pritem,pritemx,pritemexp,praccount,praccountx,prheadertext,extensionin.
    ENDAT.

  ENDLOOP.

ENDFORM.

DBC方式 shdb事务码录屏

*&---------------------------------------------------------------------*
*& Macro Define/宏定义
*&---------------------------------------------------------------------*
  DEFINE bdc_field.   "BDC_FIELD
    CLEAR gt_bdcdata.
    gt_bdcdata-fnam = &1.
    gt_bdcdata-fval = &2.
*    CONDENSE gt_bdcdata-fval NO-GAPS.
    APPEND gt_bdcdata.
  END-OF-DEFINITION.
  DEFINE bdc_dynpro.  "BDC_DYNPRO
    CLEAR gt_bdcdata.
    gt_bdcdata-program  = &1.
    gt_bdcdata-dynpro   = &2.
    gt_bdcdata-dynbegin = 'X'.
    APPEND gt_bdcdata.
  END-OF-DEFINITION.
 bdc_dynpro 'SAPLBUS_LOCATOR' '3000'.
    bdc_field 'BDC_OKCODE' '=SCREEN_1000_OPEN'.
    bdc_field 'BDC_CURSOR' 'BUS_LOCA_SRCH01-SEARCH_TYPE'.

    bdc_dynpro 'SAPLBUPA_DIALOG_JOEL' '1600'.
    bdc_field 'BDC_OKCODE' '=BUS_MAIN_ENTER'.
    bdc_field 'BDC_CURSOR' 'BUS_JOEL_MAIN-OPEN_NUMBER'.
    bdc_field 'BUS_JOEL_MAIN-OPEN_NUMBER' <fs_check>-lifnr.

*    IF lines MOD 2 NE 0.
*      bdc_dynpro 'SAPLBUS_LOCATOR' '3000'.
*      bdc_field 'BDC_OKCODE' '=SCREEN_1000_ACTIVITY'.
*      bdc_field 'BDC_CURSOR' 'BUS000FLDS-TITLE_MEDI'.
*    ENDIF.

    bdc_dynpro 'SAPLBUS_LOCATOR' '3000'.
    bdc_field 'BDC_OKCODE' '=BUAQ'.
    bdc_field 'BDC_CURSOR' 'BUS000FLDS-TITLE_MEDI'.

    bdc_dynpro 'SAPLSZA1' '0204'.
    bdc_field 'BDC_CURSOR' 'ADDR1_DATA-NAME1'.
    bdc_field 'BDC_OKCODE' '=CONT'.
    bdc_field 'ADDR1_DATA-NAME1' <fs_check>-name1.
    bdc_field 'ADDR1_DATA-SORT1' <fs_check>-sort.
    IF <fs_check>-name2 IS NOT INITIAL.
      bdc_field 'ADDR1_DATA-NAME2' <fs_check>-name2.
    ENDIF.
    IF <fs_check>-name3 IS NOT INITIAL.
      bdc_field 'ADDR1_DATA-NAME3' <fs_check>-name3.
    ENDIF.
    IF <fs_check>-name2 IS NOT INITIAL.
      bdc_field 'ADDR1_DATA-NAME4' <fs_check>-name4.
    ENDIF.

    bdc_dynpro 'SAPLBUS_LOCATOR' '3000'.
    bdc_field 'BDC_OKCODE' '=SCREEN_1000_ACTIVITY'.
    bdc_field 'BDC_CURSOR' 'BUS000FLDS-TITLE_MEDI'.

    bdc_dynpro 'SAPLSPO1' '0100'.
    bdc_field 'BDC_OKCODE' '=YES'.


    CALL TRANSACTION 'BP' USING gt_bdcdata
                          MODE p_mode
                          MESSAGES INTO gt_msgtab
                          UPDATE 'S'.     "批导更新模式(A = '异步',S = '同步')

    LOOP AT gt_msgtab WHERE msgtyp = 'A' OR msgtyp = 'E' OR msgtyp = 'X'.
      MESSAGE ID gt_msgtab-msgid TYPE gt_msgtab-msgtyp NUMBER gt_msgtab-msgnr INTO DATA(lv_msg)
          WITH gt_msgtab-msgv1 gt_msgtab-msgv2 gt_msgtab-msgv3 gt_msgtab-msgv4.
      <fs_check>-message = lv_msg && '!' && msg.
    ENDLOOP.
  ENDLOOP.

标签:SELECTION,报表,bdc,text,SCREEN,汇总,frm,abap,&----------------------------------
From: https://www.cnblogs.com/pnj-owowa/p/16707045.html

相关文章

  • 【Hive】数据倾斜原因及解决方法汇总
    1)数据倾斜根本原因:由于数据分布不均匀,导致map端读取的数据分布不均匀(数据长尾分布),从而使得map处理的数据量差异过大。(2)解决思路:Hive是分阶段执行的,map处理数据量的差异取......
  • .NET Core/.NET5/.NET6 开源项目汇总!
    前言企业管理系统一般包含后台管理UI、组织机构管理、权限管理、日志、数据访问、表单、工作流等常用必备功能。下面收集的几款优秀开源的管理系统,值得大家入门学习。如有......
  • SAP ABAP 选择屏幕选择下拉框
    PARAMETERS:p_name(2) AS LISTBOX VISIBLE LENGTH 12. DATA: lv_name TYPE vrm_id,        lt_list TYPE vrm_values,        ls_list LIKE......
  • SAP ABAP 退出程序(LEAVE/STOP/EXIT/RETURN/CHECK/CONTINUE)
    CALLTRANSACTION'ZFI001'."离开当前程序跳到对应程序,可以回到原程序LEAVETOTRANSACTION'ZFI001'"离开当前程序跳到对应程序,不能回到原程序LEAVEPROGRAM.......
  • SAP ABAP选择屏幕(ACTIVE,INPUT,REQUIRED)
    1.可见性:ACTIVE示例:PARAMETERS:p_nameLIKEtab-nameMODIFIDm1,"名字p_ageLIKEtab_ageMODIFIDm2."年龄PARAMETERS:    p_radio1 R......
  • SAP ABAP READ TABLE读取不到数据的问题
    可能性一:SORTTAB1BYIDNAME.正确示例:READTABLETBA1INTOINTOLS_TAB1WITHKEYID='01'NAME='ZS'BINARY......
  • 如何快速开发灵活自定义报表
    大数据时代对管理提出了新的要求,数据就是企业的生产力,所以报表设计的重要性便体现了出来,如何做好报表开发,如何利用报表对数据进行合理利用,许多企业仍然没有摸索到正确的思......
  • 5 号码汇总 |数据科学统计
    5号码汇总|数据科学统计在本博客中,我们将介绍5个数字摘要以及如何使用它来识别异常值,并讨论箱线图。5数字摘要是一组描述性统计数据,用于获取有关数据集的信息,但仅适......
  • MySQL:互联网公司常用分库分表方案汇总!
    一、数据库瓶颈不管是IO瓶颈,还是CPU瓶颈,最终都会导致数据库的活跃连接数增加,进而逼近甚至达到数据库可承载活跃连接数的阈值。在业务Service来看就是,可用数据库连接少甚至......
  • Mac使用经验汇总
    在此记录使用Mac的一些经验技巧。安装brew如果按照官网提示安装,巨慢无比,如下:/bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/......