直接上代码
代码语言:javascript复制*----------------------------------------------------------------------*
* 剪贴板数据到内表,根据制表符分割
* 如果有数据类型错误,比如内表是数值型但是剪贴板里面是字母,
* 则弹出W消息,并忽略这条数据
*----------------------------------------------------------------------*
FORM cliptoitab TABLES itab.
DATA: htab TYPE cVALUE cl_abap_char_utilities=>horizontal_tab .
DATA: lt_clip TYPETABLE OF char2048 WITH HEADER LINE ,
lt_fld TYPE TABLE OF char2048 WITH HEADER LINE.
DATA: cxroot TYPE REFTO cx_root,
excmsgTYPE string.
FIELD-SYMBOLS:<fs_fld> ,
<fs_tab> .
ASSIGN itab TO<fs_tab> .
CALL METHODcl_gui_frontend_services=>clipboard_import
IMPORTING
data = lt_clip[]
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF sy-subrc <>0.
MESSAGE ID sy-msgidTYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1sy-msgv2 sy-msgv3 sy-msgv4 .
ELSE.
CALL METHODcl_gui_cfw=>flush .
LOOP AT lt_clip.
CLEAR lt_fld[].
SPLIT lt_clip AThtab INTO TABLE lt_fld.
LOOP AT lt_fld.
ASSIGNCOMPONENT sy-tabix OF STRUCTURE <fs_tab> TO <fs_fld>.
CHECK sy-subrc= 0.
TRY .
lt_fld =translate( val = lt_fld from = |r| to = '' ).
lt_fld =translate( val = lt_fld from = |n| to = '' ).
lt_fld =translate( val = lt_fld from = |"| to = '' ).
<fs_fld> = lt_fld.
CATCH cx_root INTO cxroot.
excmsg =cxroot->get_text( ).
ENDTRY.
ENDLOOP.
APPEND itab .
CLEAR itab.
ENDLOOP.
IF excmsg IS NOTINITIAL.
MESSAGE w000(oo)WITH '数据转换有错误发生,已经忽略'.
ENDIF.
ENDIF.
ENDFORM.
使用:
先在Excel,或者WORD表格,或者文本文件复制数据。总之只要保证数据是Tab分隔符就可以了。
然后在在程序执行PERFORM cliptoitab.就可以了。