二维码

[report] ABAP编程中使用Write语句实现List报表实例

Twilight发表于 2014-03-29 23:59Twilight 最后回复于 2014-03-29 23:59 [复制链接] 9762 0

作业1 建立程序ZBC2_<YN>_HM1.
  • 进入ABAP数据字典,查看表sflight的表结构
  • 建立内表it_sflight和工作区wa_sflight,数据类型与sflight相同
  • 建立选择屏幕,其中carrid字段,要求用户选择一项,connid和fldate字段,用户可以选定一定范围的数据
  • 根据用户的选择,输出数据,
    要求输出的字段:carrid connid fldate price currency planetype seatsmax seatsocc
    注意,对于金额类型的字段,输出时一般还要输出它的币种。
    此外,不同币种输出的格式还不尽相同,在用WRITE语句输出金额字段时需要相应的调整语句。具体语法通过系统帮助查看。
    [*将输出的数据格式化,要求有表头,每个单元格有横线和竖线区分。表头的颜色与数据的颜色不一样,每行数据的主键字段与普通字段颜色不一样。

实现的效果:
基本语法实例1.jpg
程序代码:
  1. REPORT  ZLXG_HM1 LINE-SIZE 107.
  2. DATA:WA_SFLIGHT TYPE SFLIGHT.
  3. DATA:IT_SFLIGHT TYPE STANDARD TABLE OF SFLIGHT.

  4. PARAMETERS:PA_CARR TYPE SFLIGHT-CARRID.
  5. SELECT-OPTIONS:SO_CONN FOR WA_SFLIGHT-CONNID,
  6.                SO_FLDA FOR WA_SFLIGHT-FLDATE.

  7. SELECT *
  8.   FROM SFLIGHT
  9.   INTO CORRESPONDING FIELDS OF TABLE IT_SFLIGHT
  10.   WHERE CARRID = PA_CARR
  11.   AND CONNID IN SO_CONN
  12.   AND FLDATE IN SO_FLDA.

  13. FORMAT COLOR COL_HEADING. "头部显示COL_HEADING颜色
  14. ULINE.
  15. WRITE:/ SY-VLINE, AT 2(10) 'carrid'(001) ,"SY-VLINE:竖线  2(10):在第2个位置 输出长度为10的字符串
  16.         SY-VLINE, AT 14(10)'connid'(002),
  17.         SY-VLINE, AT 26(20)'fldate'(003),
  18.         SY-VLINE, AT 48(10)'price'(004),
  19.         SY-VLINE, AT 60(10)'currency'(005),
  20.         SY-VLINE, AT 72(10)'planetype'(006),
  21.         SY-VLINE, AT 84(10)'seatsmax'(007),
  22.         SY-VLINE, AT 96(10)'seatsocc'(008),
  23.         SY-VLINE.
  24. ULINE.
  25. FORMAT RESET."颜色重置结束

  26. LOOP AT IT_SFLIGHT INTO WA_SFLIGHT.
  27. WRITE:/ SY-VLINE, AT 2(10)  WA_SFLIGHT-carrid COLOR COL_KEY,
  28.         SY-VLINE, AT 14(10) WA_SFLIGHT-connid COLOR COL_KEY,
  29.         SY-VLINE, AT 26(20) WA_SFLIGHT-fldate COLOR COL_KEY,
  30.         SY-VLINE, AT 48(10) WA_SFLIGHT-price CURRENCY WA_SFLIGHT-currency,
  31.         SY-VLINE, AT 60(10) WA_SFLIGHT-currency,
  32.         SY-VLINE, AT 72(10) WA_SFLIGHT-planetype,
  33.         SY-VLINE, AT 84(10) WA_SFLIGHT-seatsmax,
  34.         SY-VLINE, AT 96(10) WA_SFLIGHT-seatsocc,
  35.         SY-VLINE.
  36.         ULINE.
  37. ENDLOOP.
复制代码

作业2 将ZBC2_<YN>_HM1拷贝为ZBC2_<YN>_HM2
  • 定义结构类型,包含表sflight的carrid connid fldate price currency planetype seatsmax seatsocc 字段,并新增一个字段occpercent 用来表示当前已经卖出去的座位的比例,即seatsocc/seatsmax,取值范围是0-1的两位小数,用p类型进行定义。
  • 将内表与工作区用该类型重新定义
    [*更改数据库取数据逻辑,将数据取道新的内表中
  • 遍历取得的内表,将每条数据取出来,计算occpercent,并更新内表
  • 输出数据时,在右边增加一列,将百分比数据在最后输出。
  • 在输出的最右边再增加一列 找到图标中的红绿灯图标。如果百分比低于50 显示绿灯,高于50 低于75  显示黄灯 高于75  显示红灯。
  • 更改选择屏幕,增加一个checkbox,打勾时,显示百分比和红绿灯图标,不打勾时,不不显示
  • 更改选择屏幕,增加一组radiobox ,选第一项时 显示所有数据,选第二项时,只显示绿灯和黄灯的,选第三项时,只显示绿灯的。
  • 调整选择屏幕外观,将数据的录入与选项的勾选,分成两个block 上下排列。

选择屏幕和执行效果:
基本语法实例2选择屏幕.jpg
基本语法实例2结果.jpg
程序代码:
  1. *--------------------------------------------------------------------*
  2. *TYPES
  3. *--------------------------------------------------------------------*
  4. *PERFORM tp_sflight.
  5. *
  6. *form tp_sfligt.
  7. TYPES:BEGIN OF tp_sflight,
  8.        carrid    TYPE sflight-carrid ,
  9.        connid    TYPE sflight-connid ,
  10.        fldate    TYPE sflight-fldate ,
  11.        price     TYPE sflight-price  ,
  12.        currency  TYPE sflight-currency,
  13.        planetype TYPE sflight-planetype,
  14.        seatsmax  TYPE sflight-seatsmax,
  15.        seatsocc  TYPE sflight-seatsocc,
  16.        occpercent TYPE p DECIMALS 2,
  17.       END OF tp_sflight.
  18. *--------------------------------------------------------------------*
  19. *DATA
  20. *--------------------------------------------------------------------*
  21. DATA:wa_sflight TYPE tp_sflight.
  22. DATA:it_sflight TYPE STANDARD TABLE OF tp_sflight.
  23. DATA:it_sflight_out TYPE STANDARD TABLE OF tp_sflight.
  24. *--------------------------------------------------------------------*
  25. *SELECTION-SCREEN
  26. *--------------------------------------------------------------------*
  27. SELECTION-SCREEN BEGIN OF BLOCK b_p1 WITH FRAME TITLE text-010.
  28. PARAMETERS:pa_carr TYPE sflight-carrid.
  29. SELECT-OPTIONS:so_conn FOR wa_sflight-connid,
  30.                so_flda FOR wa_sflight-fldate.
  31. SELECTION-SCREEN END OF BLOCK b_p1.
  32. SELECTION-SCREEN BEGIN OF BLOCK b_p2 WITH FRAME TITLE text-011.
  33. PARAMETERS: pa_check  AS CHECKBOX.
  34. PARAMETERS: radio1 RADIOBUTTON GROUP int,
  35.             radio2 RADIOBUTTON GROUP int,
  36.             radio3 RADIOBUTTON GROUP int.
  37. SELECTION-SCREEN END OF BLOCK b_p2.
  38. *--------------------------------------------------------------------*
  39. *LOAD-OF-PROGRAM = INITIALIZATION
  40. *--------------------------------------------------------------------*
  41. LOAD-OF-PROGRAM.
  42.   pa_carr = 'AA'.
  43.   pa_check = 'X'.
  44. *--------------------------------------------------------------------*
  45. *AT SELECTION-SCREEN
  46. *--------------------------------------------------------------------*
  47. AT SELECTION-SCREEN ON pa_check.
  48.   IF pa_check IS NOT INITIAL.
  49.     MESSAGE w003(zvino).
  50. *   Invalid operator!
  51.   ENDIF.
  52. *--------------------------------------------------------------------*
  53. *TOP-OF-PAGE.
  54. *--------------------------------------------------------------------*
  55. TOP-OF-PAGE.
  56.   FORMAT COLOR COL_HEADING.
  57.   IF pa_check = 'X'.
  58.     ULINE 1(131).
  59.   ELSE.
  60.     ULINE /1(107).
  61.   ENDIF.
  62.   WRITE:/ sy-vline, AT 2(10) 'carrid'(001) ,
  63.           sy-vline, AT 14(10)'connid'(002),
  64.           sy-vline, AT 26(20)'fldate'(003),
  65.           sy-vline, AT 48(10)'price'(004),
  66.           sy-vline, AT 60(10)'currency'(005),
  67.           sy-vline, AT 72(10)'planetype'(006),
  68.           sy-vline, AT 84(10)'seatsmax'(007),
  69.           sy-vline, AT 96(10)'seatsocc'(008).
  70.   IF pa_check = 'X'.
  71.     WRITE:sy-vline, AT 108(10)'percent'(009),
  72.           sy-vline, AT 120(10)'STATUS'(012),sy-vline.
  73.     ULINE /1(131).
  74.   ELSE.
  75.     WRITE:sy-vline.
  76.     ULINE /1(107).
  77.   ENDIF.
  78.   FORMAT RESET.

  79. *--------------------------------------------------------------------*
  80. *START-OF-SELECTION.
  81. *--------------------------------------------------------------------*
  82. START-OF-SELECTION."主处理事件 这个不能放在perform里
  83.   PERFORM select_date."把查询写成了了一个子程序,后面还可以附using changing  参数来对应取值
  84. *&---------------------------------------------------------------------*
  85. *&      Form  select_date
  86. *&---------------------------------------------------------------------*
  87. *       text
  88. *----------------------------------------------------------------------*
  89. FORM select_date.

  90.   SELECT *
  91.     FROM sflight
  92.     INTO CORRESPONDING FIELDS OF TABLE it_sflight
  93.     WHERE carrid = pa_carr
  94.     AND connid IN so_conn
  95.     AND fldate IN so_flda.

  96.   LOOP AT it_sflight INTO wa_sflight.
  97.     wa_sflight-occpercent = wa_sflight-seatsocc / wa_sflight-seatsmax.
  98.     MODIFY it_sflight FROM wa_sflight.
  99.   ENDLOOP.

  100.   CASE 'X'.
  101.     WHEN radio1.
  102.       it_sflight_out = it_sflight.
  103.     WHEN radio2.
  104.       LOOP AT it_sflight INTO wa_sflight WHERE occpercent < '0.75'.
  105.         APPEND wa_sflight TO it_sflight_out.
  106.       ENDLOOP.
  107.     WHEN radio3.
  108.       LOOP AT it_sflight INTO wa_sflight WHERE occpercent < '0.5'.
  109.         APPEND wa_sflight TO it_sflight_out.
  110.       ENDLOOP.
  111.   ENDCASE.

  112.   LOOP AT it_sflight_out INTO wa_sflight.
  113.     WRITE:/ sy-vline, AT 2(10)  wa_sflight-carrid COLOR COL_KEY,
  114.             sy-vline, AT 14(10) wa_sflight-connid COLOR COL_KEY,
  115.             sy-vline, AT 26(20) wa_sflight-fldate COLOR COL_KEY,
  116.             sy-vline, AT 48(10) wa_sflight-price  CURRENCY wa_sflight-currency,
  117.             sy-vline, AT 60(10) wa_sflight-currency,
  118.             sy-vline, AT 72(10) wa_sflight-planetype,
  119.             sy-vline, AT 84(10) wa_sflight-seatsmax,
  120.             sy-vline, AT 96(10) wa_sflight-seatsocc.
  121.     IF pa_check = 'X'.
  122.       WRITE:sy-vline, AT 108(10) wa_sflight-occpercent.
  123.       IF wa_sflight-occpercent < '0.5'.
  124.         WRITE: sy-vline, AT 120(10) icon_green_light AS ICON ,sy-vline.
  125.       ELSEIF wa_sflight-occpercent < '0.75'.
  126.         WRITE: sy-vline, AT 120(10) icon_yellow_light AS ICON ,sy-vline.
  127.       ELSE.
  128.         WRITE: sy-vline, AT 120(10) icon_red_light AS ICON ,sy-vline.
  129.       ENDIF.
  130.       ULINE /1(131).
  131.     ELSE.
  132.       WRITE:sy-vline.
  133.       ULINE /1(107).
  134.     ENDIF.
  135.   ENDLOOP.
  136. ENDFORM.                    "select_date
复制代码

perform 用传参形式的代码:
  1. *--------------------------------------------------------------------*
  2. *TYPES
  3. *--------------------------------------------------------------------*
  4. TYPES:BEGIN OF TP_SFLIGHT,
  5.        carrid    TYPE SFLIGHT-carrid ,
  6.        connid    TYPE SFLIGHT-connid ,
  7.        fldate    TYPE SFLIGHT-fldate ,
  8.        price     TYPE SFLIGHT-price  ,
  9.        currency  TYPE SFLIGHT-currency,
  10.        planetypE TYPE SFLIGHT-planetypE,
  11.        seatsmax  TYPE SFLIGHT-seatsmax,
  12.        seatsocc  TYPE SFLIGHT-seatsocc,
  13.        occpercent TYPE P DECIMALS 2,
  14.       END OF TP_SFLIGHT.
  15. *--------------------------------------------------------------------*
  16. *DATA
  17. *--------------------------------------------------------------------*
  18. DATA:WA_SFLIGHT TYPE TP_SFLIGHT.
  19. DATA:IT_SFLIGHT TYPE STANDARD TABLE OF TP_SFLIGHT.
  20. DATA:IT_SFLIGHT_OUT TYPE STANDARD TABLE OF TP_SFLIGHT.
  21. *--------------------------------------------------------------------*
  22. *SELECTION-SCREEN
  23. *--------------------------------------------------------------------*
  24. SELECTION-SCREEN BEGIN OF BLOCK B_P1 WITH FRAME TITLE TEXT-010.
  25. PARAMETERS:PA_CARR TYPE SFLIGHT-CARRID.
  26. SELECT-OPTIONS:SO_CONN FOR WA_SFLIGHT-CONNID,
  27.                SO_FLDA FOR WA_SFLIGHT-FLDATE.
  28. SELECTION-SCREEN END OF BLOCK B_P1.
  29. SELECTION-SCREEN BEGIN OF BLOCK B_P2 WITH FRAME TITLE TEXT-011.
  30. PARAMETERS: PA_check  AS CHECKBOX.
  31. PARAMETERS: radio1 RADIOBUTTON GROUP int,
  32.             radio2 RADIOBUTTON GROUP int,
  33.             radio3 RADIOBUTTON GROUP int.
  34. SELECTION-SCREEN END OF BLOCK B_P2.
  35. *--------------------------------------------------------------------*
  36. *LOAD-OF-PROGRAM = INITIALIZATION
  37. *--------------------------------------------------------------------*
  38. LOAD-OF-PROGRAM.
  39. PA_CARR = 'AA'.
  40. PA_check = 'X'.
  41. *--------------------------------------------------------------------*
  42. *AT SELECTION-SCREEN
  43. *--------------------------------------------------------------------*
  44. AT SELECTION-SCREEN ON PA_CHECK.
  45.   IF PA_check IS NOT INITIAL.
  46.      MESSAGE W003(ZVINO).
  47. *   Invalid operator!
  48.   ENDIF.
  49. *--------------------------------------------------------------------*
  50. *TOP-OF-PAGE.
  51. *--------------------------------------------------------------------*
  52. TOP-OF-PAGE.
  53. FORMAT COLOR COL_HEADING.
  54. IF PA_check = 'X'.
  55.   ULINE 1(131).
  56. ELSE.
  57.   ULINE /1(107).
  58. ENDIF.
  59. WRITE:/ SY-VLINE, AT 2(10) 'carrid'(001) ,
  60.         SY-VLINE, AT 14(10)'connid'(002),
  61.         SY-VLINE, AT 26(20)'fldate'(003),
  62.         SY-VLINE, AT 48(10)'price'(004),
  63.         SY-VLINE, AT 60(10)'currency'(005),
  64.         SY-VLINE, AT 72(10)'planetype'(006),
  65.         SY-VLINE, AT 84(10)'seatsmax'(007),
  66.         SY-VLINE, AT 96(10)'seatsocc'(008).
  67.         IF PA_check = 'X'.
  68.           WRITE:SY-VLINE, AT 108(10)'percent'(009),
  69.                 SY-VLINE, AT 120(10)'STATUS'(012),SY-VLINE.
  70.           ULINE /1(131).
  71.           ELSE.
  72.             WRITE:SY-VLINE.
  73.             ULINE /1(107).
  74.         ENDIF.
  75. FORMAT RESET.

  76. *--------------------------------------------------------------------*
  77. *START-OF-SELECTION.
  78. *--------------------------------------------------------------------*
  79. START-OF-SELECTION.
  80. PERFORM GET_DATA
  81.             USING
  82.                PA_CARR
  83.                RADIO1
  84.                RADIO2
  85.                RADIO3
  86.                SO_CONN[]
  87.                SO_FLDA[]
  88.             CHANGING
  89.                IT_SFLIGHT_OUT[].

  90. PERFORM OUT_DATA
  91.             USING
  92.                IT_SFLIGHT_OUT[].

  93. *&---------------------------------------------------------------------*
  94. *&      Form  GET_DATA
  95. *&---------------------------------------------------------------------*
  96. *       text
  97. *----------------------------------------------------------------------*
  98. *  -->  p1        text
  99. *  <--  p2        text
  100. *----------------------------------------------------------------------*
  101. FORM GET_DATA USING P_CARR
  102.                     rad1
  103.                     rad2
  104.                     rad3
  105.                     S_CONN TYPE TABLE
  106.                     S_FLDA TYPE TABLE
  107.               CHANGING T_SFLIGHT_OUT TYPE TABLE.

  108. DATA:W_SFLIGHT TYPE TP_SFLIGHT.
  109. DATA:T_SFLIGHT TYPE STANDARD TABLE OF TP_SFLIGHT.
  110. SELECT *
  111.   FROM SFLIGHT
  112.   INTO CORRESPONDING FIELDS OF TABLE T_SFLIGHT
  113.   WHERE CARRID = P_CARR
  114.   AND CONNID IN S_CONN
  115.   AND FLDATE IN S_FLDA.

  116. LOOP AT T_SFLIGHT INTO W_SFLIGHT.
  117. W_SFLIGHT-occpercent = W_SFLIGHT-seatsocc / W_SFLIGHT-seatsmax.
  118. MODIFY T_SFLIGHT FROM W_SFLIGHT.
  119. ENDLOOP.

  120. IF rad1 = 'X'.
  121. T_SFLIGHT_OUT = T_SFLIGHT.
  122. ENDIF.

  123. IF rad2 = 'X'.
  124.     LOOP AT T_SFLIGHT INTO W_SFLIGHT WHERE occpercent < '0.75'.
  125.        APPEND W_SFLIGHT TO T_SFLIGHT_OUT.
  126.     ENDLOOP.
  127. ENDIF.

  128. IF rad3 = 'X'.
  129.     LOOP AT T_SFLIGHT INTO W_SFLIGHT WHERE occpercent < '0.5'.
  130.        APPEND W_SFLIGHT TO T_SFLIGHT_OUT.
  131.     ENDLOOP.
  132. ENDIF.
  133. ENDFORM.                    " GET_DATA
  134. *&---------------------------------------------------------------------*
  135. *&      Form  OUT_DATA
  136. *&---------------------------------------------------------------------*
  137. *       text
  138. *----------------------------------------------------------------------*
  139. *  -->  p1        text
  140. *  <--  p2        text
  141. *----------------------------------------------------------------------*
  142. FORM OUT_DATA USING T_SFLIGHT_OUT TYPE TABLE.
  143. DATA:W_SFLIGHT TYPE TP_SFLIGHT.
  144. LOOP AT T_SFLIGHT_OUT INTO W_SFLIGHT.
  145. WRITE:/ SY-VLINE, AT 2(10)  W_SFLIGHT-carrid COLOR COL_KEY,
  146.         SY-VLINE, AT 14(10) W_SFLIGHT-connid COLOR COL_KEY,
  147.         SY-VLINE, AT 26(20) W_SFLIGHT-fldate COLOR COL_KEY,
  148.         SY-VLINE, AT 48(10) W_SFLIGHT-price  CURRENCY W_SFLIGHT-currency,
  149.         SY-VLINE, AT 60(10) W_SFLIGHT-currency,
  150.         SY-VLINE, AT 72(10) W_SFLIGHT-planetype,
  151.         SY-VLINE, AT 84(10) W_SFLIGHT-seatsmax,
  152.         SY-VLINE, AT 96(10) W_SFLIGHT-seatsocc.
  153.         IF PA_check = 'X'.
  154.         WRITE:SY-VLINE, AT 108(10) W_SFLIGHT-occpercent.
  155.           IF W_SFLIGHT-occpercent < '0.5'.
  156.             WRITE: SY-VLINE, AT 120(10) ICON_GREEN_LIGHT AS ICON ,SY-VLINE.
  157.             ELSEIF W_SFLIGHT-occpercent < '0.75'.
  158.               WRITE: SY-VLINE, AT 120(10) ICON_YELLOW_LIGHT AS ICON ,SY-VLINE.
  159.               ELSE.
  160.               WRITE: SY-VLINE, AT 120(10) ICON_RED_LIGHT AS ICON ,SY-VLINE.
  161.           ENDIF.
  162.           ULINE /1(131).
  163.         ELSE.
  164.             WRITE:SY-VLINE.
  165.             ULINE /1(107).
  166.         ENDIF.
  167. ENDLOOP.
  168. ENDFORM.                    " OUT_DATA
复制代码
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表