sapdev logo background
sapdev logo sapdev logo
Comments

ABAP code to Print abap web dynpro table




Once you have created a basic table on your web dynpro for ABAP application your users might want to be able to print this table, and not just the records currently displayed but all the records in the table. With the ABAP code below this becomes a fairly simply process! This will print the table to the users SAP printer or any printer setup in SAP.

An alternative method would be to call a SAP BSP and display the data using HTML statements, this would allow the user to print the information via there web browser or copy it into an external application such as excel.


Step 1 - Create Print button within your abap web dynpro application
First of all add a print button to you web dynpro for ABAP application and assign an Action to it.



Step 2 - Retrieve data displayed to user within ABAP web dynpro table
In-order to retreive the data displayed to a user within an abap web dypro table you you need to either follow the steps within the Read ABAP Web Dynpro Table section, which shows you how to get the data contained within an wdp table context, or simply re-retrieve the data from the SAP database using standard ABAP code such as:

* Retrieve new data
  select *
    from scarr
    into table it_scarr
   where CARRID = 'AA' .


Step 3 - Insert the Print code
The next step is to add the ABAP code which takes the internal table of data and sends it to the printer. It does this by using the print functionality of the object ALV method, so builds an ALV grid based on your internal table and then sends this to your SAP printer. For the example below the internal table containing the data is IT_SCARR, this gets assigned to it_datatab which in turn get assign to the field symbol <tab>. This means that it does not matter what structure your data table has, the code will build the printout appropriately. Therefore if you are using a data table with a differnet name and/or structure all you need to do is replace IT_SCARR with the name of your declared itab.


* Print web dynpro table using ALV object print functionality
  data it_datatab type ref to data.

* assign table containing data to it_datatab
  get reference of it_scarr into it_datatab.

  FIELD-SYMBOLS: <tab> TYPE table.
  ASSIGN it_datatab->* TO <tab>. "assign data table to field symbol

  DATA: it_alvtable   TYPE REF TO cl_salv_table.
  DATA: ld_prnt_params TYPE pri_params,
        ld_valid(1) TYPE c.

* Get print parameters
  CALL FUNCTION 'GET_PRINT_PARAMETERS'
    EXPORTING
      copies                 = '1'  "print_options-copies
      layout                 = 'X_65_255'
      no_dialog              = abap_true
    IMPORTING
      out_parameters         = ld_prnt_params
      valid                  = ld_valid
    EXCEPTIONS
      archive_info_not_found = 1
      invalid_print_params   = 2
      invalid_archive_params = 3
      OTHERS                 = 4.
  IF sy-subrc ne 0 OR ld_valid NE abap_true.
* invalid print parameters
    return.
  ENDIF.

* Start List Processing using generated print parameters
  NEW-PAGE PRINT ON PARAMETERS ld_prnt_params NO DIALOG.

* If you insert ABAP write statements here they will apear on your printout
* write: 'HelloWorld'.

* Create the ALV Object
  DATA: error_string TYPE string.

  cl_salv_table=>factory(
    EXPORTING
      list_display = abap_true
    IMPORTING
      r_salv_table = it_alvtable
    CHANGING
      t_table      =  ).

* Process the ALV Columns
  DATA: columns TYPE REF TO cl_salv_columns_table.

* Reference ALV columns object
  columns = it_alvtable->get_columns( ).
* Set columns to optimised width.
  columns->set_optimize( abap_false ).

*Set ALV to print in the background
  it_alvtable->display( ).
  NEW-PAGE PRINT OFF.

* Display print complete message



comments powered by Disqus