【SAP Abap】记录一次SAP长文本内容通过Web页面完整显示的应用

【SAP Abap】记录一次SAP长文本内容通过Web页面完整显示的应用

  • 1、业务背景
  • 2、实现效果
  • 3、开发代码
    • 3.1、拼接html
    • 3.2、显示html
    • 3.3、ALV导出Excel
  • 4、小甜点

1、业务背景

业务在销售订单中,通过长文本描述,记录了一些生产备注信息,如生产标准、客户要求等等。要求开发一个报表,能够方便查看和导出长文本信息。

2、实现效果

由于ALV列表长文本显示不友好,且长度有限制,仅能显示128位,故考虑双击展示长文本详细内容,最终实现效果如下:

ALV列表:
【SAP Abap】记录一次SAP长文本内容通过Web页面完整显示的应用_第1张图片
双击行详情:
【SAP Abap】记录一次SAP长文本内容通过Web页面完整显示的应用_第2张图片
超链接支持右键下载:
【SAP Abap】记录一次SAP长文本内容通过Web页面完整显示的应用_第3张图片

3、开发代码

3.1、拼接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、显示html

(1)方式一:ABAP对话框显示html(本例使用,查看不用授权)

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)方法二:调用外部浏览器显示html
需要先下载,再访问,保存文件的时候需要授权。

   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导出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、小甜点

以下函数拿来即可用,支持单个或多个长文本内容的显示

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 '

' && <fs_data>-h1 && '

'
ELSE '' ) }|. lv_html_str = |{ lv_html_str }{ COND #( WHEN <fs_data>-h2 IS NOT INITIAL THEN '

' && <fs_data>-h2 && '

'
ELSE '' ) }|. lv_html_str = |{ lv_html_str }{ COND #( WHEN <fs_data>-h3 IS NOT INITIAL THEN '

' && <fs_data>-h3 && '

'
ELSE '' ) }|. lv_html_str = |{ lv_html_str }{ COND #( WHEN <fs_data>-h4 IS NOT INITIAL THEN '

' && <fs_data>-h4 && '

'
ELSE '' ) }|. lv_html_str = |{ lv_html_str }{ COND #( WHEN <fs_data>-h5 IS NOT INITIAL THEN '
' && <fs_data>-h5 && '
'
ELSE '' ) }|. lv_html_str = |{ lv_html_str }{ COND #( WHEN <fs_data>-h6 IS NOT INITIAL THEN '
' && <fs_data>-h6 && '
'
ELSE '' ) }|. lv_html_str = |{ lv_html_str }{ COND #( WHEN <fs_data>-div IS NOT INITIAL THEN '
' && <fs_data>-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.

展示效果如下图:
【SAP Abap】记录一次SAP长文本内容通过Web页面完整显示的应用_第4张图片
附入参结构
【SAP Abap】记录一次SAP长文本内容通过Web页面完整显示的应用_第5张图片

原创文章,转载请注明来源-X档案

你可能感兴趣的:(SAP,Abap,Web,前端,开发语言)