Print Table Control data (with header)

By | October 20, 2008 | Classical | 8,654 | 8

Shows how to give an option to get a printout of the table control Data.

In SAP, we have a table control to give a felixibility to enter more than one record at a time. User enters the data and save it, inturn we save that data into the database – by direct update, or by using the BAPI or some data transfer mechanism.

Today we will show easy it is to give an option to print the data in the table control. Users will definatly like the option and you will get good Return-on-Investment. For this purpose, we will use the FM FITRV_PRINT_TABLE_CONTROL_DATA to get the print out. This FM internally calls the ALV FM go generate an output. So, by using this FM, we are not only getting the print option but we are getting all the standard ALV functionality like Filtering, Excel download, layout settings etc.

For my example, I have created a small table control which selects the data from the database and dispalys in the table control. I have provided an option to print that data by adding activating the GUI button of Print.

I have added my code which uses the FM FITRV_PRINT_TABLE_CONTROL_DATA to generate the ALV output which can be printed.
Code Snippet for printing Table Control Data

*&---------------------------------------------------------------------*
*&  Shows how to print the data from the table control
*&---------------------------------------------------------------------*
*       Prints the Table control data
*----------------------------------------------------------------------*
FORM print_table_control_data .
*
  DATAl_callback TYPE sy-repid.
*
  l_callback sy-repid.
*
  CALL FUNCTION 'FITRV_PRINT_TABLE_CONTROL_DATA'
    EXPORTING
      table_control                        tc_data
      callback_program                     l_callback
*     CALLBACK_TOP_OF_LIST                 =
      callback_top_of_page                 'TOP_OF_PAGE'
*     CALLBACK_END_OF_PAGE                 =
*     CALLBACK_END_OF_LIST                 =
      optimize_column_width                'X'
      get_curr_quan_fields_from_ddic       'X'
*     WINDOW_TITLE                         =
      print_immediately                    ' '
    TABLES
      print_data                           itab
    EXCEPTIONS
      column_information_missing           1
      printing_not_possible                2.
*
*
ENDFORM.                    " print_table_control_data
*
*&---------------------------------------------------------------------*
*    Generates the Top of page
*----------------------------------------------------------------------*
FORM top_of_page.
*
*** Top of page Report
  WRITE'Sales Order report'.
*
ENDFORM.                    "top_of_page

When we press the button for the Print, it asks for the choice:

And by pressing the “Print Preview”, we will get the output in ALV:

Oops, we don’t have the header for this ALV output. I have debugged the FM FITRV_PRINT_TABLE_CONTROL_DATA and found out that this FM only determines the Field heading if we have created our table control with reference to the “Dictionary Table” !!! Since, we have already created a screen with appropriate logic, it is not advisable to change the table control to refer to the dictionary table. So, we need to find a way to change the Fieldcatalog when we call this FM with the table control created with reference to internal table.

To add the header in this output, we will use the FM REUSE_ALV_LIST_LAYOUT_INFO_GET to get the fieldcatalog of the ALV. We will modify that field catalog and use the FM REUSE_ALV_LIST_LAYOUT_INFO_SET to set the modified field catalog. We will use this logic in the subroutine which generates the TOP_OF_PAGE event.

Here is the code which we will use in the TOP_OF_PAGE subroutine to change the fieldcatalog of the ALV.
Code Snippet for the TOP-OF-PAGE to modify the field catalog

*&---------------------------------------------------------------------*
*    Generates the Top of page for the print
*    Also creates the column header in the print
*----------------------------------------------------------------------*
FORM top_of_page.
*
*** Top of page Report
  WRITE'Sales Order report'.
*
*** Modify column headers
  DATA:   lt_slis_fcat TYPE slis_t_fieldcat_alv,
          la_fcat      LIKE LINE OF lt_slis_fcat.
*
* get the ALV field catalog from the ALV generated by the print
* FM FITRV_PRINT_TABLE_CONTROL_DATA
  CALL FUNCTION 'REUSE_ALV_LIST_LAYOUT_INFO_GET'
    IMPORTING
      et_fieldcat   lt_slis_fcat
    EXCEPTIONS
      no_infos      1
      program_error 2
      OTHERS        3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
*
* Modify the Field catalog
  LOOP AT lt_slis_fcat INTO la_fcat.
    CASE la_fcat-fieldname.
      WHEN 'VBELN'.  la_fcat-reptext_ddic 'Sales Order'.
      WHEN 'ERDAT'.  la_fcat-reptext_ddic 'Create date'.
      WHEN 'ERNAM'.  la_fcat-reptext_ddic 'Created by'.
      WHEN 'NETWR'.
        la_fcat-reptext_ddic 'Net Value'.
        la_fcat-datatype 'DEC'.
    ENDCASE.
    MODIFY lt_slis_fcat FROM la_fcat.
  ENDLOOP.
*
* Set the field catalog information back to ALV
  CALL FUNCTION 'REUSE_ALV_LIST_LAYOUT_INFO_SET'
    EXPORTING
      it_fieldcat lt_slis_fcat.
*
ENDFORM.                    "top_of_page

It will generate the output like:

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

8 Comments

  • Minaxi shah

    Very very useful…..

  • Great Work!
    Thanks a lot!

  • Anonymous

    Thanks! Works perfect πŸ™‚

  • Hi Naimesh,
    Can u explain how to use this FM for filtering.

    Thanks πŸ™‚

  • Anonymous

    Very useful

  • Anonymous

    Good job naimesh

  • kumar chaudhari

    hi nimesh
    i am facing the same problem.
    i followed your logic for getting the header of column
    but still not working

  • Pradeep Jadhav

    Very Very useful ……………………..
    Great Job Done by you

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.