ABAP根据输入的表名动态生成上传TXT数据的代码详解编程语言

开发技术:
1.文本文件上载技术
2.动态程序代码生成技术
3.ABAP动态程序执行技术
4.TXT文本文件对应用Table字段编辑技术
 
注意事项:
文件文件编辑过程中时间日期格式为 20060201 120000 表示 2006.02.01 12:00:00
文本文件中不能包含除字符,数字之外的其他特殊符号。假如Table中对应的字段数据为空时,在编辑数据时也对应该列为空。
 
程序代码:如下
*&——————————————————————*
*& Report  Z_CBO_ABAP_TXT
*&
*&——————————————————————*
* 程序说明  Table数据Upload 程序 只能导入TXT
* 模块:版本
* 参考文档
* 应用顾问
* 程序开发 
* 开发日期 
*&——————————————————————*
*&更改记录
*&日期          更改人     模块:版本    功能说明书
*&
*&
*&——————————————————————*
REPORT Z_CBO_ABAP.

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,
        text(100) 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.

*——————————————————————*
*    START-OF-SELECTION                                                *
*——————————————————————-*

START-OF-SELECTION.
  IF tabname IS INITIAL.
    MESSAGE ‘请输入数据表名称!’ TYPE ‘S’ DISPLAY LIKE ‘E’.
    EXIT.
  ENDIF.

*上载Table字段分析
  SELECT SINGLE * FROM dd02t WHERE tabname = tabname.
  if sy-subrc <> 0.
    MESSAGE ‘请输入正确的数据表名称!’ TYPE ‘S’ DISPLAY LIKE ‘E’.
    EXIT.
  endif.
  SET PF-STATUS ‘CBO’.
  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.
    IF MSG IS NOT INITIAL.
      MESSAGE MSG TYPE ‘S’ DISPLAY LIKE ‘E’.
      EXIT.
    ENDIF.
*程序代码语法检测
    PERFORM chcek_syntax_error.
*动态程序代码执行
    PERFORM dyn1 IN PROGRAM (prog).
  elseif ( sy-ucomm = ‘EXIT’ or sy-ucomm = ‘BACK’ or sy-ucomm = ‘CANC’ ).
    EXIT.
  ENDIF.

*&—————————————————————*
*&      Form  generate_upload_code
*&—————————————————————*
FORM generate_upload_code.
  REFRESH uptab.
  uptab-text = ‘REPORT ZUPLOAD_TABLE.’.
  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 ‘MODIFY’  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   = SY-CALLD
            i_global_program = SY-REPID
            i_program        = ‘ZUPLOAD_TABLE’
*            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

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/20039.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论