ABAP:从例子学习ABAP

余生颓废 提交于 2020-11-04 05:22:14

1、插入内表行:

*插入内表行:
DATA: BEGIN OF man, name(20) TYPE c, high TYPE p DECIMALS 2, weight TYPE p DECIMALS 2, END OF man. DATA: man1 LIKE TABLE OF man. man-name = '张参'. man-high = '1.68'. man-weight = 120. INSERT man INTO TABLE man1. man-name = '刘志'. man-high = '1.78'. man-weight = 160. INSERT man INTO TABLE man1. LOOP AT man1 INTO man. WRITE: / man-name,man-high,man-weight. ENDLOOP. ULINE. man-name = '钱华'. man-high = '2.22'. man-weight = 220. INSERT man INTO man1 INDEX 3. LOOP AT man1 INTO man. WRITE: / man-name,man-high,man-weight. ENDLOOP. ULINE. man-name = '钱华'. man-high = '2.22'. man-weight = 220. INSERT man INTO man1 INDEX 1. LOOP AT man1 INTO man. WRITE: / man-name,man-high,man-weight. ENDLOOP.

2、读取文件

2.1、使用upload函数

*读放文件
DATA: BEGIN OF man, name(20) TYPE c, high TYPE p DECIMALS 2, weight TYPE p DECIMALS 2, END OF man. DATA: man1 LIKE TABLE OF man. CALL FUNCTION 'UPLOAD' EXPORTING codepage = 'test' filename = 'd:\temp\testa.txt' filetype = 'dat' item = '读取文件' TABLES data_tab = man1 EXCEPTIONS invalid_filesize = 1 invalid_table_width = 2 invalid_type = 3 no_batch = 4 unknown_error = 5 gui_refuse_filetransfer = 6 OTHERS = 7. IF sy-subrc <> 0. * message id sy-msgid type sy-msgty number sy-msgno * with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. LOOP AT man1 INTO man. WRITE: / man-name,man-high,man-weight. ENDLOOP.

 2.2、使用WS_UPLOAD函数

*读取文件
DATA: BEGIN OF man,
      name(20) TYPE c,
      high     TYPE p DECIMALS 2,
      weight   TYPE p DECIMALS 2,
END OF man.

DATA: man1 LIKE TABLE OF man.

CALL FUNCTION 'WS_UPLOAD'
  EXPORTING
    codepage                = 'test'
    filename                = 'd:\temp\testa.txt'
    filetype                = 'DAT'
  TABLES
    data_tab                = man1
  EXCEPTIONS
    invalid_filesize        = 1
    invalid_table_width     = 2
    invalid_type            = 3
    no_batch                = 4
    unknown_error           = 5
    gui_refuse_filetransfer = 6
    OTHERS                  = 7.
IF sy-subrc <> 0.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

LOOP AT man1 INTO man.
  WRITE: / man-name,man-high,man-weight.
ENDLOOP.

2.3、使用GUI_UPLOAD函数

*直接读取文件
DATA: BEGIN OF man,
      name(20) TYPE c,
      high     TYPE p DECIMALS 2,
      weight   TYPE p DECIMALS 2,
END OF man.

DATA: man1 LIKE TABLE OF man.

CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename                      = 'd:\temp\testa.txt'
   FILETYPE                      = 'DAT'
*   HAS_FIELD_SEPARATOR           = ' '
*   HEADER_LENGTH                 = 0
*   READ_BY_LINE                  = 'X'
*   DAT_MODE                      = ' '
*   CODEPAGE                      = ' '
*   IGNORE_CERR                   = ABAP_TRUE
*   REPLACEMENT                   = '#'
*   CHECK_BOM                     = ' '
*   VIRUS_SCAN_PROFILE            =
*   NO_AUTH_CHECK                 = ' '
* IMPORTING
*   FILELENGTH                    =
*   HEADER                        =
  tables
    data_tab                      = man1
* CHANGING
*   ISSCANPERFORMED               = ' '
* EXCEPTIONS
*   FILE_OPEN_ERROR               = 1
*   FILE_READ_ERROR               = 2
*   NO_BATCH                      = 3
*   GUI_REFUSE_FILETRANSFER       = 4
*   INVALID_TYPE                  = 5
*   NO_AUTHORITY                  = 6
*   UNKNOWN_ERROR                 = 7
*   BAD_DATA_FORMAT               = 8
*   HEADER_NOT_ALLOWED            = 9
*   SEPARATOR_NOT_ALLOWED         = 10
*   HEADER_TOO_LONG               = 11
*   UNKNOWN_DP_ERROR              = 12
*   ACCESS_DENIED                 = 13
*   DP_OUT_OF_MEMORY              = 14
*   DISK_FULL                     = 15
*   DP_TIMEOUT                    = 16
*   OTHERS                        = 17
          .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.


LOOP AT man1 INTO man.
  WRITE: / man-name,man-high,man-weight.
ENDLOOP.

2.4、读取文件小例子

DATA file_table TYPE file_table. "引用FILE_TABLE对象

DATA: lt_file_names TYPE filetable,
lwa_file_name LIKE LINE OF lt_file_names,
lv_subrc TYPE i.

SELECT-OPTIONS: i_file FOR file_table NO INTERVALS.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR i_file-low.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
*    WINDOW_TITLE            =
*    DEFAULT_EXTENSION     =
*    DEFAULT_FILENAME        =
*    FILE_FILTER             =
initial_directory       = 'd:\temp'
multiselection          = 'X'
CHANGING
  file_table                      = lt_file_names[]
  rc                                 = lv_subrc  "打开文件的数量
*    USER_ACTION             =
EXCEPTIONS
  file_open_dialog_failed = 1
  cntl_error                        = 2
  error_no_gui                    = 3
OTHERS                                 = 4.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

i_file-sign = 'I'.
i_file-option = 'EQ'.

LOOP AT lt_file_names INTO lwa_file_name.
  i_file-low = lwa_file_name.
  APPEND i_file.
ENDLOOP.

START-OF-SELECTION.
  WRITE:'打开文件的数量:',lv_subrc LEFT-JUSTIFIED.
  WRITE: / '打开文件的数量:',LINES( i_file ) LEFT-JUSTIFIED.
LOOP AT i_file.
 WRITE: / i_file-low.
ENDLOOP.

3、保存文件

*保存文件
DATA: BEGIN OF man, name(20) TYPE c, high TYPE p DECIMALS 2, weight TYPE p DECIMALS 2, END OF man. DATA: man1 LIKE TABLE OF man. DATA: name TYPE rlgrap-filename, typa TYPE rlgrap-filetype. man-name = '张参'. man-high = '1.68'. man-weight = 120. INSERT man INTO TABLE man1. man-name = '刘志'. man-high = '1.78'. man-weight = 160. INSERT man INTO TABLE man1. man-name = '钱华'. man-high = '2.22'. man-weight = 220. INSERT man INTO man1 INDEX 2. name = 'd:\temp\testad.txt'. typa = 'dat'. CALL FUNCTION 'DOWNLOAD' EXPORTING codepage = 'testa' filename = name filetype = typa item = '文件测试' TABLES data_tab = man1 EXCEPTIONS invalid_filesize = 1 invalid_table_width = 2 invalid_type = 3 no_batch = 4 unknown_error = 5 gui_refuse_filetransfer =6 OTHERS = 7. IF sy-subrc <> 0. * message id sy-msgid type sy-msgty number sy-msgno * with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.

4、格式化输出

DATA wa LIKE spfli.
WRITE:/. WRITE:10'航班承运人',40'航班连接',60'国家代码',80'起飞城市',100'起飞机场'.

5、内表排序

*内表排序
DATA: BEGIN OF man, name(20) TYPE c, high TYPE p DECIMALS 2, weight TYPE p DECIMALS 2, END OF man. DATA: man1 LIKE hashed TABLE OF man with unique key name. man-name = '张参'. man-high = '1.68'. man-weight = 120. INSERT man INTO TABLE man1. man-name = '刘志'. man-high = '1.78'. man-weight = 160. INSERT man INTO TABLE man1. man-name = '钱华'. man-high = '2.22'. man-weight = 220. INSERT man INTO table man1. LOOP AT man1 INTO man. WRITE: / man-name,man-high,man-weight. ENDLOOP. ULINE. sort man1 ascending by weight descending . skip. uline. LOOP AT man1 INTO man. WRITE: / man-name,man-high,man-weight. ENDLOOP.

6、消息

*消息
*--开始 *DATA: msg1(20) TYPE c VALUE '测试'. *DATA: msg2(20) TYPE c VALUE 'ABAP'. *DATA: msg3(20) TYPE c VALUE '消息!'. * *MESSAGE i006(ymess) WITH msg1 msg2 msg3. * *data:BEGIN OF man, * name(30) TYPE c, * high TYPE p DECIMALS 2, * weight TYPE p DECIMALS 2, *END OF man. * *FIELD-SYMBOLS <fsa> LIKE man. *DATA man1 LIKE man. * *man-name = '雨荷'. *man-high = '1.78'. *man-weight = 140. * *ASSIGN man TO <fsa>. *WRITE:/ <fsa>-name, * man-high, *<fsa>-weight. *--结束 *--开始 *DATA: msg1(20) TYPE c VALUE '测试'. *DATA: msg2(20) TYPE c VALUE 'ABAP'. *DATA: msg3(20) TYPE c VALUE '消息!'. * *MESSAGE i001(00) WITH msg1 msg2 msg3. * * *DATA: c1(2) TYPE c, * c2(2) TYPE c, * c3(2) TYPE c, * c4(2) TYPE c, * c5(20) TYPE c VALUE '91* 12*23*344445', * c9(2) TYPE c. * *c9 = '* '. *WRITE: / . *WRITE c5. *SPLIT c5 AT c9 INTO c1 c2 c3 c4 . *WRITE: / c1,c2,c3,c4. *--结束 *--开始 *DATA: msg1(20) TYPE c VALUE '测试'. *DATA: msg2(20) TYPE c VALUE 'ABAP'. *DATA: msg3(20) TYPE c VALUE '消息!'. * *MESSAGE i001(00) WITH msg1 msg2 msg3. * * *DATA: BEGIN OF man, * name(20) TYPE c, * high TYPE p DECIMALS 2, * weight TYPE p DECIMALS 2, *END OF man. * *DATA: man1 LIKE TABLE OF man WITH HEADER LINE, *man2 LIKE TABLE OF man. * *man-name = '张参'. *man-high = '1.68'. *man-weight = 120. *APPEND man TO man1. * *man-name = '刘志'. *man-high = '1.78'. *man-weight = 160. *APPEND man TO man1. * *MOVE man1[] TO man2. * * * WRITE: / man-name,man-high,man-weight. * * *LOOP AT man1. *write: / man1-name,man1-high,man1-weight. *ENDLOOP. *--结束

 7、读取数据表

**读取数据表
*定义工作区
DATA a_spfli TYPE spfli.
*定义内表,请注意有Header line
DATA ta_spfli TYPE TABLE OF spfli WITH HEADER LINE.
*分隔线
ULINE.
WRITE / '使用工作区'.
ULINE.
* 将数据表逐行转移至工作区,只读前5行
SELECT * INTO CORRESPONDING FIELDS OF
         a_spfli FROM spfli UP TO 5 ROWS.
  WRITE: / a_spfli-connid,a_spfli-carrid,a_spfli-cityfrom,
          a_spfli-cityto.
ENDSELECT.

ULINE.
WRITE / '使用内表'.
ULINE.
SELECT * INTO CORRESPONDING FIELDS OF
         TABLE ta_spfli FROM spfli UP TO 5 ROWS.
*从数据表读数据至内表,直接从内表输出
*如果TA_SPFLI没有定义Header line,会出错
LOOP AT ta_spfli.
  WRITE: / a_spfli-connid,a_spfli-carrid,a_spfli-cityfrom,
           a_spfli-cityto.
ENDLOOP.
ULINE.
WRITE / '将内表数据转移至工作区'.
ULINE.
LOOP AT ta_spfli INTO a_spfli.
  WRITE: / a_spfli-connid,a_spfli-carrid,a_spfli-cityfrom,
             a_spfli-cityto.
ENDLOOP.

8、内连接和外连接

*内连接和外连接
*定义结构
DATA: BEGIN  OF   arcd,
      carrid TYPE spfli-carrid,
      connid TYPE spfli-connid,
      fldate TYPE sflight-fldate,
      END OF arcd.
*工作区
DATA arcd1 LIKE arcd.
*关联数据表,读取前10条记录,写入工作区,并输出
SELECT spfli~carrid spfli~connid sflight~fldate INTO arcd1 FROM spfli
       INNER JOIN sflight ON spfli~carrid = sflight~carrid
       AND spfli~connid = sflight~connid UP TO 10 ROWS.
  WRITE: / arcd1-carrid,arcd1-connid,arcd1-fldate.
ENDSELECT.

9、使用package size 读取数据

**使用package size 读取数据
*定义内表,请注意有Header line
DATA wa_spfli TYPE TABLE OF spfli WITH HEADER LINE.
*分隔线
ULINE.
* 以每次读取5条记录,直到读取所有数据
SELECT * INTO TABLE wa_spfli FROM spfli PACKAGE SIZE 5.
*输出内表记录
  LOOP AT wa_spfli.
    WRITE: / wa_spfli-connid,wa_spfli-carrid,wa_spfli-cityfrom,
             wa_spfli-cityto.
  ENDLOOP.
*输出5条记录后输出一条横线
  ULINE.
ENDSELECT.

10、交互式列表

**交互式列表
START-OF-SELECTION.
  WRITE:'初如列表,sy-lsind = ' ,sy-lsind.

AT LINE-SELECTION.
  IF sy-lsind = 1.
    WRITE: '第二列表,sy-lsind =',sy-lsind.
  ENDIF.

AT LINE-SELECTION.
  IF sy-lsind = 2.
    WRITE: '第三列表,sy-lsind =',sy-lsind.
  ENDIF.

 11、列表颜色

**列表颜色
*数据库表spfli相关
TABLES spfli.
SKIP.

*输出表头,背景灰蓝
FORMAT COLOR COL_HEADING.
ULINE AT /(91).

WRITE: / sy-vline,(15) '航线承运人', sy-vline , (15) '航班连接', sy-vline,
       (15) '国家代码', sy-vline,(15) '起飞城市', sy-vline,
       (15) '起飞机场',sy-vline.
ULINE AT /(91).
FORMAT COLOR OFF.
*输出内容,输出关键值为‘LH’为红色
SELECT * FROM spfli.
  IF spfli-carrid = 'LH'.
    FORMAT COLOR COL_NEGATIVE.
  ELSE.
    FORMAT COLOR OFF.
  ENDIF.
  WRITE: / sy-vline,(15) spfli-carrid,sy-vline,(15) spfli-connid,sy-vline,
         (15) spfli-countryto, sy-vline,(15) spfli-cityfrom,sy-vline,(15)
         spfli-airpfrom,sy-vline.
  ULINE AT /(91).
ENDSELECT.

12、输出热点

**输出热点
start-of-selection.
  write '请按'.
  format hotspot on color 5 inverse on.
  write '热点'.
  format hotspot off color off.
at line-selection.
  write 'welcome!'.

13、为列表定义工具条和菜单

*为列表定义工具条和菜单
START-OF-SELECTION.
* 设置已使用的状态条
  SET PF-STATUS 'STA1'.
  WRITE:'测试工具条按钮 '.

AT USER-COMMAND.
*当单击按钮时输出
  CASE sy-ucomm.
    WHEN '&NFO'.
      WRITE: '已选择!'.
    WHEN 'E'.
      leave to screen 0.
    when others.
      WRITE '1111'.
  ENDCASE.

14、在弹出式窗口中显示列表

*在弹出式窗口中显示列表
START-OF-SELECTION.
* 设置已使用的状态条
  SET PF-STATUS 'STA1'.
  WRITE:'测试工具条按钮 '.

AT USER-COMMAND.
*当单击按钮时输出
  CASE sy-ucomm.
    WHEN '&NFO'.
      WRITE: '已选择!'.
    WHEN 'E'.
      leave to screen 0.
    when others.
        IF sy-lsind = 1.
        SET PF-STATUS 'STA1'.
*定义子窗口并输出
        WINDOW STARTING AT 10 10 ENDING AT 30 20.
        WRITE '在子窗口输出!'.
        WRITE: '请注意!'.
      ENDIF.
  ENDCASE.

15、隐藏字段技术

**隐藏字段技术
*数据库表spfli相关
TABLES: spfli, sflight.

START-OF-SELECTION.
  SKIP.
*输出
  ULINE AT /(91).

  WRITE: / sy-vline,(15) '航线承运人', sy-vline , (15) '航班连接', sy-vline,
         (15) '国家代码', sy-vline,(15) '起飞城市', sy-vline,
         (15) '起飞机场',sy-vline.
  ULINE AT /(91).

*输出
  SELECT * FROM spfli
*    WHERE carrid = 'UA' AND connid = 3504
    .
    WRITE: / sy-vline,(15) spfli-carrid,sy-vline,
                      (15) spfli-connid,sy-vline,
                      (15) spfli-countryto,sy-vline,
                      (15) spfli-cityfrom,sy-vline,
                      (15) spfli-airpfrom, sy-vline.
*隐藏关键字
    HIDE: spfli-carrid,spfli-connid.
    ULINE AT /(91).
  ENDSELECT.

AT LINE-SELECTION.
*只输出第一页
  IF sy-lsind = 1.
    write : / 'spfli-carrid:',spfli-carrid.
    write : / 'spfli-connid:',spfli-connid.
    ULINE AT /(91).
    WRITE: / sy-vline,(15) '航线承运人', sy-vline , (15) '航班连接', sy-vline,
         (15) '国家代码', sy-vline,(15) '起飞城市', sy-vline,
         (15) '起飞机场',sy-vline.
    ULINE AT /(91).
*根据隐藏字段读取并打印从表
    SELECT * FROM sflight
      WHERE carrid = spfli-carrid AND connid = spfli-connid
      .
      WRITE: / sy-vline,(15) spfli-carrid,sy-vline,
                      (15) spfli-connid,sy-vline,
                      (15) spfli-countryto,sy-vline,
                      (15) spfli-cityfrom,sy-vline,
                      (15) spfli-airpfrom, sy-vline.
      ULINE AT /(91).
    ENDSELECT.
  ENDIF.

16、使用hide技术从列表读取行

**使用hide技术从列表读取行
*数据库表spfli相关
TABLES: spfli.
DATA: sela(1) TYPE c,
      num    TYPE i VALUE 0.

START-OF-SELECTION.
  SKIP.
*输出
  ULINE AT /(95).

  WRITE: / sy-vline,'',sy-vline,(15) '航线承运人', sy-vline , (15)
           '航班连接', sy-vline,(15) '国家代码', sy-vline,(15)
           '起飞城市', sy-vline,(15) '起飞机场',sy-vline.
  ULINE AT /(95).

*输出
  SELECT * FROM spfli.
    WRITE: / sy-vline,sela AS CHECKBOX,sy-vline,(15) spfli-carrid,
             sy-vline,(15) spfli-connid,sy-vline,(15) spfli-countryto,
             sy-vline,(15) spfli-cityfrom,sy-vline,
                      (15) spfli-airpfrom,sy-vline.
* 隐藏关键字
    HIDE: spfli-carrid,spfli-connid,spfli-countryto,
          spfli-cityfrom,spfli-airpfrom.
    ULINE AT /(95).
* 隐藏选择框,使表格行不会选择
    HIDE sela.
    num = num + 1.
  ENDSELECT.

END-OF-SELECTION.
*  num1 = sy-lsind - 1.

AT LINE-SELECTION.
  WRITE :  'sy-lsind:',sy-lsind.
   WRITE :  'num:',num.
*只输出第一页
  IF sy-lsind = 1.
    DO num TIMES.
* 读取行
      READ LINE sy-index FIELD VALUE sela.
* 如果checked,输出隐含字段
      IF sela = 'X'.
        WRITE: / spfli-carrid,spfli-connid,spfli-countryto,
                 spfli-cityfrom,spfli-airpfrom.
      ENDIF.
    ENDDO.
  ENDIF.

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!