程序代码
REPORT z437_test_2025.
DATA gt_spfli TYPE STANDARD TABLE OF spfli. " 内表
DATA go_alv TYPE REF TO cl_gui_alv_grid. " 创建和管理ALV表格
DATA gs_layout TYPE lvc_s_layo. " 存储ALV表格的布局信息
DATA gs_sort TYPE lvc_s_sort.
DATA gt_sort TYPE lvc_t_sort. " 内表
DATA gs_fieldcat TYPE lvc_s_fcat.
DATA gt_fieldcat TYPE lvc_t_fcat. " 内表
*----------------------------------------------------------------------*
* DESC: INITIALIZATION 事件
*----------------------------------------------------------------------*
INITIALIZATION.
sy-title = 'OO ALV 基础显示'. " 设置报表标题
*----------------------------------------------------------------------*
* DESC: AT SELECTION-SCREEN OUTPUT 事件
*----------------------------------------------------------------------*
START-OF-SELECTION.
SELECT * FROM spfli
INTO TABLE gt_spfli
UP TO 30 ROWS.
CREATE OBJECT go_alv
EXPORTING
i_parent = cl_gui_container=>default_screen. " 默认屏幕
CLEAR gs_layout.
gs_layout-zebra = 'X'. " 斑马线
gs_layout-cwidth_opt = 'X'. " 自适应列宽
gs_layout-sel_mode = 'D'. " 表格选择模式被设置为'D',表示多选模式 <---
" 添加排序
CLEAR gs_sort.
gs_sort-spos = '1'.
gs_sort-fieldname = 'CARRID'. " 指定排序字段
gs_sort-subtot = 'X'. " 对排序字段进行小计
gs_sort-up = ''.
gs_sort-down = 'X'. " 指定按降序排序
APPEND gs_sort TO gt_sort.
" 调用函数,自动生成了字段目录
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SPFLI'
CHANGING
ct_fieldcat = gt_fieldcat.
" 在ALV报表对'DISTANCE'字段进行求和操作
FIELD-SYMBOLS <fcat> LIKE gs_fieldcat.
LOOP AT gt_fieldcat ASSIGNING <fcat>.
CASE <fcat>-fieldname.
WHEN 'DISTANCE'.
<fcat>-do_sum = 'X'.
WHEN OTHERS.
ENDCASE.
ENDLOOP.
" 将内表gt_spfli中的数据显示在ALV表格中
CALL METHOD go_alv->set_table_for_first_display
EXPORTING
is_layout = gs_layout
i_structure_name = 'SPFLI'
CHANGING
it_outtab = gt_spfli " 内表
it_fieldcatalog = gt_fieldcat " 内表
it_sort = gt_sort. " 内表
cl_abap_list_layout=>suppress_toolbar( ). " 隐藏标准的工具栏
WRITE space.
呈现结果: