ABAP学习(28):HTML内容生成

ABAP生成HTML

使用类:cl_wd_html_writer,实现html内容生成。

注意:add_style方法生成的元素有问题,建议使用add_attribute,name='style' value='border:1px;padding:5px;font:"Microsoft YaHei"'

示例:

"一般使用字符串拼接生成html,容易出錯,不方便檢查
"使用if_wd_html_writer,生成html,更加方便檢查
FORM f_create_html.
  DATA:lo_html TYPE REF TO if_wd_html_writer.
  DATA:lo_exception TYPE REF TO cx_root.
  DATA:lv_msg TYPE string.

  "生成對象
  lo_html = cl_wd_html_writer=>new_writer( ).
*  "方法使用很簡單
*  lo_html->start_element( tag = 'html' ). "生成 <html>
*  lo_html->add_attribute( name = 'name' value = 'val' ).  "生成<標籤 name = 'val'>
*  lo_html->add_class( name = 'class1'  ). "生成<標籤 class = 'class1'>
*  lo_html->add_style( name = 'border' value ='1px' ). "生成<標籤 styles="border:1px">
*  lo_html->end_element( tag = 'html' ). "生成</html>
*  lo_html->empty_element( tag = 'br' ). "生成<br/>
*  lo_html->add_text( text = 'content text' ). "添加標籤文本<標籤>content text</標籤>
*  "返回生成的html
*  lv_html = lo_html->get_html( ).

*<html>
*<head>
*  <meta charset="utf-8">
*  <meta name="viewport" content="width=device-width, initial-scale=1">
*  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
*  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
*  <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
*  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
*  <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
*</head>
*
*<body>
*<div class="container">
*  <ul class="nav nav-pills">
*    <li class="nav-item">
*      <a class="nav-link active" href="ztom_html1.htm">首頁</a>
*    </li>
*    <li class="nav-item">
*      <a class="nav-link" href="ztom_html2.htm">Page2</a>
*    </li>
*  </ul>
*</div>
*</body>
*</html>
  TRY .
  "生成上述html,有start_element必須有end_element
  lo_html->start_element( 'html'
        )->start_element( 'head'
        )->start_element( 'meta'
        )->add_attribute( name = 'charset' value = 'utf-8'
        )->end_element( 'meta'
        )->start_element( 'meta'
        )->add_attribute( name = 'name' value = 'viewport'
        )->add_attribute( name = 'content' value = 'width=device-width, initial-scale=1'
        )->end_element( 'meta'
        )->start_element( 'link'
        )->add_attribute( name = 'rel' value = 'stylesheet'
        )->add_attribute( name = 'href' value = 'https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css'
        )->end_element( 'link'
        )->start_element( 'script'
        )->add_attribute( name = 'src' value = 'https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js'
        )->end_element( 'script'
        )->start_element( 'script'
        )->add_attribute( name = 'src' value = 'https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js'
        )->end_element( 'script'
        )->start_element( 'script'
        )->add_attribute( name = 'src' value = 'https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js'
        )->end_element( 'script'
        )->start_element( 'script'
        )->add_attribute( name = 'src' value = 'https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js'
        )->end_element( 'script'
        )->end_element( 'head'
        )->start_element( 'body'
        )->start_element( 'div'
        )->add_class( name = 'container'
        )->start_element( 'ul'
        )->add_class( name = 'nav nav-pills'
        )->start_element( 'li'
        )->add_class( name = 'nav-item'
        )->start_element( 'a'
        )->add_class( name = 'nav-link active'
        )->add_attribute( name = 'href' value = 'https://www.csdn.net'
        )->add_text( text = 'CSDN'
        )->end_element( 'a'
        )->end_element( 'li'
        )->start_element( 'li'
        )->add_class( name = 'nav-item'
        )->start_element( 'a'
        )->add_class( name = 'nav-link active'
        )->add_attribute( name = 'href' value = 'https://www.baidu.com'
        )->add_text( text = '百度'
        )->end_element( 'a'
        )->end_element( 'li'
        )->end_element( 'ul'
        )->end_element( 'div'

        )->start_element( 'table'
        )->add_attribute( name = 'border' value = '1'
        )->add_attribute( name = 'style' value = 'border-collapse:collapse;text-align:right;font-family:"Arial";font-size:10.5px;padding:0px 5px'
        )->start_element( tag = 'tr'
        )->add_attribute( name = 'style' value = 'background-color:#82CAFA'
        )->start_element( tag = 'th'
        )->add_attribute( name = 'style' value = 'width:120px;text-align:center;'
        )->add_text( text = 'DN建立日期'
        )->end_element( tag = 'th'
        )->start_element( tag = 'th'
        )->add_attribute( name = 'style' value = 'width:120px;text-align:center;'
        )->add_attribute( name = 'colspan' value = '2'
        )->add_text( text = '2021-DEC'
        )->end_element( tag = 'th'
        )->end_element( tag = 'tr'
        )->start_element( tag = 'tr'
        )->add_attribute( name = 'background-color' value = '#82CAFA'
        )->start_element( tag = 'th'
        )->add_attribute( name = 'style' value = 'width:120px;text-align:center;'
        )->add_text( text = '2021-Apr'
        )->end_element( tag = 'th'
        )->start_element( tag = 'th'
        )->add_attribute( name = 'style' value = 'width:120px;text-align:center;'
        )->add_text( text = 'AMT'
        )->end_element( tag = 'th'
        )->start_element( tag = 'th'
        )->add_attribute( name = 'style' value = 'width:120px;text-align:center;'
        )->add_text( text = 'QTY'
        )->end_element( tag = 'th'
        )->end_element( tag = 'tr'

        )->end_element( tag = 'table'
        )->end_element( 'body'
        )->end_element( 'html' )
    .
    lv_html = lo_html->get_html( ).
  CATCH cx_root INTO lo_exception.
    lv_msg = lo_exception->if_message~get_text( ).
    WRITE:/ lv_msg.
  ENDTRY.
ENDFORM.

 

posted @ 2022-03-19 09:09  渔歌晚唱  阅读(436)  评论(0编辑  收藏  举报