Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

WWW_ITAB_TO_HTML alternative

Mark-Pfister
Active Contributor

Dear all,

I'm trying to find a method or FM that takes an internal table as input and creates an HTML representation of that internal table.
FM WWW_ITAB_TO_HTML can do that - but it is marked as obsolete.
I searched for alternatives but wasn't able to find anything useful - besides code snipes generating HTML manually.

I need to create HTML in order to get formatted information into an XML format defined by a regulatory body in the EU.

The program needs to be running in the background without any user interaction.

Thanks for your help on this


Mark

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor

Maybe there's no library because it's so simple to do, and there are so many possible HTML renderings for one table... Just take that one and adapt it for your case:

CLASS lcl_ DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS itab_to_html
      IMPORTING
        table       TYPE ANY TABLE
      RETURNING
        VALUE(html) TYPE string.
ENDCLASS.

CLASS lcl_ IMPLEMENTATION.
  METHOD itab_to_html.
    DATA: lt_field TYPE TABLE OF string,
          l_field  TYPE string,
          l_num    TYPE i.
    FIELD-SYMBOLS:
          <l_field> TYPE any.

    html = '<table border="1">'.

    LOOP AT table ASSIGNING FIELD-SYMBOL(<line>).
      l_num = 0.
      CLEAR lt_field.

      DO.
        ADD 1 TO l_num.
        ASSIGN COMPONENT l_num OF STRUCTURE <line> TO <l_field>.
        IF sy-subrc NE 0.
          EXIT.
        ENDIF.
        l_field = <l_field>.
        APPEND l_field TO lt_field.
      ENDDO.

      html = html && '<tr><td>' && concat_lines_of( table = lt_field sep = '</td><td>' ) && '</td></tr>'.
    ENDLOOP.

    html = html && '</table>'.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  SELECT * FROM scarr INTO TABLE @DATA(scarrs).
  DATA(html) = lcl_=>itab_to_html( table = scarrs ).
11 REPLIES 11

p244500
Active Contributor

Hi,

I think you can do this using XLS Transformations. Just check .

Any transformation, whatever it's XSLT or ST (prefer ST, much faster)

0 Kudos

Dear Nawanandana,
I check but could find a build in transformation of an internal table into HTML... Did I look in the wrong spot?
I guess I could write a transformation myself - but that is exactly what I don't want to do 🙂

I check in S4 HANA 1709 - the FMs are still there....

Mark Pfister why don't you write your own transformation? HTML is a very simple format (depends on what you want it to contain, of course):
<table><tr><td>R1C1</td><td>R1C2</td></tr><tr><td>R2C1</td><td>R2C2</td></tr></table>

Example done in 2 minutes (very easy via the wizard because the internal table is based on a DDIC structure, I just needed to replace default names with table, tr and td):

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined">
  <tt:root line-type="ddic:SFLIGHT" name="SFLIGHT"/>
  <tt:template>
    <table>
      <tt:loop ref=".SFLIGHT">
        <tr>
          <td tt:value-ref="MANDT"/>
          <td tt:value-ref="CARRID"/>
          <td tt:value-ref="CONNID"/>
          <td tt:value-ref="FLDATE"/>
          <td tt:value-ref="PRICE"/>
          <td tt:value-ref="CURRENCY"/>
          <td tt:value-ref="PLANETYPE"/>
          <td tt:value-ref="SEATSMAX"/>
          <td tt:value-ref="SEATSOCC"/>
          <td tt:value-ref="PAYMENTSUM"/>
          <td tt:value-ref="SEATSMAX_B"/>
          <td tt:value-ref="SEATSOCC_B"/>
          <td tt:value-ref="SEATSMAX_F"/>
          <td tt:value-ref="SEATSOCC_F"/>
        </tr>
      </tt:loop>
    </table>
  </tt:template>
</tt:transform>

p244500
Active Contributor
0 Kudos

Hi Mark,

You can do it with given blog , it's clearly explain. you have to create structure and then go to tcode XSLT_TOOL(Transformation)

and assign your structure into the transformation. I have done couple of program using this its very easy. ex: download master information in to XML format . For the information check attached document which is in the blog . Still if you need any help let me know.

0 Kudos

Hi sandra.rossi,

Thanks for your comment and explanation!
I don't want to write an HTML transformation as I don't want to believe that I'm the first person that has such a requirement and that no easy to use, straightforward (e.g. not to call an ALV GRID or Print a list on convert from there etc..) solution has ever been developed by SAP 😉 .

I need to convert data from SAP's Class System (think material classification - not ABAP class) into an HTML table.
And the classes I need to convert are configurable via customizing... so it is not as straightforward and I can't use a DDIC structure.

As said I'm not familiar with the transformations: Could I dynamically generate them based on a type?

Best

Mark

Hello sandra.rossi,

I guess your correct - in the end of the day the HTML generation of an HTML Table is not that complicated.

Your example would be an nice addition to the SAP Standard (if it would be extend with standard HTML formatting options) in my opinion!
Only because it is easy doesn't justify that everyone needs to reinvent the wheel 😉

I would vote your comment as "best answer" - if you would convert it to an answer 🙂

Best

Mark

Mark Pfister Converted to an answer. To fit into the standard, I think the class should be much more abstracted, because there are many "simple" possibilities, so people would like sometimes having different styles, different column headings (or none), different value formats, etc. It should be a non-final class so that to adapt each part of the rendering. Maybe:
DATA(html_table) = NEW lcl_itab_to_html( itab ).
html_table->set_heading( column = 1 text = 'Price' css = 'font-weight:bold' ). html_table->set_format( column = 1 number_format = html_table->number_format_no_grouping ). DATA(html_string) = html_table->render( ).

In the end, a global HTML composer class would be even better... (note that in the past, SAP has had an unfortunate attempt with Dynamic Documents - I'm looking for one composer by the way, please let a message if you know one 😉 )

NTeunckens
Active Contributor

I would assume there are better (newer) ways of doing it, but you could check for the following 'classic' Function Modules and (Demo-)Programs on how HTML is generated ...

Functions :

  • G_PP_TABLE_TO_HTML_TAB
  • HR_TIME_SHEET_TO_HTML
  • HR_TIME_SHEET_TO_HTML_PERIOD
  • HR_TIME_SHEET_INTO_HTML


Demo programs :

  • BCALV_TEST_HTML_HEADER
  • BCALV_TEST_FULLSCREEN_HTML


Kind regards

Nic T.

0 Kudos

Hello nic.teunckens ,

Thank you for your answer!
The FM G_PP_TABLE_TO_HTML_TAB is not available on the lowest release I need to support - and I'm not 100% sure if the HR FMs are part of standard ECC - or if HR needs to be "activated".

I decided you just implement the HTML generation myself in the end.

Best

Mark

Sandra_Rossi
Active Contributor

Maybe there's no library because it's so simple to do, and there are so many possible HTML renderings for one table... Just take that one and adapt it for your case:

CLASS lcl_ DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS itab_to_html
      IMPORTING
        table       TYPE ANY TABLE
      RETURNING
        VALUE(html) TYPE string.
ENDCLASS.

CLASS lcl_ IMPLEMENTATION.
  METHOD itab_to_html.
    DATA: lt_field TYPE TABLE OF string,
          l_field  TYPE string,
          l_num    TYPE i.
    FIELD-SYMBOLS:
          <l_field> TYPE any.

    html = '<table border="1">'.

    LOOP AT table ASSIGNING FIELD-SYMBOL(<line>).
      l_num = 0.
      CLEAR lt_field.

      DO.
        ADD 1 TO l_num.
        ASSIGN COMPONENT l_num OF STRUCTURE <line> TO <l_field>.
        IF sy-subrc NE 0.
          EXIT.
        ENDIF.
        l_field = <l_field>.
        APPEND l_field TO lt_field.
      ENDDO.

      html = html && '<tr><td>' && concat_lines_of( table = lt_field sep = '</td><td>' ) && '</td></tr>'.
    ENDLOOP.

    html = html && '</table>'.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  SELECT * FROM scarr INTO TABLE @DATA(scarrs).
  DATA(html) = lcl_=>itab_to_html( table = scarrs ).