开 发说明:在CBO的程序开发过程中,需要为Table准备大量的测试数据,手动录入效率低,不专业,我们可以采用其他的高级编辑工具(例如:EXCEL, EditPlus)按照Table数据存储结构准备好数据,最后保存为ASC的文本文件,通过执行下面开发的程序,下面的程序执行的功能就是把编辑好的文 本文件上的数据上载到SAP对应的Table中,小程序非常实用,也适用于我们大量更新Table数据时使用,ABAPer们的手头必备啊!
开发技术:
1.文本文件上载技术
2.动态程序代码生成技术
3.ABAP动态程序执行技术
4.TXT文本文件对应用Table字段编辑技术
注意事项:
文件文件编辑过程中时间日期格式为 20060201 120000 表示 2006.02.01 12:00:00
文本文件中不能包含除字符,数字之外的其他特殊符号。假如Table中对应的字段数据为空时,在编辑数据时也对应该列为空。
程序代码:如下
*******************************************************************
* (Copyright @2006 Mysingle Digital System Co.Ltd.
* All Rights Reserved|Confidential)
* System Module : ABAP CBO
* Program Description : Table Upload & Download
* Developer : ZOU XIN
* Develop Date : 2006.01.01
* Use Status : Release 1.0
*******************************************************************
REPORT z_cbo_abap_01 NO STANDARD PAGE HEADING.
TABLES : dd03l,dd02t.
DATA: BEGIN OF tab OCCURS 10,
fcode(4),
END OF tab.
DATA : BEGIN OF itab OCCURS 0,
chk(1) TYPE c, " check box
tabname LIKE dd03l-tabname, " Table name
fieldname LIKE dd03l-fieldname, " Feld Name
position LIKE dd03l-position, " Table
keyflag LIKE dd03l-keyflag, " Primary Key
datatype LIKE dd03l-datatype, " Data Type
intlen LIKE dd03l-leng,
END OF itab.
DATA itab1 LIKE itab OCCURS 0 WITH HEADER LINE.
DATA : BEGIN OF uptab OCCURS 0,
text(72) TYPE c,
END OF uptab.
DATA: BEGIN OF error_message,
line1(72),
line2(72),
line3(72),
END OF error_message.
DATA : cnt1(8) TYPE c,
fcode LIKE sy-ucomm,
changed LIKE s38e-buf_varied,
prog(8) TYPE c,
msg(120) TYPE c,
er_include LIKE sy-repid,
er_line LIKE sy-index,
er_off LIKE sy-tabix,
er_subrc LIKE sy-subrc.
* 程序录入界面
PARAMETERS : tabname LIKE dd03l-tabname DEFAULT 'ZP023'.
START-OF-SELECTION.
*上载Table字段分析
SELECT SINGLE * FROM dd02t WHERE tabname = tabname.
SET PF-STATUS 'ZOUXIN'.
SELECT * INTO CORRESPONDING FIELDS OF TABLE itab FROM dd03l
WHERE tabname = tabname
AND as4local = 'A'
ORDER BY position.
itab-chk = 'X'.
MODIFY itab INDEX 1 TRANSPORTING chk .
*动态上载程序代码生成函数
PERFORM generate_upload_code.
*ABAP程序代码编辑器调用
PERFORM edit_generator_code.
AT USER-COMMAND .
IF sy-ucomm = 'EDIT'.
PERFORM edit_generator_code.
ELSEIF sy-ucomm = 'EXEC'.
GENERATE SUBROUTINE POOL uptab NAME prog MESSAGE msg.
*程序代码语法检测
PERFORM chcek_syntax_error.
*动态程序代码执行
PERFORM dyn1 IN PROGRAM (prog).
ENDIF.
*&---------------------------------------------------------------*
*& Form generate_upload_code
*&---------------------------------------------------------------*
FORM generate_upload_code.
REFRESH uptab.
uptab-text = 'REPORT ZUP19800526.'.
APPEND uptab.
CONCATENATE 'TABLES :' tabname '.' INTO uptab-text
SEPARATED BY space.
APPEND uptab.
uptab-text = 'DATA : BEGIN OF UPTAB OCCURS 0,'.
APPEND uptab.
LOOP AT itab WHERE chk = ''.
CLEAR uptab.
CONCATENATE ' ' itab-fieldname '(' itab-intlen ')' ' TYPE C ,'
INTO uptab-text+10(80).
APPEND uptab.
ENDLOOP.
uptab-text+6(82) = 'END OF UPTAB.'.
APPEND uptab.
uptab-text = 'DATA : BEGIN OF RESULT OCCURS 0.'.
APPEND uptab.
CLEAR uptab.
CONCATENATE 'INCLUDE STRUCTURE' itab-tabname '.'
INTO uptab-text+10(80) SEPARATED BY space.
APPEND uptab.
uptab-text = 'DATA : END OF RESULT.'.
APPEND uptab.
uptab-text = 'FORM DYN1.'.
APPEND uptab.
CLEAR uptab.
uptab-text+2(88) = 'CALL FUNCTION ''UPLOAD'''.
APPEND uptab.
CLEAR uptab.
uptab-text+4(86) = 'EXPORTING'.
APPEND uptab.
CLEAR uptab.
uptab-text+6(84) = 'FILENAME = ''C:\'''.
APPEND uptab.
uptab-text+6(84) = 'FILETYPE = ''DAT'''.
APPEND uptab.
uptab-text+4(86) = 'TABLES'.
APPEND uptab.
CLEAR uptab.
uptab-text+6(84) = 'DATA_TAB = UPTAB.'.
APPEND uptab.
uptab-text+2(88) = 'LOOP AT UPTAB.'.
APPEND uptab.
CLEAR uptab.
uptab-text+4(86) = 'CLEAR RESULT.'.
APPEND uptab.
uptab-text+4(86) = 'MOVE-CORRESPONDING UPTAB TO RESULT.'.
APPEND uptab.
uptab-text+4(86) = 'APPEND RESULT.'.
APPEND uptab.
uptab-text+2(88) = 'ENDLOOP.'.
APPEND uptab.
CLEAR uptab.
CONCATENATE 'INSERT' itab-tabname 'FROM TABLE RESULT.'
INTO uptab-text+2(88) SEPARATED BY space.
APPEND uptab.
uptab-text = 'ENDFORM.' .
APPEND uptab.
CLEAR uptab.
ENDFORM. " generate_upload_code
*&---------------------------------------------------------------*
*& Form edit_generator_code
*&---------------------------------------------------------------*
FORM edit_generator_code.
CALL FUNCTION 'EDITOR_APPLICATION'
EXPORTING
application = 'BF'
display = ' '
name = 'Source Code.....'
IMPORTING
fcode = fcode
changed = changed
TABLES
content = uptab.
LOOP AT uptab.
WRITE:/1 uptab-text.
ENDLOOP.
ENDFORM. " PRINT_GENERATOR_CODE
*&---------------------------------------------------------------*
*& Form chcek_syntax_error
*&---------------------------------------------------------------*
FORM chcek_syntax_error.
CALL FUNCTION 'EDITOR_SYNTAX_CHECK'
EXPORTING
i_global_check = ' '
i_global_program = ' '
i_program = 'ZUP19800526'
i_r2_check = ' '
i_r2_destination = ' '
i_trdir = ' '
IMPORTING
o_error_include = er_include
o_error_line = er_line
o_error_message = error_message
o_error_offset = er_off
o_error_subrc = er_subrc
TABLES
i_source = uptab.
IF er_subrc <> 0.
er_line = er_line - 2.
WRITE:/1 'Error Line : ',er_line.
WRITE:/1 error_message-line1,error_message-line2,
error_message-line3.
STOP.
ENDIF.
ENDFORM. " chcek_syntax_error
1.程序初始界面,输入Upload的table名.
2.程序运行第一界面,显示程序自动生成的上载程序代码
3.程序运行第二界面,确认程序自动生成的上载程序代码,执行
4.程序运行第三界面中小界面,程序执行提示输入上在数据具体路径
4.OK,数据上载成功
补充1:上载数据文件格式,*.TXT DAT格式文件,俗称纯文本文件
补充2:SAP Table格式,注意不要理会Mandt Client字段,其他字段数据对应