从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 sflight
INTO CORRESPONDING FIELDS OF TABLE lt_sflight
UP 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'
EXPORTING
field_nr = sy-tabix
text = w_head-text
fgcolor = 'black'
bgcolor = 'green'
TABLES
header = t_header.
" 调用函数,将内部表转换为HTML布局
CALL FUNCTION 'WWW_ITAB_TO_HTML_LAYOUT'
EXPORTING
field_nr = sy-tabix
size = '3'
fgcolor = 'black'
TABLES
fields = t_fields.
ENDLOOP.
" 设置显示的标题
wa_header-text = 'Flights Details'.
wa_header-font = 'Arial'.
wa_header-size = '2'.
" 准备HTML内容
" 清空HTML表,然后调用函数将内部表转换为HTML
REFRESH t_html.
CALL FUNCTION 'WWW_ITAB_TO_HTML'
EXPORTING
table_header = wa_header
TABLES
html = t_html
fields = t_fields
row_header = t_header
itable = lt_sflight.
" 下载HTML到前端
" 调用函数,将生成的HTML保存为文件并下载
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = 'C:\Users\HP\Desktop\Flights.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.
" 在前端显示HTML文件
" 调用方法,打开HTML文件
CALL METHOD cl_gui_frontend_services=>execute
EXPORTING
document = 'C:\Users\HP\Desktop\Flights.html'
operation = 'OPEN'
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
bad_parameter = 3
file_not_found = 4
path_not_found = 5
file_extension_unknown = 6
error_execute_failed = 7
synchronous_failed = 8
not_supported_by_gui = 9
others = 10.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.