如图,需要在内表中插入一些小计、总计等。
*&---------------------------------------------------------------------*
*& Report z_barry_sum *
*& *
*&---------------------------------------------------------------------*
REPORT z_barry_sum .
TABLES:hrpy_rgdir,t001,pa0001 .
TYPE-POOLS: slis.
DATA:BEGIN OF itab OCCURS 0,
sachp(20), "部门
pernr(20), "人员编号
name(20), "姓名
month(4), "月份
bet01 LIKE pa0008-bet01, "岗位工资
bet02 LIKE pa0008-bet01, "岗位津贴
bet03 LIKE pa0008-bet01, "灰色收入
END OF itab .
DATA: BEGIN OF itab2 OCCURS 0 ,
sachp(10), "部门
pernr(10), "人员编号
name(10), "姓名
month(4), "月份
trfgr(20), "工资等级
bet01(20), "岗位工资
bet02(20), "岗位津贴
bet03(20), "灰色收入
color(4) ,
END OF itab2.
*****************
DATA : myrepid LIKE sy-repid. "REPORT NAME
DATA : allfields TYPE slis_t_fieldcat_alv.
DATA : wa_allfields TYPE LINE OF slis_t_fieldcat_alv.
DATA : layout TYPE slis_layout_alv .
DATA : ls_event TYPE slis_alv_event.
DATA : gt_events TYPE slis_t_event.
*****************
CONSTANTS: gc_grid_title(30) TYPE c VALUE '工资个人明细清单'.
START-OF-SELECTION.
PERFORM getdata.
PERFORM fixdata.
*&---------------------------------------------------------------------*
*& Form getdata
*&---------------------------------------------------------------------*
FORM getdata.
itab-sachp = 'A部门'.
itab-pernr = '111111'.
itab-name = '张三'.
itab-month = '11'.
itab-bet01 = '100'.
itab-bet02 = '120'.
itab-bet03 = '140'.
APPEND itab.
itab-pernr = '222222'.
itab-name = '李四'.
itab-bet01 = '200'.
itab-bet02 = '210'.
itab-bet03 = '140'.
APPEND itab.
itab-sachp = 'B部门'.
itab-pernr = '333333'.
itab-name = '王麻子'.
itab-bet01 = '160'.
itab-bet02 = '120'.
itab-bet03 = '140'.
APPEND itab.
itab-pernr = '444444'.
itab-name = '狗剩'.
itab-bet01 = '500'.
itab-bet02 = '120'.
itab-bet03 = '260'.
APPEND itab.
itab-sachp = 'C部门'.
itab-pernr = '555555'.
itab-name = '铁蛋'.
itab-bet01 = '400'.
itab-bet02 = '220'.
itab-bet03 = '140'.
APPEND itab.
ENDFORM. "getdata
*&---------------------------------------------------------------------*
*& Form fixdata
*&---------------------------------------------------------------------*
FORM fixdata.
SORT itab BY sachp pernr .
LOOP AT itab .
PERFORM fill .
MOVE-CORRESPONDING itab TO itab2 .
APPEND itab2.
CLEAR itab2 .
itab2-sachp = ' '.
APPEND itab2 .
AT END OF sachp.
SUM.
PERFORM fill .
MOVE-CORRESPONDING itab TO itab2.
itab2-color = 'C300'.
CONCATENATE itab-sachp '合计' INTO itab2-sachp .
APPEND itab2 .
CLEAR itab2 .
itab2-sachp = ' '.
APPEND itab2 .
ENDAT.
AT LAST .
SUM.
PERFORM fill .
MOVE-CORRESPONDING itab TO itab2.
itab2-color = 'C310'.
itab2-sachp = '总计' .
APPEND itab2 .
ENDAT.
ENDLOOP .
CLEAR wa_allfields.
PERFORM build_layout USING layout.
PERFORM display_data .
ENDFORM. "fixdata
*&---------------------------------------------------------------------*
*& Form display_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_data .
wa_allfields-fieldname = 'SACHP'.
wa_allfields-seltext_l = '部门'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
wa_allfields-fieldname = 'PERNR'.
wa_allfields-seltext_l = '人员编号'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
wa_allfields-fieldname = 'NAME'.
wa_allfields-seltext_l = '姓名'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
wa_allfields-fieldname = 'MONTH'.
wa_allfields-seltext_l = '月份'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
**
wa_allfields-fieldname = 'BET01'.
wa_allfields-seltext_l = '岗位工资'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
**
wa_allfields-fieldname = 'BET02'.
wa_allfields-seltext_l = '岗位津贴'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
wa_allfields-fieldname = 'BET03'.
wa_allfields-seltext_l = '灰色收入'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
myrepid = sy-repid. "REPROT NAME
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = myrepid
i_grid_title = '月工资个人明细清单' "TITLE
is_layout = layout
it_events = gt_events
i_save = 'X'
it_fieldcat = allfields
TABLES
t_outtab = itab2
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM . "display_data
*&---------------------------------------------------------------------*
*& Form build_layout
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LAYOUT text
*----------------------------------------------------------------------*
FORM build_layout USING p_layout TYPE slis_layout_alv.
* p_layout-zebra = 'X'.
p_layout-colwidth_optimize = 'X'.
p_layout-info_fieldname = 'COLOR'.
ENDFORM. " BUILD_LAYOUT
*&---------------------------------------------------------------------*
*& Form FILL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM fill .
itab2-sachp = '部门'.
itab2-pernr = '人员编号'.
itab2-name = '姓名'.
itab2-month = '月份'.
itab2-bet01 = '岗位工资'.
itab2-bet02 = '岗位津贴'.
itab2-bet03 = '灰色收入'.
itab2-color = 'C100'.
APPEND itab2 .
CLEAR itab2 .
ENDFORM . "FILL
效果如下:
如果有更多的列需要分类小计,如下图:
则可以再加一个AT语句,下面为更改后的程序:
REPORT z_barry_sum .
TABLES:hrpy_rgdir,t001,pa0001 .
TYPE-POOLS: slis.
DATA:BEGIN OF itab OCCURS 0,
bukrs(10), "公司
sachp(20), "部门
pernr(20), "人员编号
name(20), "姓名
month(4), "月份
bet01 LIKE pa0008-bet01, "岗位工资
bet02 LIKE pa0008-bet01, "岗位津贴
bet03 LIKE pa0008-bet01, "灰色收入
END OF itab .
DATA: BEGIN OF itab2 OCCURS 0 ,
bukrs(16), "公司
sachp(10), "部门
pernr(10), "人员编号
name(10), "姓名
month(4), "月份
trfgr(20), "工资等级
bet01(20), "岗位工资
bet02(20), "岗位津贴
bet03(20), "灰色收入
color(4) ,
END OF itab2.
*****************
DATA : myrepid LIKE sy-repid. "REPORT NAME
DATA : allfields TYPE slis_t_fieldcat_alv.
DATA : wa_allfields TYPE LINE OF slis_t_fieldcat_alv.
DATA : layout TYPE slis_layout_alv .
DATA : ls_event TYPE slis_alv_event.
DATA : gt_events TYPE slis_t_event.
*****************
CONSTANTS: gc_grid_title(30) TYPE c VALUE '工资个人明细清单'.
START-OF-SELECTION.
PERFORM getdata.
PERFORM fixdata.
*&---------------------------------------------------------------------*
*& Form getdata
*&---------------------------------------------------------------------*
FORM getdata.
itab-bukrs = '忽悠公司'.
itab-sachp = 'A部门'.
itab-pernr = '111111'.
itab-name = '张三'.
itab-month = '11'.
itab-bet01 = '100'.
itab-bet02 = '120'.
itab-bet03 = '140'.
APPEND itab.
itab-bukrs = '炒作公司'.
itab-pernr = '222222'.
itab-name = '李四'.
itab-bet01 = '200'.
itab-bet02 = '210'.
itab-bet03 = '140'.
APPEND itab.
itab-bukrs = '忽悠公司'.
itab-sachp = 'B部门'.
itab-pernr = '333333'.
itab-name = '王麻子'.
itab-bet01 = '160'.
itab-bet02 = '120'.
itab-bet03 = '140'.
APPEND itab.
itab-pernr = '444444'.
itab-name = '狗剩'.
itab-bet01 = '500'.
itab-bet02 = '120'.
itab-bet03 = '260'.
APPEND itab.
itab-sachp = 'C部门'.
itab-pernr = '555555'.
itab-name = '铁蛋'.
itab-bet01 = '400'.
itab-bet02 = '220'.
itab-bet03 = '140'.
APPEND itab.
ENDFORM. "getdata
*&---------------------------------------------------------------------*
*& Form fixdata
*&---------------------------------------------------------------------*
FORM fixdata.
SORT itab BY bukrs sachp pernr .
LOOP AT itab .
PERFORM fill .
MOVE-CORRESPONDING itab TO itab2 .
APPEND itab2.
CLEAR itab2 .
* APPEND itab2 .
AT END OF sachp.
SUM.
PERFORM fill .
MOVE-CORRESPONDING itab TO itab2.
itab2-color = 'C300'.
CONCATENATE itab-sachp '合计' INTO itab2-sachp .
APPEND itab2 .
CLEAR itab2 .
* APPEND itab2 .
ENDAT.
AT END OF bukrs.
SUM.
PERFORM fill .
MOVE-CORRESPONDING itab TO itab2.
itab2-color = 'C310'.
CONCATENATE itab-bukrs '小计' INTO itab2-bukrs .
APPEND itab2 .
CLEAR itab2 .
* APPEND itab2 .
ENDAT.
AT LAST .
SUM.
PERFORM fill .
MOVE-CORRESPONDING itab TO itab2.
itab2-color = 'C710'.
itab2-bukrs = '总计' .
APPEND itab2 .
ENDAT.
ENDLOOP .
CLEAR wa_allfields.
PERFORM build_layout USING layout.
PERFORM display_data .
ENDFORM. "fixdata
*&---------------------------------------------------------------------*
*& Form display_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_data .
wa_allfields-fieldname = 'BUKRS'.
wa_allfields-seltext_l = '公司'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
wa_allfields-fieldname = 'SACHP'.
wa_allfields-seltext_l = '部门'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
wa_allfields-fieldname = 'PERNR'.
wa_allfields-seltext_l = '人员编号'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
wa_allfields-fieldname = 'NAME'.
wa_allfields-seltext_l = '姓名'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
wa_allfields-fieldname = 'MONTH'.
wa_allfields-seltext_l = '月份'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
**
wa_allfields-fieldname = 'BET01'.
wa_allfields-seltext_l = '岗位工资'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
**
wa_allfields-fieldname = 'BET02'.
wa_allfields-seltext_l = '岗位津贴'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
wa_allfields-fieldname = 'BET03'.
wa_allfields-seltext_l = '灰色收入'.
APPEND wa_allfields TO allfields.
CLEAR wa_allfields.
myrepid = sy-repid. "REPROT NAME
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = myrepid
i_grid_title = '月工资个人明细清单' "TITLE
is_layout = layout
it_events = gt_events
i_save = 'X'
it_fieldcat = allfields
TABLES
t_outtab = itab2
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM . "display_data
*&---------------------------------------------------------------------*
*& Form build_layout
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LAYOUT text
*----------------------------------------------------------------------*
FORM build_layout USING p_layout TYPE slis_layout_alv.
* p_layout-zebra = 'X'.
p_layout-colwidth_optimize = 'X'.
p_layout-info_fieldname = 'COLOR'.
ENDFORM. " BUILD_LAYOUT
*&---------------------------------------------------------------------*
*& Form FILL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM fill .
* itab2-bukrs = '公司'.
* itab2-sachp = '部门'.
* itab2-pernr = '人员编号'.
* itab2-name = '姓名'.
* itab2-month = '月份'.
* itab2-bet01 = '岗位工资'.
* itab2-bet02 = '岗位津贴'.
* itab2-bet03 = '灰色收入'.
* itab2-color = 'C100'.
* APPEND itab2 .
* CLEAR itab2 .
ENDFORM . "FILL