ALV单元格合并

一、使用WRITE方法

REPORT yalvmultipleheader
NO STANDARD PAGE HEADING.

type-pools: slis.

data :t_fieldcat  type slis_t_fieldcat_alv,
      w_fieldcat  type slis_fieldcat_alv,
      layout      type slis_layout_alv,
      t_sort      type slis_t_sortinfo_alv,
      t_event     type slis_t_event with header line,
      d_repid     like sy-repid,
      d_layout    type slis_layout_alv,
      key         type slis_keyinfo_alv.

types : begin of ty_data,
        store(15),
        book(30),
        author(30),
        amount(7) type p,
        price type dmbtr,
        total type dmbtr, "(10) TYPE p DECIMALS 2,
        curr type waers,
        end of ty_data.

data : t_data type ty_data occurs 0 with header line.


START-OF-SELECTION.
PERFORM f_process_data.
PERFORM f_write_data.

*&---------------------------------------------------------------------*
*&      Form  F_PROCESS_DATA
*&---------------------------------------------------------------------*
form f_process_data .
*fill data t_data
  t_data-store = 'Gramedia'.
  t_data-book = 'Negeri 5 Menara'.
  t_data-author = 'A. Fuadi'.
  t_data-amount = 1000.
  t_data-price = 50000.
  t_data-total = t_data-amount * t_data-price.
  t_data-curr = 'IDR'.
  append t_data.
  t_data-store = 'Eureka'.
  t_data-amount = 1000.
  t_data-price = 50000.
  t_data-total = t_data-amount * t_data-price.
  append t_data.
endform.                    " F_PROCESS_DATA
*&---------------------------------------------------------------------*
*&      Form  F_WRITE_DATA
*&---------------------------------------------------------------------*
form f_write_data .
  perform field_catalog.
  perform build_layout.
  perform build_event.

  d_repid = sy-repid.

    call function 'REUSE_ALV_LIST_DISPLAY'
    exporting
      i_callback_program = d_repid
      is_layout          = d_layout
      i_bypassing_buffer = 'X'
      it_fieldcat        = t_fieldcat
      i_default          = 'X'
      i_save             = 'A'
      it_events          = t_event[]
      it_sort            = t_sort
    tables
      t_outtab           = t_data[]
    exceptions
      program_error      = 1
      others             = 2.
endform.                    " F_WRITE_DATA
*&---------------------------------------------------------------------*
*&      Form  FIELD_CATALOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form field_catalog .
  perform f_fieldcats using :
 'STORE'  'STORE'       '20' ''  ''     ''  ''  '',
 'BOOK'   'BOOK'        '20' ''  ''     ''  ''  '',
 'AUTHOR' 'AUTHOR'      '20' ''  ''     ''  ''  '',
 'AMOUNT' 'AMOUNT'      '10' ''  ''     ''  ''  '',
 'PRICE'  'PRICE'       '15' ''  ''     ''  ''  '',
 'TOTAL'  'TOTAL'       '20' '' ''     ''  ''  ''.
endform.                    " FIELD_CATALOG
*&---------------------------------------------------------------------*
*&      Form  F_FIELDCATS
*&---------------------------------------------------------------------*
form f_fieldcats using fu_fname
                       fu_text
                       fu_len
                       fu_sum
                       fu_curr
                       fu_spot
                       fu_out
                       fu_sign.

  data : lt_fieldcat type slis_fieldcat_alv.
  lt_fieldcat-fieldname      = fu_fname.  "field name
  lt_fieldcat-seltext_l      = lt_fieldcat-seltext_m =  "text
  lt_fieldcat-seltext_s      = fu_text.
  lt_fieldcat-reptext_ddic   = fu_text.
  lt_fieldcat-outputlen      = fu_len.  "text length
  lt_fieldcat-do_sum         = fu_sum.  "summing
  lt_fieldcat-cfieldname     = fu_curr. "currency
  lt_fieldcat-hotspot        = fu_spot.
  lt_fieldcat-no_out         = fu_out.
  lt_fieldcat-no_sign        = fu_sign.

  append lt_fieldcat to t_fieldcat.
  clear: lt_fieldcat.

endform.                    " F_FIELDCATS
*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
form build_layout .
  d_layout-colwidth_optimize = ''.
  d_layout-zebra = 'X'.
  d_layout-no_colhead         = 'X'. "space.

endform.                    " BUILD_LAYOUT
*&---------------------------------------------------------------------*
*&      Form  BUILD_EVENT
*&---------------------------------------------------------------------*
form build_event .
  t_event-form = 'TOP_PAGE'.
  t_event-name = 'TOP_OF_PAGE'.
  append t_event.
endform.                    " BUILD_EVENT

*&---------------------------------------------------------------------*
*&      Form  top_of_page
*&---------------------------------------------------------------------*
form top_page.
  uline (295) .
  format color 1 .
  write: / sy-vline, (18) 'Store' centered,
           sy-vline, (18) 'Book' centered,
           sy-vline, (18) 'Author' centered,
           sy-vline, (8) 'Amount' centered,
           sy-vline, (34) 'Price' centered,
           sy-vline.

  write: / sy-vline, (18) ' ' centered,
         sy-vline, (18)  ' ' centered,
         sy-vline, (18) ' ' centered,
         sy-vline, (8) ' ' centered,
         sy-vline, (13) 'per Unit' centered,
         sy-vline, (18) 'Total' centered,
         sy-vline.
  format COLOR off.
endform.                    "top_of_page

效果:

ALV单元格合并_第1张图片

二、使用 SE80 或事务 SE24 创建类 ZCL_GUI_ALV_GRID_MERGE。然后继承类 CL_GUI_ALV_GRID 并实现以下方法(网址:Zellen verbinden - Tricktresor)

*SE24基于源代码创建类
CLASS zcl_gui_alv_grid_merge DEFINITION
  PUBLIC
  INHERITING FROM cl_gui_alv_grid
  CREATE PUBLIC .

  PUBLIC SECTION.
    METHODS z_set_merge_horiz
      IMPORTING
        row           TYPE i
      CHANGING
        tab_col_merge TYPE lvc_t_co01 .
    METHODS z_set_merge_vert
      IMPORTING
        row           TYPE i
      CHANGING
        tab_col_merge TYPE lvc_t_co01 .
    METHODS z_display .
    METHODS z_set_cell_style
      IMPORTING
        row    TYPE i OPTIONAL
        col    TYPE i OPTIONAL
        style  TYPE lvc_style
        style2 TYPE lvc_style OPTIONAL .
    METHODS z_set_fixed_col_row
      IMPORTING
        col TYPE i
        row TYPE i .
    METHODS z_init_cell_styles .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS ZCL_GUI_ALV_GRID_MERGE IMPLEMENTATION.

  METHOD z_display.

    DATA lv_stable TYPE lvc_s_stbl.
    DATA lv_soft   TYPE c.

    CALL METHOD me->set_data_table
      CHANGING
        data_table = mt_data[].

    CALL METHOD set_auto_redraw
      EXPORTING
        enable = 1.

  ENDMETHOD.

  METHOD z_init_cell_styles.

    FIELD-SYMBOLS  TYPE lvc_s_data.

    LOOP AT mt_data ASSIGNING .
      -style = 0.
    ENDLOOP.

  ENDMETHOD.

  METHOD z_set_cell_style.

    FIELD-SYMBOLS  TYPE lvc_s_data.
    IF row IS INITIAL.
      IF col IS INITIAL.

        EXIT.
      ELSE.

        LOOP AT mt_data ASSIGNING 
              WHERE col_pos = col.
          -style  = -style + style.
          -style2 = -style2 + style2.
        ENDLOOP.
      ENDIF.
    ELSE.
      IF col IS INITIAL.

        LOOP AT mt_data ASSIGNING 
              WHERE row_pos = row.
          -style  = -style + style.
          -style2 = -style2 + style2.
        ENDLOOP.
      ELSE.
        READ TABLE mt_data ASSIGNING 
            WITH KEY row_pos = row
                     col_pos = col.
        IF sy-subrc EQ 0.
          -style  = -style + style.
          -style2 = -style2 + style2.
        ELSE.
          EXIT.
        ENDIF.
      ENDIF.
    ENDIF.

  ENDMETHOD.

  METHOD z_set_fixed_col_row.

    me->set_fixed_cols( col ).
    me->set_fixed_rows( row ).

  ENDMETHOD.

  METHOD z_set_merge_horiz.

    FIELD-SYMBOLS  TYPE lvc_s_co01.
    FIELD-SYMBOLS  TYPE lvc_s_data.
    DATA outputlen TYPE i.

    SORT tab_col_merge.
    LOOP AT tab_col_merge ASSIGNING .

      IF -col_id    LE 0.                CONTINUE. ENDIF.
      IF -outputlen LE -col_id. CONTINUE. ENDIF.
      outputlen = -outputlen - -col_id.
      LOOP AT mt_data ASSIGNING 
           WHERE row_pos = row  AND
                 ( col_pos BETWEEN -col_id AND
                                   -outputlen ).

        IF -col_pos = -col_id.
          -mergehoriz = outputlen.

        ELSE.
          CLEAR -mergehoriz.
          CLEAR -value.
        ENDIF.
      ENDLOOP.

    ENDLOOP.

  ENDMETHOD.

  METHOD z_set_merge_vert.

    FIELD-SYMBOLS  TYPE lvc_s_co01.
    FIELD-SYMBOLS  TYPE lvc_s_data.
    DATA outputlen TYPE i.

    SORT tab_col_merge.

    LOOP AT tab_col_merge ASSIGNING .

      IF -col_id    LE 0.                CONTINUE. ENDIF.
      IF -outputlen LE -col_id. CONTINUE. ENDIF.
      outputlen = -outputlen - -col_id.
      LOOP AT mt_data ASSIGNING 
           WHERE row_pos = row  AND
                 ( col_pos BETWEEN -col_id AND
                                   -outputlen ).

        IF -col_pos = -col_id.
          -mergevert = outputlen.

        ELSE.
          CLEAR -mergevert.
          CLEAR -value.
        ENDIF.
      ENDLOOP.

    ENDLOOP.

  ENDMETHOD.
ENDCLASS.

SE38创建程序创建0200的屏幕并画一个CU_CON控件

*SE38直接复制粘贴
REPORT y4_b4_test16.
INCLUDE .
INCLUDE .
DATA retc TYPE sy-subrc. .
DATA ok_code TYPE sy-ucomm.
DATA it_grp TYPE lvc_t_sgrp.
DATA it_fil TYPE lvc_t_filt.
DATA wa_fil TYPE lvc_s_filt.

* **** ALV_GRID    ****************************************************
TYPES: BEGIN OF t_check_styles,
         field01(20),
         field02(20),
         field03(20),
         field04(20),
         field05(20),
         field06(20),
         field07(20),
         field08(20),
         field09(20),
         field10(20),
         field11(20),
         field12(20).
TYPES END OF t_check_styles.

DATA it_styles TYPE t_check_styles OCCURS 0.
DATA wa_styles TYPE t_check_styles.
FIELD-SYMBOLS  TYPE t_check_styles.
DATA : lt_fieldcatalog TYPE lvc_t_fcat.
DATA : ls_fieldcatalog TYPE lvc_t_fcat.
DATA : wa_cat TYPE lvc_s_fcat.
DATA : fieldname(40).
DATA : fieldnr(2) TYPE n.

FIELD-SYMBOLS  TYPE lvc_s_fcat.

DATA lt_iinfo TYPE lvc_t_info.
DATA wa_iinfo TYPE lvc_s_info.
DATA lt_idata TYPE lvc_t_data.
DATA wa_idata TYPE lvc_s_data.

DATA it_col_merge TYPE lvc_t_co01.
DATA wa_col_merge TYPE lvc_s_co01.
DATA: g_container TYPE scrfname VALUE 'CU_CON'.
DATA: g_custom_container TYPE REF TO cl_gui_custom_container.
DATA g_alv_grid TYPE REF TO zcl_gui_alv_grid_merge.
CLASS cl_gui_cfw DEFINITION LOAD.

DATA: x_save, "for Parameter I_SAVE
      gs_variant TYPE disvariant. "for parameter IS_VARIANT
DATA gs_layout TYPE lvc_s_layo. " Layout
DATA wa_style TYPE lvc_s_styl.

START-OF-SELECTION.
  CALL SCREEN 0200.

END-OF-SELECTION.

MODULE status_0200 OUTPUT.

** Status und Titel setzen
  SET PF-STATUS '0200'.
  SET TITLEBAR '001'.

** Objekte instanzieren und zuordnen: Grid
  IF g_custom_container IS INITIAL.

    CREATE OBJECT g_custom_container
      EXPORTING
        container_name = g_container.

    CREATE OBJECT g_alv_grid
      EXPORTING
        i_parent = g_custom_container.

    gs_layout-stylefname = 'CELL'.
    gs_layout-no_headers = 'X'.
    gs_layout-cwidth_opt = ' '.
    gs_layout-no_toolbar = 'X'.

** Feldkatalog erzeugen
    REFRESH lt_fieldcatalog.

    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
      EXPORTING
        i_internal_tabname     = 'IT_STYLES'
      CHANGING
        ct_fieldcat            = lt_fieldcatalog
      EXCEPTIONS
        inconsistent_interface = 1
        program_error          = 2
        OTHERS                 = 3.

    REFRESH lt_fieldcatalog.
    REFRESH lt_iinfo.

    DO 12 TIMES.
      CLEAR wa_cat.
      fieldnr = sy-index.
      wa_cat-col_pos = sy-index.
      CONCATENATE 'FIELD' fieldnr INTO fieldname.
      wa_cat-fieldname = fieldname.
      wa_cat-tabname   = '1'.
      wa_cat-datatype  = 'CHAR'.
      wa_cat-inttype   = 'C'.
      wa_cat-intlen    = 20.
      IF sy-index > 1.
        wa_cat-outputlen    = 6.
      ELSE.
        wa_cat-outputlen    = 20.
      ENDIF.
      wa_cat-reptext   = fieldname.
      wa_cat-scrtext_l = fieldname.
      wa_cat-scrtext_m = fieldname.
      wa_cat-scrtext_s = fieldname.
      wa_cat-scrtext_l = fieldname.
      APPEND wa_cat TO lt_fieldcatalog.
    ENDDO.

* 1 Zeile
    CLEAR wa_styles.
    wa_styles-field01 = 'TRICKTRESOR'.
    wa_styles-field03 = 'F'.
    wa_styles-field04 = 'P'.
    wa_styles-field09 = 'M'.
    wa_styles-field10 = 'K'.
    APPEND wa_styles TO it_styles.
* 2 Zeile
    CLEAR wa_styles.
    wa_styles-field03 = 'HQ'.
    wa_styles-field04 = 'HC'.
    wa_styles-field08 = 'HW'.
    wa_styles-field09 = 'HC'.
    wa_styles-field10 = 'HC'.
    wa_styles-field12 = 'HW'.
    APPEND wa_styles TO it_styles.
* 3-Zeile
    CLEAR wa_styles.
    wa_styles-field01 = 'Bezeichnung'.
    wa_styles-field02 = 'Radius'.
    wa_styles-field03 = 'WPX 12'.
    wa_styles-field04 = 'WAP 25'.
    wa_styles-field05 = 'WAP 35'.
    wa_styles-field06 = 'WTP 35'.
    wa_styles-field07 = 'WXP 45'.
    wa_styles-field08 = 'WPM'.
    wa_styles-field09 = 'WXM 35'.
    wa_styles-field10 = 'WAK 15'.
    wa_styles-field11 = 'WAK 25'.
    wa_styles-field12 = 'WKM'.
    APPEND wa_styles TO it_styles.

* 4..Zeile
    CLEAR wa_styles.
    wa_styles-field01 = 'SPMW 060304 T - A 27'.
    wa_styles-field02 = '0.54'.
    wa_styles-field03 = icon_led_green.
    wa_styles-field04 = icon_led_yellow.
    wa_styles-field05 = icon_led_red.
    wa_styles-field08 = icon_led_yellow.
    APPEND wa_styles TO it_styles.

    CLEAR wa_styles.
    wa_styles-field01 = 'SPMW 060304 - A 57'.
    wa_styles-field02 = '0.43'.
    wa_styles-field03 = icon_led_yellow.
    wa_styles-field05 = icon_led_red.
    wa_styles-field08 = icon_led_yellow.
    wa_styles-field10 = icon_led_yellow.
    wa_styles-field11 = icon_led_red.
    wa_styles-field12 = icon_led_yellow.
    APPEND wa_styles TO it_styles.

    CLEAR wa_styles.
    wa_styles-field01 = 'SPMW 060304 - D 51'.
    wa_styles-field02 = '0.76'.
    wa_styles-field04 = icon_led_yellow.
    wa_styles-field05 = icon_led_red.
    wa_styles-field06 = icon_led_red.
    wa_styles-field07 = icon_led_red.
    APPEND wa_styles TO it_styles.

    CLEAR wa_styles.
    wa_styles-field01 = 'SPMW 060304 - F 55'.
    wa_styles-field02 = '0.44'.
    wa_styles-field03 = icon_led_red.
    wa_styles-field05 = icon_led_green.
    wa_styles-field06 = icon_led_yellow.
    wa_styles-field07 = icon_led_red.
    wa_styles-field09 = icon_led_yellow.
    wa_styles-field10 = icon_led_green.
    wa_styles-field11 = icon_led_yellow.
    wa_styles-field12 = icon_led_yellow.
    APPEND wa_styles TO it_styles.

    CALL METHOD g_alv_grid->set_table_for_first_display
      EXPORTING
        is_variant      = gs_variant
        i_save          = x_save
        is_layout       = gs_layout
      CHANGING
        it_fieldcatalog = lt_fieldcatalog
        it_outtab       = it_styles.

    REFRESH it_col_merge.

*** DEMO vertikal verbinden
    wa_col_merge-col_id    = 1.
    wa_col_merge-outputlen = 2.
    APPEND wa_col_merge TO it_col_merge.

    "打竖合并
    CALL METHOD g_alv_grid->z_set_merge_vert
      EXPORTING
        row           = 1 "第一行
      CHANGING
        tab_col_merge = it_col_merge.
    "设置格式
    wa_style-style     = alv_style_font_bold"粗体
                       + alv_style_align_center_center"居中
                       + alv_style_color_key."颜色

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        row   = 1
        col   = 1
        style = wa_style-style.

*** VERTIKAL verbinden
    CLEAR it_col_merge.

    wa_col_merge-col_id    = 4.
    wa_col_merge-outputlen = 8.
    APPEND wa_col_merge TO it_col_merge.

    wa_col_merge-col_id    = 10.
    wa_col_merge-outputlen = 12.
    APPEND wa_col_merge TO it_col_merge.

    "打横合并
    CALL METHOD g_alv_grid->z_set_merge_horiz
      EXPORTING
        row           = 1
      CHANGING
        tab_col_merge = it_col_merge.

    wa_style-style     = alv_style_font_bold.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        row   = 1
        col   = 3
        style = wa_style-style.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        row   = 1
        col   = 4
        style = wa_style-style.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        row   = 1
        col   = 9
        style = wa_style-style.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        row   = 1
        col   = 10
        style = wa_style-style.

    REFRESH it_col_merge.

    wa_col_merge-col_id    = 4.
    wa_col_merge-outputlen = 7.
    APPEND wa_col_merge TO it_col_merge.

    CALL METHOD g_alv_grid->z_set_merge_horiz
      EXPORTING
        row           = 2
      CHANGING
        tab_col_merge = it_col_merge.

    wa_style-style     = alv_style_color_group +
                         alv_style_align_center_center.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 3
        style = wa_style-style.

    wa_style-style     = alv_style_color_heading +
                         alv_style_align_center_center.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 4
        style = wa_style-style.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 5
        style = wa_style-style.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 6
        style = wa_style-style.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 7
        style = wa_style-style.
    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 8
        style = wa_style-style.

    wa_style-style     = alv_style_color_total +
                         alv_style_align_center_center.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 9
        style = wa_style-style.

    wa_style-style     = alv_style_color_negative +
                         alv_style_align_center_center.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 10
        style = wa_style-style.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 11
        style = wa_style-style.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 12
        style = wa_style-style.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 13
        style = wa_style-style.

    wa_style-style     = alv_style_color_positive +
                         alv_style_align_center_center.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 14
        style = wa_style-style.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 15
        style = wa_style-style.

    wa_style-style     = alv_style_color_int_background +
                         alv_style_align_center_center.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        col   = 16
        style = wa_style-style.

    wa_style-style     = alv_style_color_positive +
                         alv_style_align_center_center +
                         alv_style_font_italic.

    CALL METHOD g_alv_grid->z_set_cell_style
      EXPORTING
        row   = 4
        col   = 2
        style = wa_style-style.

    g_alv_grid->z_set_fixed_col_row(
            EXPORTING col = 3
                      row = 3 ).

    g_alv_grid->z_display( ).

  ENDIF.
ENDMODULE. "status_0100 OUTPUT

MODULE user_command_0200 INPUT.

  cl_gui_cfw=>dispatch( ).

  CASE ok_code.
    WHEN '&F03' OR '&F12' OR '&F15'.
      SET SCREEN 0. LEAVE SCREEN.
  ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

你可能感兴趣的:(#,ALV,java,前端,开发语言)