【SAP Abap】记录一次SAP长文本内容通过Web页面完整显示的应用
- 1、业务背景
- 2、实现效果
- 3、开发代码
- 3.1、拼接html
- 3.2、显示html
- 3.3、ALV导出Excel
1、业务背景
业务在销售订单中,通过长文本描述,记录了一些生产备注信息,如生产标准、客户要求等等。要求开发一个报表,能够方便查看和导出长文本信息。
2、实现效果
由于ALV列表长文本显示不友好,且长度有限制,仅能显示128位,故考虑双击展示长文本详细内容,最终实现效果如下:
ALV列表:
双击行详情:
超链接支持右键下载:
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_tablestypes: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 } 导出日期:{ 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_rowp_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'IMPORTINGev_html = lv_html_strTABLESit_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'EXPORTINGFILENAME = 'C:\hello.html'TABLESDATA_TAB = T_HTMLEXCEPTIONSFILE_WRITE_ERROR = 1NO_BATCH = 2GUI_REFUSE_FILETRANSFER = 3INVALID_TYPE = 4NO_AUTHORITY = 5UNKNOWN_ERROR = 6HEADER_NOT_ALLOWED = 7SEPARATOR_NOT_ALLOWED = 8FILESIZE_NOT_ALLOWED = 9HEADER_TOO_LONG = 10DP_ERROR_CREATE = 11DP_ERROR_SEND = 12DP_ERROR_WRITE = 13UNKNOWN_DP_ERROR = 14ACCESS_DENIED = 15DP_OUT_OF_MEMORY = 16DISK_FULL = 17DP_TIMEOUT = 18FILE_NOT_FOUND = 19DATAPROVIDER_EXCEPTION = 20CONTROL_FLUSH_ERROR = 21OTHERS = 22 .IF SY -SUBRC <> 0 .MESSAGE ID SY -MSGID TYPE SY -MSGTY NUMBER SY -MSGNOWITH SY -MSGV1 SY -MSGV2 SY -MSGV3 SY -MSGV4 .ENDIF .CALL FUNCTION 'GUI_RUN'EXPORTINGCOMMAND = '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(EXPORTINGio_alv = gr_tableit_table = lt_vbak_po[]i_row_int = 1i_column_int = 1).CATCH zcx_excel.ENDTRY."选择文件保存路径TRY.DATA:filepath TYPE string VALUE 'D:'.cl_gui_frontend_services=>directory_browse(EXPORTINGwindow_title = 'Select path to download EXCEL-file'initial_folder = filepathCHANGINGselected_folder = filepathEXCEPTIONScntl_error = 1error_no_gui = 2not_supported_by_gui = 3OTHERS = 4).ENDTRY.CHECK sy-subrc IS INITIAL AND filepath IS NOT INITIAL."调用write_file方法导出excello_converter->write_file( i_path = |{ filepath && '\' }销售订单生产备注-{ sy-datum }.xlsx| ).ENDFORM.
原创文章,转载请注明来源-X档案