Você está na página 1de 4

How to force Page-Break while printing ALV, after a fixed number of records (Ex:15 records)

Author: Krishna Kishor Kammaje


Contact : krishna.kammaje@yahoo.com One of the common problems with ALV reports occurs while printing. When you print an ALV report, it does not fire TOP_OF_PAGE for every new page printed, also, SYPAGNO does not get incremented for every new page printed. This document will explain you How to force a PAGE BREAK after certain number of ALV records, thus firing TOP_OF_PAGE and incrementing SY-PAGNO.

Scenario
Below is the PRINT PREVIEW of my report output.

As can be seen, there are no page breaks after 15 records.

Partial Solution:
Page break can be forced if there is any change in the value of a sorted column. For example, to force a page break for every change in PERNR, fill IT_SORT table in the FM ALV_GRID_DISPLAY as below.

====================================================

PERFORM LSORT USING

'PERNR' 'T_FINAL' '' '*'.

FORM LSORT USING l_FIELD l_TABLE l_UP l_group. ADD 1 TO L_POS. w_SORT-SPOS = l_POS. w_SORT-FIELDNAME = l_FIELD. w_SORT-TABNAME = l_TABLE. w_SORT-UP = l_UP. w_sort-group = l_group. APPEND w_sort to IT_SORT. ENDFORM. * Display the data in ALV format CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program i_callback_top_of_page I_CALLBACK_HTML_TOP_OF_PAGE IS_LAYOUT it_fieldcat IT_SORT it_events TABLES t_outtab = = = = = = = sy-repid 'TOP_OF_PAGE' 'HTML_TOP_OF_PAGE' ws_layout t_fieldcat it_sort it_events

= t_final.

=========================================== * in w_sort-group will force a page break, while UL will force an under line. Our aim is to get a Page Break for every 15 rows. How to do this? Here it is.

Full Solution:
Add a new field pagebrk in the ALV internal table definition. i.e. in T_FINAL for the above scenario. Modify the field catalog to include this field. (Else you will encounter a short dump) Here is the field catalog for the field pagebrk

line_fieldcat-fieldname = 'PAGEBRK'. line_fieldcat-tabname = 'T_FINAL'. line_fieldcat-no_out = 'X'. APPEND line_fieldcat TO t_fieldcat. line_fieldcat-no_out = ''.
line_fieldcat-no_out makes sure that the field is not shown in the report. Modify the IT_SORT as below. PERFORM LSORT USING 'PERNR' 'T_FINAL' '' ''.

PERFORM LSORT USING 'PAGEBRK' 'T_FINAL' '' '*'. Use the above mentioned form LSORT. Now, before you call the display ALV FM, T_FINAL need to be updated with pagebrk column as below.

SORT t_final by pernr. DESCRIBE TABLE t_final LINES l_tot. l_lin = 15. Initial page record length. l_pag = 1. LOOP AT t_final INTO w_final. IF sy-tabix LE l_tot. IF sy-tabix LE l_lin. w_final-pagebrk = l_pag. MODIFY t_final FROM w_final INDEX sytabix TRANSPORTING pagebrk. ELSE. l_lin = l_lin + 15. Break after 15 records. l_pag = l_pag + 1. w_final-pagebrk = l_pag. MODIFY t_final FROM w_final INDEX sytabix TRANSPORTING pagebrk. ENDIF. ENDIF. ENDLOOP
Below is the output thus obtained

I know, now it looks simple.

Você também pode gostar