从sflight数据库表中检索航班信息,并将这些信息转换成HTML格式,然后下载或显示在前端
开发步骤
① 自定义一个数据类型 ty_sflight 来存储航班信息
② 声明内表和工作区变量,用于存储表头、字段、HTML内容和航班详细信息以及创建字段目录lt_fcat
③ 从sflight数据库表中选择数据并存储在lt_sflight内表中
④ 填充字段目录lt_fcat,并为每个字段设置描述
⑤ 循环遍历字段目录lt_fcat,为每个字段设置HTML表头和属性
提示Tips
WWW_ITAB_TO_HTML_HEADERS函数用于填充列标题
WWW_ITAB_TO_HTML_LAYOUT函数用于填充列单元格属性
⑥ 设置HTML显示的标题,并准备HTML内容,调用WWW_ITAB_TO_HTML函数,将内部表转换为HTML格式
提示Tips
WWW_ITAB_TO_HTML函数用于将内表数据转换为HTML
⑦ 将生成的HTML保存为文件,并尝试下载或在前端显示
激活并运行程序,效果如下图所示:
完整代码如下所示:
REPORT z437_test_2024.* 自定义数据类型
TYPES:BEGIN OF ty_sflight,carrid TYPE s_carr_id, " 航线代码connid TYPE s_conn_id, " 航班连接编号fldate TYPE s_date, " 航班日期price TYPE s_price, " 航空运费currency TYPE s_currcode, " 航班的本地货币
END OF ty_sflight.DATA: t_header TYPE STANDARD TABLE OF w3head WITH HEADER LINE, " Header表头t_fields TYPE STANDARD TABLE OF w3fields WITH HEADER LINE, " Fields字段t_html TYPE STANDARD TABLE OF w3html, " Html内容wa_header TYPE w3head,w_head TYPE w3head.DATA: lt_sflight TYPE TABLE OF ty_sflight, " Flights Details航班详细信息lt_fcat TYPE lvc_t_fcat WITH HEADER LINE. " Fieldcatalog字段目录*----------------------------------------------------------------------*
* DESC: START-OF-SELECTION 事件
*----------------------------------------------------------------------*
START-OF-SELECTION." 从sflight表中选择最多10条数据记录SELECT * FROM sflightINTO CORRESPONDING FIELDS OF TABLE lt_sflightUP TO 10 ROWS.*----------------------------------------------------------------------*
* DESC: END-OF-SELECTION 事件
*----------------------------------------------------------------------*
END-OF-SELECTION." 填充字段目录" 创建字段目录的循环,为每个字段设置描述lt_fcat-coltext = 'Airline Code'.APPEND lt_fcat.lt_fcat-coltext = 'Connection Number'.APPEND lt_fcat.lt_fcat-coltext = 'Flight date'.APPEND lt_fcat.lt_fcat-coltext = 'Airfare'.APPEND lt_fcat.lt_fcat-coltext = 'Currency'.APPEND lt_fcat." 填充表头和属性" 循环遍历字段目录,为每个字段设置表头和属性LOOP AT lt_fcat.w_head-text = lt_fcat-coltext." 调用函数,将内部表转换为HTML表头CALL FUNCTION 'WWW_ITAB_TO_HTML_HEADERS'EXPORTINGfield_nr = sy-tabixtext = w_head-textfgcolor = 'black'bgcolor = 'green'TABLESheader = t_header." 调用函数,将内部表转换为HTML布局CALL FUNCTION 'WWW_ITAB_TO_HTML_LAYOUT'EXPORTINGfield_nr = sy-tabixsize = '3'fgcolor = 'black'TABLESfields = t_fields.ENDLOOP." 设置显示的标题wa_header-text = 'Flights Details'.wa_header-font = 'Arial'.wa_header-size = '2'." 准备HTML内容" 清空HTML表,然后调用函数将内部表转换为HTMLREFRESH t_html.CALL FUNCTION 'WWW_ITAB_TO_HTML'EXPORTINGtable_header = wa_headerTABLEShtml = t_htmlfields = t_fieldsrow_header = t_headeritable = lt_sflight." 下载HTML到前端" 调用函数,将生成的HTML保存为文件并下载CALL FUNCTION 'GUI_DOWNLOAD'EXPORTINGfilename = 'C:\Users\HP\Desktop\Flights.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." 在前端显示HTML文件" 调用方法,打开HTML文件CALL METHOD cl_gui_frontend_services=>executeEXPORTINGdocument = 'C:\Users\HP\Desktop\Flights.html'operation = 'OPEN'EXCEPTIONScntl_error = 1error_no_gui = 2bad_parameter = 3file_not_found = 4path_not_found = 5file_extension_unknown = 6error_execute_failed = 7synchronous_failed = 8not_supported_by_gui = 9others = 10.IF sy-subrc <> 0.MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.ENDIF.