【OO ALV实现合并行和列】

需求场景

做了一个FUNCTION ALV,按行展示,但是用户需要做一个转置,前面列按行显示,后面数量列由按列显示调整为按行显示。查了一下,好像是FUNCTION ALV实现不了,参考了一位博主实现了这个需求。
ABAP ALV多行表头(合并列,合并单元格)

问题点

这里面实现按行显示的代码有问题,我这里做了一些调整,代码如下:

FORM set_merge_vert USING col row len value.
  CLEAR: wa_col_merge,it_col_merge.
  wa_col_merge-col_id    = row.
  wa_col_merge-outputlen = len.
  APPEND wa_col_merge TO it_col_merge.

  CALL METHOD g_alv_grid->z_set_merge_vert
    EXPORTING
      col           = col
      value         = value
    CHANGING
      tab_col_merge = it_col_merge.

  "设置Style
  CLEAR:wa_style-style.
  wa_style-style     = alv_style_font_bold_no
  + alv_style_align_center_center
  + alv_style_color_key.

  CALL METHOD g_alv_grid->z_set_cell_style
    EXPORTING
      row   = row
      col   = col
      style = wa_style-style.
ENDFORM.                    " SET_MERGE_VERT

  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 col_pos = col  AND
      ( row_pos BETWEEN -col_id AND
      -outputlen ).

        IF -row_pos = -col_id.
          -mergevert = outputlen.
          -value = value.
        ELSE.
          CLEAR -mergevert.
          CLEAR -value.
        ENDIF.
      ENDLOOP.
    ENDLOOP.
  ENDMETHOD.                    "Z_SET_MERGE_VERT

效果如下

【OO ALV实现合并行和列】_第1张图片

你可能感兴趣的:(ABAP,合并单元格,合并行,合并列)