[SAP Abap] An application that records a SAP long text content and displays it completely through a web page

[SAP Abap] An application that records a SAP long text content and displays it completely through a web page

1. Business background

In the sales order, the business records some production remark information, such as production standards, customer requirements, etc., through long text descriptions. It is required to develop a report that can easily view and export long text information.

2. Realize the effect

Since the display of long text in the ALV list is unfriendly and the length is limited, only 128 bits can be displayed. Therefore, double-clicking to display the detailed content of the long text is considered. The final effect is as follows:

ALV list:
Insert image description here
double-click row details:
Insert image description here
hyperlink supports right-click download:
Insert image description here

3. Develop code

3.1. Splicing html

FUNCTION zmmfm056h.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  EXPORTING
*"     REFERENCE(EV_HTML) TYPE  STRING
*"  TABLES
*"      IT_DATA STRUCTURE  ZSMM114
*"----------------------------------------------------------------------

  "说明:Web页面调试工具:https://www.runoob.com/try/try.php?filename=tryhtml_tables
  types:begin of ty_str,
    str(1000),
    END OF ty_str.
  TYPES: BEGIN OF ty_url,
           name TYPE string,
           url  TYPE string,
         END OF ty_url.

  DATA: lt_str TYPE TABLE OF ty_str,
        lt_url TYPE TABLE OF ty_url WITH HEADER LINE.

  IF it_data[] IS INITIAL.
    ev_html = |<!DOCTYPE html><html><head><title>abap show html</title></head><body><p>请输入要展示的数据</p></body></html>|.
    RETURN.
  ENDIF.

  SORT it_data[] BY vbeln posnr.

  ev_html = |<!DOCTYPE html><html><head><title>明细数据</title></head><body link="#0563C1" vlink="#954F72" style="word-wrap:break-word">|
         && |<h1 style="text-align:center;">销售订单生产备注信息</h1>|
         && |<h4 style="text-align:center;">导出人:{
    
     sy-uname }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;导出日期:{
    
     sy-datum }</h4>|.

  LOOP AT it_data[] ASSIGNING FIELD-SYMBOL(<fs_data>).
    AT NEW vbeln.
      ev_html = ev_html
             && |<h3 style="text-align:left;">序号:{
    
     sy-index }</h3>|
             && |<table border=1 width=800 style='border-collapse:collapse;table-layout:fixed;'>|
             && | <tr style='font-family:arial;color:black;font-weight:bold;font-size:16px;text-align:center'>|
             && |  <td width=160>计划编号</td><td width=160>客户参考</td><td width=160>CRM下单人员</td><td width=160>销售订单类型</td><td width=160>销售订单号</td></tr>|
             && | <tr style='text-align:center'><td>{
    
     <fs_data>-bstkd_e }</td><td>{
    
     <fs_data>-bstnk }</td>|
             && |  <td>{
    
     <fs_data>-zzxdry }</td><td>{
    
     <fs_data>-auart }</td><td>{
    
     <fs_data>-vbeln }</td></tr>|
             && | <tr><td style='font-family:arial;color:black;font-weight:bold;font-size:16px;text-align:center'>抬头生产备注</td><td colspan=4>{
    
     <fs_data>-zscbz1 }</td></tr>|
             && | <tr><td style='font-family:arial;color:black;font-weight:bold;font-size:16px;text-align:center'>环保技术要求</td><td colspan=4>{
    
     <fs_data>-zhbjsyq }</td></tr>|
             .
            
      IF <fs_data>-zfiles IS INITIAL.
        ev_html = ev_html && | <tr><td style='font-family:arial;color:black;font-weight:bold;font-size:17px;text-align:center'>附件名称及链接</td>|
                          && | <td colspan=4>[无附件]</td></tr>|.
      ELSE.
        SPLIT <fs_data>-zfiles AT '||' INTO TABLE lt_str.
        IF lt_str[] IS INITIAL.
          ev_html = ev_html && | <tr><td style='font-family:arial;color:black;font-weight:bold;font-size:17px;text-align:center'>附件名称及链接</td>|
                             && | <td colspan=4>{
    
     <fs_data>-zfiles }</td></tr>|.
        ELSE.
          ev_html = ev_html && | <tr><td rowspan={
    
     lines( lt_str[] ) } style='font-family:arial;color:black;font-weight:bold;font-size:17px;text-align:center'>附件名称及链接</td>|.
          LOOP AT lt_str ASSIGNING FIELD-SYMBOL(<fs_str>).
            split <fs_str>-str at '|' into data(lstr1) DATA(lstr2).
            AT FIRST.
              ev_html = ev_html
                    && | <td colspan=4><a href="{ lstr2 }" target="_blank" rel="noopener noreferrer">{
    
     lstr1 }</a></td></tr>|.
              CONTINUE.
            ENDAT.
            ev_html = ev_html
                  && | <tr><td colspan=4><a href="{ lstr2 }" target="_blank" rel="noopener noreferrer">{
    
     lstr1 }</a></td></tr>|.
          ENDLOOP.
        ENDIF.
      ENDIF.
      ev_html = ev_html && | </table>|.
    ENDAT.
    SHIFT <fs_data>-posnr LEFT DELETING LEADING '0'.
    ev_html = ev_html
           && |<table border=1 width=800 style='border-collapse:collapse;table-layout:fixed;'>|
           && | <tr style='font-family:arial;color:black;font-weight:bold;font-size:16px;text-align:center'>|
           && |  <td rowspan=5 width=60>{
    
     <fs_data>-posnr }</td><td width=130>是否按单</td><td width=115>物料号</td><td width=160>物料描述</td>|
           && | <td width=70>订单数量</td><td width=70>销售单位</td><td width=70>生产工厂</td><td width=120>供应链转储订单</td></tr>|
           && | <tr style='text-align:center'><td>{
    
     <fs_data>-ismto }</td><td>{
    
     <fs_data>-matnr ALPHA = out }</td><td>{
    
     <fs_data>-arktx }</td>|
           && | <td>{
    
     <fs_data>-kwmeng }</td><td>{
    
     <fs_data>-vrkme }</td><td>{
    
     <fs_data>-reswk }</td><td>{
    
     <fs_data>-ebeln }</td></tr>|
           && | <tr><td style='font-family:arial;color:black;font-weight:bold;font-size:16px;text-align:center'>物料长文本</td><td colspan=6>{
    
     <fs_data>-ltext }</td></tr>|
           && | <tr><td style='font-family:arial;color:black;font-weight:bold;font-size:16px;text-align:center'>行项目生产备注</td><td colspan=6>{
    
     <fs_data>-zscbz2 }</td></tr>|
           && | <tr><td style='font-family:arial;color:black;font-weight:bold;font-size:16px;text-align:center'>客户标准号</td><td colspan=6>{
    
     <fs_data>-zkhbzh }</td></tr>|
           && |</table>|.

  ENDLOOP.

  ev_html = ev_html && |</body></html>|.

ENDFUNCTION.

3.2. Display html

(1) Method 1: ABAP dialog box displays html (used in this example, no authorization is required for viewing)

FORM frm_double_click  USING    p_row TYPE salv_de_row
                                p_column TYPE salv_de_column.
  DATA: lt_data TYPE TABLE OF zsmm114 WITH HEADER LINE.

  READ TABLE gt_vbak_po INTO DATA(ls_wa) INDEX p_row."读当前行
  CLEAR: lt_data,lt_data[].
  IF p_column EQ 'VBELN'.   "按订单查询
    LOOP AT gt_vbak_po ASSIGNING FIELD-SYMBOL(<fs_vbak>) WHERE vbeln = ls_wa-vbeln.
      lt_data = CORRESPONDING #( <fs_vbak> ).
      APPEND lt_data.
      CLEAR lt_data.
    ENDLOOP.
  ELSE.                     "按订单行查询
    lt_data = CORRESPONDING #( ls_wa ).
    APPEND lt_data.
    CLEAR lt_data.
  ENDIF.

  CHECK lt_data[] IS NOT INITIAL.
  DATA: lv_html_str TYPE string.
  CALL FUNCTION 'ZMMFM056H'
    IMPORTING
      ev_html = lv_html_str
    TABLES
      it_data = lt_data[].

  "lv_html_str = |<!DOCTYPE html><html><head><title>abap show html</title></head><body><p>Hello  world </p></body></html>|.
  cl_abap_browser=>show_html( html_string = lv_html_str context_menu = 'X' printing = 'X' title = '销售订单生产备注信息').
  
ENDFORM.

(2) Method 2: To call an external browser to display HTML,
you need to download it first and then access it. Authorization is required when saving the file.

   CALL  FUNCTION  'GUI_DOWNLOAD'
     EXPORTING
      FILENAME                 =  'C:\hello.html'
     TABLES
      DATA_TAB                 = T_HTML
     EXCEPTIONS
      FILE_WRITE_ERROR         =  1
      NO_BATCH                 =  2
      GUI_REFUSE_FILETRANSFER  =  3
      INVALID_TYPE             =  4
      NO_AUTHORITY             =  5
      UNKNOWN_ERROR            =  6
      HEADER_NOT_ALLOWED       =  7
      SEPARATOR_NOT_ALLOWED    =  8
      FILESIZE_NOT_ALLOWED     =  9
      HEADER_TOO_LONG          =  10
      DP_ERROR_CREATE          =  11
      DP_ERROR_SEND            =  12
      DP_ERROR_WRITE           =  13
      UNKNOWN_DP_ERROR         =  14
      ACCESS_DENIED            =  15
      DP_OUT_OF_MEMORY         =  16
      DISK_FULL                =  17
      DP_TIMEOUT               =  18
      FILE_NOT_FOUND           =  19
      DATAPROVIDER_EXCEPTION   =  20
      CONTROL_FLUSH_ERROR      =  21
       OTHERS                   =  22 .
   IF SY -SUBRC <>  0 .
     MESSAGE  ID SY -MSGID  TYPE SY -MSGTY  NUMBER SY -MSGNO
     WITH SY -MSGV1 SY -MSGV2 SY -MSGV3 SY -MSGV4 .
   ENDIF .
 
   CALL  FUNCTION  'GUI_RUN'
     EXPORTING
      COMMAND  =  'C:\hello.html'
*     PARAMETER        =
*     CD      =
*   IMPORTING
*     RETURNCODE       =

3.3. ALV export to Excel

FORM frm_export_xls.
  DATA: lt_vbak_po LIKE gt_vbak_po.
  CLEAR: lt_vbak_po, lt_vbak_po[].
  lt_vbak_po[] = CORRESPONDING #( gt_vbak_po[] ).
  LOOP AT lt_vbak_po[] ASSIGNING FIELD-SYMBOL(<fs_lt_po>).
    REPLACE ALL OCCURRENCES OF '||' IN <fs_lt_po>-zfiles WITH cl_abap_char_utilities=>cr_lf.
  ENDLOOP.

  DATA: lo_converter TYPE REF TO zcl_excel_converter.
  "创建zcl_excel_converter类
  CREATE OBJECT lo_converter.
  TRY .
      lo_converter->convert(
        EXPORTING
          io_alv        = gr_table
          it_table      = lt_vbak_po[]
          i_row_int     = 1
          i_column_int  = 1
          ).
    CATCH zcx_excel.
  ENDTRY.

  "选择文件保存路径
  TRY.
      DATA:filepath TYPE string VALUE 'D:'.
      cl_gui_frontend_services=>directory_browse(
          EXPORTING
            window_title         = 'Select path to download EXCEL-file'
            initial_folder       = filepath
          CHANGING
            selected_folder      = filepath
          EXCEPTIONS
            cntl_error           = 1
            error_no_gui         = 2
            not_supported_by_gui = 3
            OTHERS               = 4
        ).
  ENDTRY.
  CHECK sy-subrc IS INITIAL AND filepath IS NOT INITIAL.

  "调用write_file方法导出excel
  lo_converter->write_file( i_path = |{
    
     filepath && '\' }销售订单生产备注-{
    
     sy-datum }.xlsx| ).

ENDFORM.

4. Small desserts

The following functions are ready to use and support the display of single or multiple long text contents.

FUNCTION zbcfm003.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(IV_TITLE) TYPE  CHAR255
*"     REFERENCE(IV_CONTEXT_MENU) TYPE  XFELD OPTIONAL
*"     REFERENCE(IV_PRINTING) TYPE  XFELD OPTIONAL
*"  TABLES
*"      IT_DATA STRUCTURE  ZSBC009 OPTIONAL
*"----------------------------------------------------------------------

  DATA lv_html_str TYPE string.
  lv_html_str = |<!DOCTYPE html><html><head><meta charset="utf-8">|
             && |<title>{
    
     iv_title }</title></head><body>|.

  IF it_data[] IS INITIAL.
    lv_html_str = |{
    
     lv_html_str }<p>{
    
     COND #( WHEN sy-langu EQ 1 THEN '空空如也' ELSE 'Nothing to display') }</p>|.
  ELSE.
    LOOP AT it_data[] ASSIGNING FIELD-SYMBOL(<fs_data>).
      IF <fs_data> IS INITIAL.
        CONTINUE.
      ENDIF.
      lv_html_str = |{
    
     lv_html_str }{
    
     COND #( WHEN <fs_data>-h1 IS NOT INITIAL THEN '<h1>' && <fs_data>-h1 && '</h1>' ELSE '' ) }|.
      lv_html_str = |{
    
     lv_html_str }{
    
     COND #( WHEN <fs_data>-h2 IS NOT INITIAL THEN '<h2>' && <fs_data>-h2 && '</h2>' ELSE '' ) }|.
      lv_html_str = |{
    
     lv_html_str }{
    
     COND #( WHEN <fs_data>-h3 IS NOT INITIAL THEN '<h3>' && <fs_data>-h3 && '</h3>' ELSE '' ) }|.
      lv_html_str = |{
    
     lv_html_str }{
    
     COND #( WHEN <fs_data>-h4 IS NOT INITIAL THEN '<h4>' && <fs_data>-h4 && '</h4>' ELSE '' ) }|.
      lv_html_str = |{
    
     lv_html_str }{
    
     COND #( WHEN <fs_data>-h5 IS NOT INITIAL THEN '<h5>' && <fs_data>-h5 && '</h5>' ELSE '' ) }|.
      lv_html_str = |{
    
     lv_html_str }{
    
     COND #( WHEN <fs_data>-h6 IS NOT INITIAL THEN '<h6>' && <fs_data>-h6 && '</h6>' ELSE '' ) }|.
      lv_html_str = |{
    
     lv_html_str }{
    
     COND #( WHEN <fs_data>-div IS NOT INITIAL THEN '<div>' && <fs_data>-div && '</div>' ELSE '' ) }|.
      lv_html_str = |{
    
     lv_html_str }<div>&nbsp;</div>|.
    ENDLOOP.
  ENDIF.

  lv_html_str = |{
    
     lv_html_str }</body></html>|.

  cl_abap_browser=>show_html( html_string = lv_html_str
                              context_menu = iv_context_menu
                              printing = iv_printing
                              title = iv_title ).

ENDFUNCTION.

The display effect is as follows:
Insert image description here
Attached parameter structure
Insert image description here

Original article, please indicate the source when reprinting - X-Files

Guess you like

Origin blog.csdn.net/XLevon/article/details/131998475