该方法就建一个结构,没有将表头和行项目数据分开,当内表的交货单号变化时就换页。
报表代码如下:
*&---------------------------------------------------------------------*
*& Report ZSDR006
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsdr006.
TYPE-POOLS:slis.
TABLES:vbak, vbap, lips, likp, vbkd.
DATA: itab TYPE TABLE OF zsdr006 WITH HEADER LINE.
DATA: itab1 TYPE TABLE OF zsdr006 WITH HEADER LINE.
DATA: it_out TYPE TABLE OF zsdr006 WITH HEADER LINE.
DATA: it_temp TYPE TABLE OF zsdr006 WITH HEADER LINE.
DATA: c_form_name TYPE tdsfname ,
c_form_title TYPE string.
DATA: gv_title TYPE lvc_title.
DATA: gs_layout TYPE slis_layout_alv,
gt_fields TYPE slis_t_fieldcat_alv,
gs_fields TYPE LINE OF slis_t_fieldcat_alv.
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE text-001.
SELECT-OPTIONS: s_vkorg FOR vbak-vkorg,
s_kunnr FOR vbak-kunnr,
s_vbeln FOR vbap-vbeln.
PARAMETERS: p1 RADIOBUTTON GROUP g1 DEFAULT 'X',
p2 RADIOBUTTON GROUP g1,
p3 RADIOBUTTON GROUP g1,
p4 RADIOBUTTON GROUP g1.
SELECTION-SCREEN END OF BLOCK block1.
START-OF-SELECTION.
PERFORM getdata.
PERFORM alvdata.
*&---------------------------------------------------------------------*
*& Form getdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM getdata.
SELECT
a~vbeln
a~vkorg
a~kunnr
a~wadat_ist
a~bldat
a~trmtyp
b~posnr
b~matnr
b~werks
b~lfimg
b~meins
b~kdmat
b~ntgew
b~brgew
b~vgbel
b~vgpos
INTO CORRESPONDING FIELDS OF TABLE itab FROM likp AS a INNER JOIN lips AS b ON a~vbeln = b~vbeln WHERE a~vkorg IN s_vkorg AND a~kunnr IN s_kunnr AND b~vbeln IN s_vbeln.
LOOP AT itab.
DATA: a(5) TYPE c.
SELECT SINGLE bstkd INTO itab-bstkd FROM vbkd WHERE vbeln = itab-vgbel AND posnr = itab-vgpos.
SELECT SINGLE maktx INTO itab-maktx FROM makt WHERE matnr = itab-matnr AND spras = 1.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = itab-posnr
IMPORTING
output = itab-posnr.
MODIFY itab.
ENDLOOP.
ENDFORM. "getdata
*&---------------------------------------------------------------------*
*& Form alvdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM alvdata.
gs_layout-colwidth_optimize = 'X'.
gs_layout-box_fieldname = 'CHE'.
gs_layout-zebra = 'X'.
REFRESH gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'VKORG'.
gs_fields-seltext_l = '销售组织'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'VBELN'.
gs_fields-seltext_l = '交货单号'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'POSNR'.
gs_fields-seltext_l = '行项目号'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'WADAT_IST'.
gs_fields-seltext_l = '发货日期'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'KUNNR'.
gs_fields-seltext_l = '客户编号'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'MATNR'.
gs_fields-seltext_l = '物料编号'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'MAKTX'.
gs_fields-seltext_l = '物料描述'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'LFIMG'.
gs_fields-seltext_l = '数量'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'BSTKD'.
gs_fields-seltext_l = '客户采购订单号'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'VGBEL'.
gs_fields-seltext_l = '销售订单号'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
gs_fields-fieldname = 'TRMTYP'.
gs_fields-seltext_l = '运输方式'.
APPEND gs_fields TO gt_fields.
CLEAR gs_fields.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_PF'
i_callback_user_command = 'USER_COM'
i_grid_title = gv_title
is_layout = gs_layout
it_fieldcat = gt_fields
i_save = 'X'
TABLES
t_outtab = itab
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. "alvdata
*&---------------------------------------------------------------------*
*& Form set_pf
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->EXTAB text
*----------------------------------------------------------------------*
FORM set_pf USING extab TYPE slis_t_extab.
SET PF-STATUS 'ZSDR006'.
ENDFORM. "set_pf
*&---------------------------------------------------------------------*
*& Form user_com
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->R_UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
FORM user_com USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA: lr_grid TYPE REF TO cl_gui_alv_grid,
myindex TYPE sy-tabix.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lr_grid.
CALL METHOD lr_grid->check_changed_data.
rs_selfield-refresh = 'X'.
CASE r_ucomm.
WHEN '&PRINT'.
DATA:i TYPE i.
i = 0.
LOOP AT itab WHERE che EQ 'X'.
i = i + 1.
ENDLOOP.
IF i = 0.
MESSAGE:'请选择需要打印的列' TYPE 'E'.
ELSE.
LOOP AT itab WHERE che EQ 'X'.
itab1 = itab.
APPEND itab1.
ENDLOOP.
ENDIF.
PERFORM printdata.
ENDCASE.
ENDFORM. "user_com
*&---------------------------------------------------------------------*
*& Form printdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM printdata.
DATA: f_index LIKE sy-tabix.
DATA: lines_len TYPE i.
DATA: ls_control_parameters TYPE ssfctrlop.
DATA: ls_output_options TYPE ssfcompop.
DATA: l_smf_name TYPE rs38l_fnam.
SORT itab1 BY vbeln posnr.
IF NOT p1 IS INITIAL.
c_form_name = 'ZSDSF006A'.
ENDIF.
IF NOT p2 IS INITIAL.
c_form_name = 'ZSDSF006B'.
ENDIF.
IF NOT p3 IS INITIAL.
c_form_name = 'ZSDSF006C'.
ENDIF.
ls_output_options-tdimmed = 'X'.
ls_output_options-tdcopies = '1'.
ls_output_options-tdnoprint = ''.
ls_output_options-tddelete = 'X'.
ls_control_parameters-no_dialog = ''.
ls_control_parameters-preview = 'X'.
ls_control_parameters-langu = '1'.
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' "这个函数主要用来取你要调用的function module
EXPORTING
formname = c_form_name "将smartform赋值给formname
IMPORTING
fm_name = l_smf_name.
DATA:v_vbeln TYPE vbak-vbeln.
DATA: newpage TYPE i.
DATA:len TYPE i, a TYPE i.
newpage = 0. "为0则不换页,为1换页
len = 0. "统计内表总行数
a = 0. "循环加1,如果是最后一行也执行打印
v_vbeln = ''.
DESCRIBE TABLE itab1 LINES len.
LOOP AT itab1.
a = a + 1.
IF itab1-vbeln <> v_vbeln AND sy-tabix <> 1. "如果交货单后改变并且不是第一个行项目,因为第一个行项目v_vbeln为空,和它比较交货单号也改变了
newpage = 1.
it_temp = itab1. "如果单号改变则讲该行项目放入到 it_temp 里,然后将 it_temp 的值赋值给 it_out
APPEND it_temp.
ELSE. "交货单号没变
IF NOT it_temp IS INITIAL."如果订单号有改变,则将该值赋值给it_out
LOOP AT it_temp.
it_out = it_temp.
APPEND it_out.
ENDLOOP.
ENDIF.
CLEAR it_temp.
refresh it_temp[].
newpage = 0.
it_out = itab1.
APPEND it_out.
ENDIF.
v_vbeln = itab1-vbeln.
IF newpage = 1 OR a = len."如果换页则输出打印
ls_control_parameters-no_close = 'X'.
AT LAST.
ls_control_parameters-no_close = space.
ENDAT.
CALL FUNCTION l_smf_name "调用smartform,这里是以function的方式和smartform交互
EXPORTING
control_parameters = ls_control_parameters
output_options = ls_output_options
user_settings = 'X'
TABLES
gt_sfout = it_out[]
EXCEPTIONS
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CLEAR it_out.
REFRESH it_out[].
"newpage = 0.
ls_control_parameters-no_open = 'X'.
AT LAST.
ls_control_parameters-no_open = space.
ENDAT.
ENDIF.
ENDLOOP.
CLEAR itab1.
REFRESH itab1[].
ENDFORM. "printdata
smartform在资源里
结构如下: