ABAP - SALV实战 采购订单批量查询报表

本文详细介绍了如何使用ABAP编程语言进行报表开发,区分了查询报表与功能报表,并展示了如何通过SQL查询数据,使用CL_salv_table和CL_salv_layout等类来处理数据、设置列、总计等功能,以展示在ALV表单中。
摘要由CSDN通过智能技术生成
  • 报表开发一般分查询报表和功能报表
  • 查询报表相对于功能报表工作量和难度都略小一些
  • 该报表的特色有表头窗体,还有底部合计
  • 实现效果如下图所示
  • 完整代码,可直接粘贴复制使用 
    TABLES:ekko,ekpo,ekbe.
    
    TYPES:BEGIN OF ty_data,
            ebeln     TYPE ekko-ebeln,
            ebelp     TYPE ekpo-ebelp,
            loekz     TYPE ekpo-loekz,
            matnr     TYPE ekpo-matnr,
            maktx     TYPE makt-maktx,
            menge_cg  TYPE ekpo-menge,
            lifnr     TYPE ekko-lifnr,
            name1     TYPE lfa1-name1,
            aedat     TYPE ekko-aedat,
            eindt     TYPE eket-eindt,
            budat     TYPE ekbe-budat,
            belnr     TYPE ekbe-belnr,
            menge_sh  TYPE ekbe-menge,
            wrbtr     TYPE ekbe-wrbtr,
            bwart     TYPE ekbe-bwart,
            shkzg     TYPE ekbe-shkzg,
            vgabe     TYPE ekbe-vgabe,
          END OF ty_data.
    
    DATA lv_werks TYPE string.
    
    CLASS cl_model DEFINITION.
      PUBLIC SECTION.
        DATA t_data TYPE TABLE OF ty_data.
        DATA o_alv TYPE REF TO cl_salv_table.
        METHODS:
          get_data.
        METHODS:
          show_alv.
      PRIVATE SECTION.
        METHODS:
          set_status CHANGING co_alv TYPE REF TO cl_salv_table.
        METHODS:
          set_layout CHANGING co_alv TYPE REF TO cl_salv_table.
        METHODS:
          set_column CHANGING co_alv TYPE REF TO cl_salv_table.
        METHODS:
          set_topage CHANGING co_alv TYPE REF TO cl_salv_table.
        METHODS:
          set_aggsum CHANGING co_alv TYPE REF TO cl_salv_table.
    
    ENDCLASS.
    
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
    SELECT-OPTIONS: s_ebeln FOR ekko-ebeln,
                    s_matnr FOR ekpo-matnr,
                    s_werks FOR ekpo-werks,
                    s_lifnr FOR ekko-lifnr,
                    s_bsart FOR ekko-bsart,
                    s_bwart FOR ekbe-bwart,
                    s_aedat FOR ekko-aedat.
    SELECTION-SCREEN END OF BLOCK b1.
    
    START-OF-SELECTION.
    * 实例化
      DATA(lo_model) = NEW cl_model( ).
    * 调用实例的获取数据方法
      lo_model->get_data( ).
      IF lo_model->t_data[] IS NOT INITIAL.
    * 调用实例的ALV展示方法
        lo_model->show_alv( ).
      ELSE.
        MESSAGE '您输入的业务数据获取不到数据' TYPE 'S' DISPLAY LIKE 'E'.
      ENDIF.
    
    CLASS cl_model IMPLEMENTATION.
      METHOD get_data.
    
        SELECT ekpo~ebeln
               ekpo~ebelp
               ekpo~loekz
               ekpo~matnr
               ekpo~menge AS menge_cg
               ekbe~vgabe
               ekbe~budat
               ekbe~belnr
               ekbe~wrbtr
               ekbe~bwart
               ekbe~shkzg
               ekbe~menge AS menge_sh
               ekko~ebeln
               ekko~lifnr
               ekko~aedat
          INTO CORRESPONDING FIELDS OF TABLE t_data
          FROM ekko
         INNER JOIN ekpo  ON ekpo~ebeln = ekko~ebeln
          LEFT JOIN ekbe  ON ekpo~ebeln = ekbe~ebeln
                         AND ekpo~ebelp = ekbe~ebelp
         WHERE ekpo~werks IN s_werks
           AND ekpo~ebeln IN s_ebeln
           AND ekpo~matnr IN s_matnr
           AND ekko~lifnr IN s_lifnr
           AND ekko~bsart IN s_bsart
           AND ekko~aedat IN s_aedat.
    
        CHECK t_data[] IS NOT INITIAL.
    
        SELECT ebeln,ebelp,eindt
          FROM eket FOR ALL ENTRIES IN @t_data
         WHERE ebeln = @t_data-ebeln
           AND ebelp = @t_data-ebelp
          INTO TABLE @DATA(lt_eket).
    
        SELECT matnr,maktx
          FROM makt FOR ALL ENTRIES IN @t_data
         WHERE matnr = @t_data-matnr
           AND spras = @sy-langu
          INTO TABLE @DATA(lt_makt).
    
        SELECT lifnr,name1
          FROM lfa1 FOR ALL ENTRIES IN @t_data
         WHERE lifnr = @t_data-lifnr
          INTO TABLE @DATA(lt_lfa1).
    
        SORT   lt_eket BY ebeln ebelp.
        SORT   lt_makt BY matnr.
        SORT   lt_lfa1 BY lifnr.
        SORT   t_data  BY ebeln ebelp.
        LOOP AT t_data ASSIGNING FIELD-SYMBOL(<fs_data>).
          READ TABLE lt_makt INTO DATA(lw_matk) WITH KEY matnr = <fs_data>-matnr BINARY SEARCH.
          IF sy-subrc = 0.
            <fs_data>-maktx = lw_matk-maktx.  "物料描述
          ENDIF.
    
          READ TABLE lt_lfa1 INTO DATA(lw_lfa1) WITH KEY lifnr = <fs_data>-lifnr BINARY SEARCH.
          IF sy-subrc = 0.
            <fs_data>-name1 = lw_lfa1-name1.  "供应商名称
          ENDIF.
    
          READ TABLE lt_eket INTO DATA(lw_eket) WITH KEY ebeln = <fs_data>-ebeln
                                                         ebelp = <fs_data>-ebelp BINARY SEARCH.
          IF sy-subrc = 0.
            <fs_data>-eindt = lw_eket-eindt.  "计划交货日期
          ENDIF.
    
          IF <fs_data>-shkzg = 'H'.
            <fs_data>-menge_sh = 0 - <fs_data>-menge_sh.   "收货数量
            <fs_data>-wrbtr = 0 - <fs_data>-wrbtr.         "收货金额
          ENDIF.
        ENDLOOP.
    
      ENDMETHOD.
    
      METHOD show_alv.
        DATA: lx_msg TYPE REF TO cx_salv_msg.
        TRY.
            cl_salv_table=>factory(
              IMPORTING
                r_salv_table = o_alv
              CHANGING
                t_table      = t_data ).
          CATCH cx_salv_msg INTO lx_msg.
        ENDTRY.
    * 调用status设置方法
        me->set_status( CHANGING co_alv = o_alv ).
    * 调用layout设置方法
        me->set_layout( CHANGING co_alv = o_alv ).
    * 调用column设置方法
        me->set_column( CHANGING co_alv = o_alv ).
    * 调用topage设置方法
        me->set_topage( CHANGING co_alv = o_alv ).
    * 调用aggsum设置方法
        me->set_aggsum( CHANGING co_alv = o_alv ).
    * 调用display展示ALV
        o_alv->display( ).
      ENDMETHOD.
    
      METHOD set_status.
        DATA: lo_functions TYPE REF TO cl_salv_functions_list.
    *   获取状态对象
        lo_functions = co_alv->get_functions( ).
    *   设置状态
        lo_functions->set_all( abap_true ).
      ENDMETHOD.
    
      METHOD set_layout.
        DATA: lo_display TYPE REF TO cl_salv_display_settings.
    *   取得Layout对象
        lo_display = co_alv->get_display_settings( ).
    *   设置Zebra显示
        lo_display->set_striped_pattern( 'X' ).
    *   设置Title
        lo_display->set_list_header( '采购订单批量查询报表' ).
      ENDMETHOD.
    
      METHOD set_column.
    *   声明接收全体列对象的变量
        DATA: lo_cols TYPE REF TO cl_salv_columns.
    *   获取全部列的对象
        lo_cols = co_alv->get_columns( ).
    *   设置自动优化列宽度
        lo_cols->set_optimize( 'X' ).
        TRY.
    *   设置列
            lo_cols->get_column( 'MENGE_SH' )->set_short_text( '收货数量' ).
            lo_cols->get_column( 'WRBTR' )->set_short_text( '收货金额' ).
    *   隐藏列
            lo_cols->get_column( 'VGABE' )->set_visible( value = if_salv_c_bool_sap=>false ).
            lo_cols->get_column( 'SHKZG' )->set_visible( value = if_salv_c_bool_sap=>false ).
          CATCH cx_salv_not_found.
        ENDTRY.
      ENDMETHOD.
    
      METHOD set_topage.
        DATA: lo_header  TYPE REF TO cl_salv_form_layout_grid,
              lo_h_label TYPE REF TO cl_salv_form_label,
              lo_h_flow  TYPE REF TO cl_salv_form_layout_flow.
    *   获取窗口对象
        CREATE OBJECT lo_header.
    *   通过Row 和 column字段定位在top of page的文本输出位置
    *   第一行
        lo_h_label = lo_header->create_label( row = 1  column = 1 ).
        lo_h_label->set_text( value = '当前日期:' && sy-datum ).
    *   第二行
        lo_h_label = lo_header->create_label( row = 2 column = 1 ).
        lo_h_label->set_text( value = '收货工厂:' && lv_werks ).
    *   第三行
        DATA(lv_num) = lines( me->t_data ).
        lo_h_label = lo_header->create_label( row = 3  column = 1 ).
        lo_h_label->set_text( value = '总记录数:' && lv_num ).
    *   页眉显示
        co_alv->set_top_of_list( lo_header ).
    *   页眉打印
        co_alv->set_top_of_list_print( lo_header ).
      ENDMETHOD.
    
      METHOD set_aggsum.
        DATA: lo_aggrs TYPE REF TO cl_salv_aggregations.
        lo_aggrs = co_alv->get_aggregations( ).
    *   给列添加合计
        TRY.
            CALL METHOD lo_aggrs->add_aggregation
              EXPORTING
                columnname  = 'MENGE_SH'
                aggregation = if_salv_c_aggregation=>total.
            CALL METHOD lo_aggrs->add_aggregation
              EXPORTING
                columnname  = 'WRBTR'
                aggregation = if_salv_c_aggregation=>total.
          CATCH cx_salv_data_error .                        "#EC NO_HANDLER
          CATCH cx_salv_not_found .                         "#EC NO_HANDLER
          CATCH cx_salv_existing .                          "#EC NO_HANDLER
        ENDTRY.
    *   将合计放置到SALV的顶端
    *    lo_aggrs->set_aggregation_after_items( ).
        lo_aggrs->set_aggregation_allowed( 'MENGE_SH' ).
    
      ENDMETHOD.
    ENDCLASS.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值