标签:har 参考 版本 mount eth url ring for nic
ABAP 查看股票 SALV版本:
执行界面:
代码:
1 REPORT zrep_ooalv_stock. 2 3 ********************************************************************* 4 *使用方法: 5 *输入股票号码,获得新浪财经API实时沪深两市股票数据,双击可显示个股K线 6 *代码移植: 7 *在SAP SE38中创建报表ZREP_OOALV_STOCK,粘贴代码 8 *需要双击屏幕号码 Screen 100并激活 9 *双击状态‘OOALV1‘,设置‘BACK‘按钮,激活 10 *声明: 11 *程序仅供参考,不能作为投资依据. Aaron Hao - 2017/11/11 12 ********************************************************************* 13 14 15 "定义股票信息结构体 16 TYPES: BEGIN OF ty_stock_live, 17 sid TYPE char10, sname TYPE char10, 18 topen TYPE char10, yclose TYPE char10, 19 cprice TYPE char10, thigh TYPE char10, 20 tlow TYPE char10, buy TYPE char10, 21 sale TYPE char10, number TYPE char20, 22 amount TYPE char20, buy11 TYPE char10, 23 buy12 TYPE char10, buy21 TYPE char10, 24 buy22 TYPE char10, buy31 TYPE char10, 25 buy32 TYPE char10, buy41 TYPE char10, 26 buy42 TYPE char10, buy51 TYPE char10, 27 buy52 TYPE char10, sale11 TYPE char10, 28 sale12 TYPE char10, sale21 TYPE char10, 29 sale22 TYPE char10, sale31 TYPE char10, 30 sale32 TYPE char10, sale41 TYPE char10, 31 sale42 TYPE char10, sale51 TYPE char10, 32 sale52 TYPE char10, date TYPE char15, 33 time TYPE char15, color TYPE lvc_t_scol, "Color. 34 END OF ty_stock_live. 35 36 TYPES: BEGIN OF ty_stock_id, 37 stock_id TYPE char6, 38 END OF ty_stock_id. 39 40 TYPES ty_tab_stock_id TYPE STANDARD TABLE OF ty_stock_id. 41 42 TYPES ty_tab_stock_live TYPE STANDARD TABLE OF ty_stock_live. 43 44 DATA: gv_stock_id(6) TYPE c. 45 DATA: tmp_num(6) TYPE n, 46 idx TYPE i, 47 idx_tmp TYPE i. 48 49 "定义内表,定义SALV 的类CL_GUI_SALV_TABLE的对象变量 50 DATA gt_stock_id TYPE ty_tab_stock_id. 51 DATA gs_stock_id TYPE ty_stock_id. 52 53 "定义股票信息内表 54 DATA gt_stock TYPE ty_tab_stock_live . 55 DATA gs_stock TYPE ty_stock_live. 56 DATA: gt_stock_pub_list TYPE TABLE OF string. 57 58 "定义股票信息OOALV相关类 59 DATA go_table TYPE REF TO cl_salv_table. 60 DATA go_functions TYPE REF TO cl_salv_functions_list. 61 DATA: go_columns TYPE REF TO cl_salv_columns_table. 62 DATA: go_column TYPE REF TO cl_salv_column_table. 63 DATA: go_column_list TYPE REF TO cl_salv_column_list. 64 DATA:go_container TYPE REF TO cl_gui_custom_container. 65 DATA: go_dock TYPE REF TO cl_gui_docking_container, 66 go_picture TYPE REF TO cl_gui_picture. 67 DATA: gv_url TYPE char256, 68 gt_data TYPE STANDARD TABLE OF x255. 69 70 71 CLASS lcl_event_handler DEFINITION DEFERRED. 72 73 DATA: go_timer TYPE REF TO cl_gui_timer, 74 go_evt_hndl TYPE REF TO lcl_event_handler. 75 "定义ALV双击事件类 76 CLASS zcl_handle_events DEFINITION. 77 PUBLIC SECTION. 78 METHODS: on_double_click FOR EVENT double_click OF 79 cl_salv_events_table 80 IMPORTING 81 row "事件触发所在的行号 82 column."事件触发所在的列名 83 METHODS: on_user_command FOR EVENT added_function OF 84 cl_salv_events_table 85 IMPORTING e_salv_function. 86 ENDCLASS. 87 88 CLASS zcl_handle_events IMPLEMENTATION. 89 METHOD on_double_click. 90 PERFORM show_stock_details 91 USING row column. 92 ENDMETHOD. 93 METHOD on_user_command. 94 PERFORM handle_user_command USING e_salv_function. 95 ENDMETHOD. 96 ENDCLASS. 97 98 "程序开始 99 100 START-OF-SELECTION. 101 SELECT-OPTIONS: 102 s_sid FOR gv_stock_id. 103 PERFORM init_timer. 104 PERFORM show_grid. 105 106 "获得输入的股票号码 107 FORM get_input_stock_id. 108 LOOP AT s_sid. 109 CLEAR gs_stock_id. 110 IF s_sid-option = ‘EQ‘ . 111 IF s_sid-low <> ‘‘ . 112 gs_stock_id-stock_id = s_sid-low. 113 INSERT gs_stock_id INTO TABLE gt_stock_id. 114 ENDIF. 115 IF s_sid-high <> ‘‘ . 116 gs_stock_id-stock_id = s_sid-high. 117 INSERT gs_stock_id INTO TABLE gt_stock_id. 118 ENDIF. 119 ENDIF. 120 IF s_sid-option = ‘BT‘. 121 tmp_num = s_sid-low. 122 WHILE tmp_num <= s_sid-high. 123 gs_stock_id-stock_id = tmp_num. 124 INSERT gs_stock_id INTO TABLE gt_stock_id. 125 tmp_num = tmp_num + 1. 126 ENDWHILE. 127 ENDIF. 128 ENDLOOP. 129 ENDFORM. 130 131 "获得新浪财经网络API数据 132 FORM get_live_stock_info. 133 DATA: lo_abap_conv TYPE REF TO cl_abap_conv_in_ce, 134 li_http_client TYPE REF TO if_http_client, 135 lv_stock_pub_url TYPE string VALUE ‘http://hq.sinajs.cn/list=‘, 136 abap_encoding TYPE abap_encoding, 137 l_str TYPE string, 138 l_xstr TYPE xstring, 139 l_tmp TYPE string, 140 l_url TYPE string. 141 DATA: BEGIN OF wa_num, 142 num(6) TYPE c, 143 END OF wa_num. 144 DATA: BEGIN OF wa_lin, 145 str TYPE string, 146 END OF wa_lin. 147 DATA: it_num LIKE TABLE OF wa_num, 148 it_restab LIKE TABLE OF wa_num, 149 it_res LIKE TABLE OF wa_lin. 150 IF gt_stock_id[] IS INITIAL. 151 RETURN. 152 ENDIF. 153 CLEAR:it_res. 154 REFRESH:it_res. 155 LOOP AT gt_stock_id INTO gs_stock_id. 156 CLEAR l_tmp. 157 IF gs_stock_id-stock_id(1) < 6. 158 CONCATENATE ‘sz‘ gs_stock_id-stock_id INTO l_tmp. 159 ENDIF. 160 IF gs_stock_id-stock_id(1) = ‘6‘. 161 CONCATENATE ‘sh‘ gs_stock_id-stock_id INTO l_tmp. 162 ENDIF. 163 CONCATENATE l_str l_tmp INTO l_str SEPARATED BY ‘,‘. 164 ENDLOOP. 165 IF l_str(1) = ‘,‘. 166 SHIFT l_str LEFT DELETING LEADING ‘,‘. 167 ENDIF. 168 CONCATENATE lv_stock_pub_url l_str INTO l_url. 169 CALL METHOD cl_http_client=>create_by_url 170 EXPORTING 171 url = l_url 172 IMPORTING 173 client = li_http_client 174 EXCEPTIONS 175 argument_not_found = 1 176 plugin_not_active = 2 177 internal_error = 3 178 OTHERS = 4. 179 CALL METHOD li_http_client->request->set_header_field 180 EXPORTING 181 name = ‘Content-Type‘ 182 value = ‘text/html;charset=utf-8‘. " utf-8 183 CALL METHOD li_http_client->send 184 EXCEPTIONS 185 http_communication_failure = 1 186 http_invalid_state = 2. 187 CALL METHOD li_http_client->receive 188 EXCEPTIONS 189 http_communication_failure = 1 190 http_invalid_state = 2 191 http_processing_failed = 3. 192 l_xstr = li_http_client->response->get_data( ). 193 IF sy-subrc = 0 . 194 li_http_client->close( ). 195 ENDIF. 196 CALL METHOD cl_abap_conv_in_ce=>create 197 EXPORTING 198 input = l_xstr 199 encoding = ‘8400‘ 200 replacement = ‘?‘ 201 ignore_cerr = abap_true 202 RECEIVING 203 conv = lo_abap_conv. 204 TRY. 205 CALL METHOD lo_abap_conv->read 206 IMPORTING 207 data = l_str. 208 CATCH cx_sy_conversion_codepage. 209 CATCH cx_sy_codepage_converter_init. 210 CATCH cx_parameter_invalid_type. 211 CATCH cx_parameter_invalid_range. 212 ENDTRY. 213 REFRESH gt_stock_pub_list . 214 SPLIT l_str AT cl_abap_char_utilities=>newline 215 INTO TABLE gt_stock_pub_list. 216 * LOOP AT gt_stock_pub_list INTO l_str. 217 * WRITE: / l_str. 218 * ENDLOOP. 219 220 ENDFORM. 221 222 "创建OOALV表格表头等信息 223 FORM build_alv. 224 DEFINE init_alv. 225 go_column->set_long_text( &1 ). 226 go_column->set_medium_text( &1 ). 227 go_column->set_short_text( &1 ). 228 END-OF-DEFINITION. 229 230 TRY. 231 go_columns = go_table->get_columns( ). 232 go_column ?= go_columns->get_column(‘SID‘). 233 init_alv ‘股票号码‘. 234 go_column ?= go_columns->get_column(‘SNAME‘). 235 init_alv ‘股票名称‘. 236 go_column ?= go_columns->get_column(‘TOPEN‘). 237 init_alv ‘开盘价‘. 238 go_column ?= go_columns->get_column(‘YCLOSE‘). 239 init_alv ‘昨收价‘. 240 go_column ?= go_columns->get_column(‘CPRICE‘). 241 init_alv ‘现价‘. 242 go_column ?= go_columns->get_column(‘THIGH‘). 243 init_alv ‘最高价‘ . 244 go_column ?= go_columns->get_column(‘TLOW‘). 245 init_alv ‘最低价‘ . 246 go_column ?= go_columns->get_column(‘BUY‘). 247 init_alv ‘买一价‘. 248 go_column ?= go_columns->get_column(‘SALE‘). 249 init_alv ‘卖一价‘. 250 go_column ?= go_columns->get_column(‘NUMBER‘). 251 init_alv ‘成交数量‘. 252 go_column ?= go_columns->get_column(‘AMOUNT‘). 253 init_alv ‘成交金额‘. 254 go_column ?= go_columns->get_column(‘BUY11‘). 255 init_alv ‘买一数量‘. 256 go_column ?= go_columns->get_column(‘BUY12‘). 257 init_alv ‘买一金额‘. 258 go_column ?= go_columns->get_column(‘BUY21‘). 259 init_alv ‘买二数量‘. 260 go_column ?= go_columns->get_column(‘BUY22‘). 261 init_alv ‘买二金额‘. 262 go_column ?= go_columns->get_column(‘BUY31‘). 263 init_alv ‘买三数量‘. 264 go_column ?= go_columns->get_column(‘BUY32‘). 265 init_alv ‘买三金额‘. 266 go_column ?= go_columns->get_column(‘BUY41‘). 267 init_alv ‘买四数量‘. 268 go_column ?= go_columns->get_column(‘BUY42‘). 269 init_alv ‘买四金额‘. 270 go_column ?= go_columns->get_column(‘BUY51‘). 271 init_alv ‘买五数量‘. 272 go_column ?= go_columns->get_column(‘BUY52‘). 273 init_alv ‘买五金额‘. 274 go_column ?= go_columns->get_column(‘SALE11‘). 275 init_alv ‘卖一数量‘. 276 go_column ?= go_columns->get_column(‘SALE12‘). 277 init_alv ‘卖一金额‘. 278 go_column ?= go_columns->get_column(‘SALE21‘). 279 init_alv ‘卖二数量‘. 280 go_column ?= go_columns->get_column(‘SALE22‘). 281 init_alv ‘卖二金额‘. 282 go_column ?= go_columns->get_column(‘SALE31‘). 283 init_alv ‘卖三数量‘. 284 go_column ?= go_columns->get_column(‘SALE32‘). 285 init_alv ‘卖三金额‘. 286 go_column ?= go_columns->get_column(‘SALE41‘). 287 init_alv ‘卖四数量‘. 288 go_column ?= go_columns->get_column(‘SALE42‘). 289 init_alv ‘卖四金额‘. 290 go_column ?= go_columns->get_column(‘SALE51‘). 291 init_alv ‘卖五数量‘. 292 go_column ?= go_columns->get_column(‘SALE52‘). 293 init_alv ‘卖五金额‘. 294 go_column ?= go_columns->get_column(‘DATE‘). 295 init_alv ‘日期‘ . 296 go_column ?= go_columns->get_column(‘TIME‘). 297 init_alv ‘时间‘. 298 299 300 CATCH cx_salv_not_found. "#EC NO_HANDLER 301 ENDTRY. 302 303 ENDFORM . 304 305 "将获得的API股票信息录入内表,并显示在ALV中 306 FORM fill_alv. 307 DATA lt_res TYPE TABLE OF string. 308 DATA l_str TYPE string. 309 DATA l_str_field TYPE string. 310 FIELD-SYMBOLS: <fs_value> TYPE any. 311 312 FIELD-SYMBOLS: <fs_stock> TYPE ty_stock_live. 313 DATA: ls_color TYPE lvc_s_scol. 314 315 REFRESH lt_res. 316 REFRESH gt_stock. 317 LOOP AT gt_stock_pub_list INTO l_str. 318 IF strlen( l_str ) < 30. 319 CONTINUE. 320 ENDIF. 321 SPLIT l_str AT ‘,‘ INTO TABLE lt_res. 322 READ TABLE lt_res INDEX 1 INTO l_str_field. 323 gs_stock-sid = l_str_field+13(6). 324 gs_stock-sname = l_str_field+21. 325 326 idx = 2. 327 WHILE idx < 33. 328 READ TABLE lt_res INDEX idx INTO l_str_field. 329 idx = idx + 1. 330 ASSIGN COMPONENT idx OF STRUCTURE gs_stock TO <fs_value>. 331 <fs_value> = l_str_field. 332 ENDWHILE. 333 334 INSERT gs_stock INTO TABLE gt_stock. 335 ENDLOOP. 336 337 "为内表设置一个特殊的列, COLOR,然后为该列赋值即可控制每一行的颜色 338 TRY. 339 CALL METHOD go_columns->set_color_column 340 EXPORTING 341 value = ‘COLOR‘. 342 CATCH cx_salv_data_error. 343 ENDTRY. 344 345 LOOP AT gt_stock ASSIGNING <fs_stock>. 346 REFRESH <fs_stock>-color. 347 IF <fs_stock>-cprice > <fs_stock>-topen. 348 ls_color-color-col = 6. "上涨,红色 349 ls_color-color-int = 1. 350 ls_color-color-inv = 0. 351 APPEND ls_color TO <fs_stock>-color. 352 ELSE. 353 IF <fs_stock>-cprice < <fs_stock>-topen. 354 ls_color-color-col = 5." 下跌,绿色 355 ls_color-color-int = 0. 356 ls_color-color-inv = 0. 357 APPEND ls_color TO <fs_stock>-color. 358 ENDIF. 359 ENDIF. 360 ENDLOOP. 361 362 ENDFORM. 363 364 "显示股票详细信息K线图 365 FORM show_stock_details USING p_row TYPE i 366 p_column TYPE lvc_fname. 367 368 DATA: l_row TYPE char10. 369 WRITE p_row TO l_row LEFT-JUSTIFIED. 370 371 READ TABLE gt_stock INDEX l_row INTO gs_stock. 372 IF sy-subrc = 0. 373 gv_stock_id = gs_stock-sid. 374 "调用屏幕,显示图片 375 CALL SCREEN 100. 376 ENDIF. 377 378 ENDFORM. 379 380 "刷新,暂时不用 381 FORM handle_user_command USING p_function TYPE salv_de_function. 382 CASE p_function. 383 WHEN ‘REFRESH‘. 384 go_table->refresh( ). 385 ENDCASE. 386 ENDFORM. 387 388 "调用上述方法,显示ALV结果 389 FORM show_grid. 390 391 392 PERFORM get_input_stock_id. 393 PERFORM get_live_stock_info. 394 395 TRY. 396 "调用类方法创建类对象 397 CALL METHOD cl_salv_table=>factory 398 IMPORTING 399 r_salv_table = go_table 400 CHANGING 401 t_table = gt_stock. 402 CATCH cx_salv_msg. 403 ENDTRY. 404 405 "调用类方法显示SALV 406 go_functions = go_table->get_functions( ). 407 go_functions->set_all( abap_true ). 408 409 410 PERFORM build_alv. 411 PERFORM fill_alv. 412 413 414 TRY. 415 "设置热点列 416 "go_columns = go_table->get_columns( ). 417 "go_column ?= go_columns->get_column( ‘SID‘). 418 "go_column->set_cell_type( if_salv_c_cell_type=>hotspot ). 419 420 "=====获取事件对象 421 DATA: lr_event TYPE REF TO cl_salv_events_table. 422 lr_event = go_table->get_event( ). 423 424 "=====事件注册 425 DATA: mo_handle_event TYPE REF TO zcl_handle_events. 426 CREATE OBJECT mo_handle_event. 427 SET HANDLER mo_handle_event->on_double_click FOR lr_event. 428 CATCH cx_salv_not_found. 429 ENDTRY. 430 431 go_table->display( ). 432 433 ENDFORM. 434 435 "获得新浪财经API的个股K线图 436 FORM show_picture USING i_stock_id. 437 438 DATA: l_tmp TYPE string. 439 440 CLEAR l_tmp. 441 IF i_stock_id(1) < 6. 442 CONCATENATE ‘http://image.sinajs.cn/newchart/daily/n/‘ 443 ‘sz‘ i_stock_id ‘.gif‘ INTO l_tmp. 444 ENDIF. 445 IF i_stock_id(1) = ‘6‘. 446 CONCATENATE ‘http://image.sinajs.cn/newchart/daily/n/‘ 447 ‘sh‘ i_stock_id ‘.gif‘ INTO l_tmp. 448 ENDIF. 449 gv_url = l_tmp. 450 451 "创建容器 452 CREATE OBJECT go_dock 453 EXPORTING 454 repid = sy-repid 455 dynnr = sy-dynnr 456 side = go_dock->dock_at_left 457 extension = 620. 458 IF sy-subrc NE 0 . 459 "Instantiate container Error! 460 ENDIF. 461 462 "创建图片 463 IF go_dock IS NOT INITIAL. 464 CREATE OBJECT go_picture 465 EXPORTING 466 parent = go_dock 467 EXCEPTIONS 468 OTHERS = 1. 469 IF sy-subrc NE 0 . 470 "Instantiate Picture Error! 471 ENDIF. 472 ENDIF. 473 474 "获取URL 475 CALL FUNCTION ‘DP_CREATE_URL‘ 476 EXPORTING 477 type = ‘JPG/GIF‘ 478 subtype = cndp_sap_tab_unknown 479 TABLES 480 data = gt_data 481 CHANGING 482 url = gv_url 483 EXCEPTIONS 484 dp_invalid_parameter = 1 485 dp_error_put_table = 2 486 dp_error_general = 3 487 OTHERS = 4. 488 IF sy-subrc <> 0. 489 "Get URL Error! 490 ENDIF. 491 492 "显示图片 493 CALL METHOD go_picture->load_picture_from_url 494 EXPORTING 495 url = gv_url. 496 497 * CALL METHOD go_picture->set_display_mode 498 * EXPORTING 499 * display_mode = 3. 500 501 502 ENDFORM. 503 504 505 506 "定时器设定 507 CLASS lcl_event_handler DEFINITION. 508 PUBLIC SECTION. 509 METHODS: 510 handle_timer FOR EVENT finished OF cl_gui_timer. 511 ENDCLASS. 512 513 CLASS lcl_event_handler IMPLEMENTATION. 514 METHOD handle_timer. 515 "重新连接API,获取最新数据 516 PERFORM get_live_stock_info. 517 "填充内表 518 PERFORM fill_alv. 519 "刷新ALV组件 520 go_table->refresh( ). 521 522 CALL METHOD go_timer->run 523 EXCEPTIONS 524 OTHERS = 9. 525 ENDMETHOD. 526 ENDCLASS. 527 528 529 FORM init_timer. 530 IF go_timer IS NOT BOUND. 531 CREATE OBJECT go_timer 532 EXCEPTIONS 533 OTHERS = 9. 534 CREATE OBJECT go_evt_hndl. 535 ENDIF. 536 SET HANDLER go_evt_hndl->handle_timer FOR go_timer. 537 "10秒钟刷新一次 538 go_timer->interval = 10. 539 CALL METHOD go_timer->run 540 EXCEPTIONS 541 OTHERS = 9. 542 ENDFORM. 543 544 "屏幕100是为K线图片专用的 545 MODULE status_0100 OUTPUT. 546 SET PF-STATUS ‘ALV1‘. 547 "屏幕显示后,调用图片显示 548 PERFORM show_picture USING gv_stock_id. 549 ENDMODULE. 550 551 "屏幕100是为K线图片专用的 552 MODULE user_command_0100 INPUT. 553 CASE sy-ucomm. 554 WHEN ‘BACK‘. 555 "返回前一个界面,先删除容器组件,再删除图片组件 556 CALL METHOD go_picture->free( ). 557 CALL METHOD go_dock->free( ). 558 "返回ALV屏幕 559 LEAVE TO SCREEN 0. 560 WHEN OTHERS. 561 ENDCASE. 562 ENDMODULE. " USER_COMMAND_0100 INPUT
标签:har 参考 版本 mount eth url ring for nic
原文地址:http://www.cnblogs.com/techtalk/p/7820530.html