SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

How to convert any Internal table into HTML Table

How to convert any Internal table into HTML Table

We often come across the requirement to convert the data in an internal table into HTML table and send an email or download as a HTML file. I would like to show you a very simple way to do this just with one method.

Below is the format of the HTML table:

<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>

To convert the data into HTML table, Create a class and use the code given below :

How to convert any Internal table into HTML Table

How to convert any Internal table into HTML Table

How to convert any Internal table into HTML Table

Type declarations:


  types:
    BEGIN OF ty_fields,
        field TYPE fieldname,
        width TYPE char20,
        text  TYPE char80,
      END OF ty_fields .
  types:
    tt_fields TYPE STANDARD TABLE OF ty_fields . 

Past below code in the method CONVERT_ITEM_TO_HTML


*&---------------------------------------------------------------------*
*&
*& ****  Coding by SAPTechPro.com  *****
*&---------------------------------------------------------------------*
METHOD CONVERT_ITAB_TO_HTML.
DATA: ls_final  TYPE string,
          lv_width TYPE string.
FIELD-SYMBOLS: <l_data> TYPE any.
    ls_final = '<table class="table-responsive table-condensed" style="background: #d7ecf4;word-wrap: break-word;table-layout: fixed;">'.
APPEND ls_final TO e_html.
    ls_final = '<thead>'.
APPEND ls_final TO e_html.
    ls_final = '<tr>'.
APPEND ls_final TO e_html.
LOOP AT i_fields ASSIGNING FIELD-SYMBOL(<l_fields>).
      ls_final = '<th style="background: #5dcbfd;&WIDTH&">' && <l_fields>-text && '</th>'.
IF <l_fields>-width IS INITIAL.
REPLACE FIRST OCCURRENCE OF '&WIDTH&' IN ls_final WITH space.
ELSE.
        lv_width = 'width:' && <l_fields>-width &&  ';'.
REPLACE FIRST OCCURRENCE OF '&WIDTH&' IN ls_final WITH lv_width.
ENDIF.
APPEND ls_final TO e_html.
ENDLOOP.
    ls_final = '</tr>'.
APPEND ls_final TO e_html.
    ls_final = '</thead>'.
APPEND ls_final TO e_html.
    ls_final = '<tbody>'.
APPEND ls_final TO e_html.
LOOP AT i_itab ASSIGNING FIELD-SYMBOL(<l_out>).
      ls_final = '<tr>'.
APPEND ls_final TO e_html.
LOOP AT i_fields ASSIGNING <l_fields>.
ASSIGN COMPONENT <l_fields>-field OF STRUCTURE <l_out> TO <l_data>.
IF sy-subrc = 0.
          ls_final = '<td>' && <l_data> && '</td>'.
APPEND ls_final TO e_html.
ENDIF.
ENDLOOP.
      ls_final = '</tr>'.
APPEND ls_final TO e_html.
ENDLOOP.
    ls_final = '</tbody>'.
APPEND ls_final TO e_html.
    ls_final = '</table>'.
APPEND ls_final TO e_html.
ENDMETHOD.

Call the method CONVERT_ITAB_TO_HTML and pass the field catalog and Internal table to convert as shown below:


*&---------------------------------------------------------------------*
*& Report  ZSTP_ITAB_TO_HTML
*&
*&---------------------------------------------------------------------*
*&
*& ****  Coding by SAPTechPro.com  *****
*&---------------------------------------------------------------------*

REPORT zstp_itab_to_html.

DATA: li_mata   TYPE STANDARD TABLE OF mara,
      li_fields	TYPE zstp_itab_to_html=>tt_fields,
      ls_field  TYPE zkir_class=>ty_fields,
      li_html	  TYPE STANDARD TABLE OF string.


SELECT *
  FROM mara
  INTO TABLE li_mata.
IF sy-subrc = 0.

* Internal table li_mata contains many field and we may not required all the fields in the HTML output
* So fill the field catelog with list of fields required in the HTML

  ls_field-text = 'Material Number'.
  ls_field-field = 'MATNR'.
  ls_field-width = '16%'. "Specify the width in % if you want to increase the column width
  APPEND ls_field TO li_fields.

  clear ls_field.
  ls_field-text = 'Material Type'.
  ls_field-field = 'MTART'.
  APPEND ls_field TO li_fields.

  clear ls_field.
  ls_field-text = 'Material Group'.
  ls_field-field = 'MATKL'.
  APPEND ls_field TO li_fields.

  clear ls_field.
  ls_field-text = 'MEINS'.
  ls_field-field = 'MEINS'.
  ls_field-width = '6%'.
  APPEND ls_field TO li_fields.

  zstp_itab_to_html=>convert_itab_to_html(
    EXPORTING
      i_fields = li_fields    " Table of fields
      i_itab   = li_mata
    IMPORTING
      e_html   = li_html ).

  IF li_html IS NOT INITIAL.
* Output of the li_html can be used for email sending or can be downloaded as a HTML file.
  ENDIF.


ENDIF. 

 

Content of li_html internal table in Debugging:

How to convert any Internal table into HTML Table

This content will display as a HTML table in the browser as shown below:

How to convert any Internal table into HTML Table

Please write your questions or comments in comments box below..

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in