ABAP简单ALV报表实现 FUNCTION ALV 01

ABAP简单报表
使用 REUSE_ALV_GRID_DISPLAY 实现

下面贴上代码:

TABLES: SFLIGHT.

"定义输出内表结构
TYPES: BEGIN OF TY_OUTPUT.
    INCLUDE TYPE SFLIGHT.
TYPES: ZBOX       TYPE C,
       ROW_COLOR  TYPE C LENGTH 4, "行颜色
       CELL_COLOR TYPE LVC_T_SCOL. "单元格颜色,需要定义一个内表字段,然后在后面的循环中根据条件对这个字段(内表)赋值
TYPES: END OF TY_OUTPUT.

"定义输出内表
DATA: GT_OUTPUT TYPE TABLE OF TY_OUTPUT,
      GS_OUTPUT TYPE TY_OUTPUT.

DATA: GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV. "内表字段属性
DATA: GS_FIELDCAT TYPE SLIS_FIELDCAT_ALV. "内表字段属性
DATA: GS_LAYOUT TYPE SLIS_LAYOUT_ALV. "ALV全局属性
DATA: GT_SORT TYPE SLIS_T_SORTINFO_ALV. "排序
DATA: GS_SORT TYPE SLIS_SORTINFO_ALV. "
DATA: GT_FILTER TYPE SLIS_T_FILTER_ALV. "过滤器
DATA: GS_FILTER TYPE SLIS_FILTER_ALV.  "

DATA: LS_SCOL TYPE LVC_S_SCOL.
DATA: LT_SCOL TYPE LVC_T_SCOL. "设置单元格颜色

DATA: GV_COL TYPE I. "列编号

SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME.
SELECT-OPTIONS: S_CARRID FOR SFLIGHT-CARRID.  "航线
SELECTION-SCREEN END OF BLOCK BLK1.

START-OF-SELECTION.

  "取数
  PERFORM FRM_SELECT_DATA.
  "数据处理
  PERFORM FRM_DATA_PROCESS.

END-OF-SELECTION.
  "ALV显示
  PERFORM FRM_SET_ALV.


FORM FRM_SELECT_DATA.
  "取值
  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE GT_OUTPUT
    FROM SFLIGHT
    WHERE CARRID IN S_CARRID.
ENDFORM.

FORM FRM_DATA_PROCESS.
  LOOP AT GT_OUTPUT ASSIGNING FIELD-SYMBOL(<FW_OUTPUT>).
    IF <FW_OUTPUT>-CARRID EQ 'LH'.
      "设置行颜色为C300
      <FW_OUTPUT>-ROW_COLOR = 'C300'.
    ENDIF.

    IF <FW_OUTPUT>-SEATSOCC EQ 0 .
      "设置单元格颜色
      CLEAR LS_SCOL.
      LS_SCOL-FNAME = 'SEATSOCC'.
      LS_SCOL-COLOR-COL = '3'.  "颜色代码 1、灰蓝色 2、浅灰色 3、黄色 4、蓝色 5、绿色 6、红色 7、橙色
      LS_SCOL-COLOR-INT = '1'.   "颜色是否加重 0 默认 、1 加重
      LS_SCOL-COLOR-INV = '0'.   "是否反色 0 默认(底纹为当前颜色)1 反色(字为当前颜色)     注意: 这里设置为1时,颜色不要加重,不然会没效果
      APPEND LS_SCOL TO LT_SCOL.
    ENDIF.

    IF <FW_OUTPUT>-SEATSOCC_B EQ 0 .
      CLEAR LS_SCOL.
      LS_SCOL-FNAME = 'SEATSOCC_B'.
      LS_SCOL-COLOR-COL = '3'.
      LS_SCOL-COLOR-INT = '1'.
      LS_SCOL-COLOR-INV = '0'.
      APPEND LS_SCOL TO LT_SCOL.
    ENDIF.

    IF <FW_OUTPUT>-SEATSOCC_F EQ 0.
      CLEAR LS_SCOL.
      LS_SCOL-FNAME = 'SEATSOCC_F'.
      LS_SCOL-COLOR-COL = '3'.
      LS_SCOL-COLOR-INT = '1'.
      LS_SCOL-COLOR-INV = '0'.
      APPEND LS_SCOL TO LT_SCOL.
    ENDIF.

    IF LT_SCOL IS NOT INITIAL.
      "最后将内表赋值给 CELL_COLOR 字段
      <FW_OUTPUT>-CELL_COLOR = LT_SCOL.
      FREE LT_SCOL.
    ENDIF.
  ENDLOOP.
ENDFORM.

***  "生成属性内表

************************************方法一 BEGIN************************************
FORM FRM_FIELDCAT USING P_FIELDNAME P_REFTAB P_REFFIELD P_TEXT.

  CLEAR GS_FIELDCAT.
  GS_FIELDCAT-FIELDNAME = P_FIELDNAME.
  GS_FIELDCAT-REF_TABNAME = P_REFTAB.
  GS_FIELDCAT-REF_FIELDNAME = P_REFFIELD.
  GS_FIELDCAT-SELTEXT_L = P_TEXT.
  GV_COL = GV_COL + 1.
  GS_FIELDCAT-COL_POS = GV_COL.

  IF P_FIELDNAME EQ 'CARRID'.
    GS_FIELDCAT-HOTSPOT = 'X'. "设置单击
  ENDIF.

  IF P_FIELDNAME EQ 'PRICE'.
    GS_FIELDCAT-EMPHASIZE = 'C710'. "设置颜色
  ENDIF.

  APPEND GS_FIELDCAT TO GT_FIELDCAT.

ENDFORM.

FORM FRM_SET_FIELDCAT.
  PERFORM FRM_FIELDCAT USING  'CARRID' 'SFLIGHT' 'CARRID' '航线'.
  PERFORM FRM_FIELDCAT USING  'CONNID' 'SFLIGHT' 'CONNID' '航班'.
  PERFORM FRM_FIELDCAT USING  'FLDATE' 'SFLIGHT' 'FLDATE' '航班日期'.
  PERFORM FRM_FIELDCAT USING  'PRICE' 'SFLIGHT' 'PRICE' '航空运费'.
  PERFORM FRM_FIELDCAT USING  'CURRENCY' 'SFLIGHT' 'CURRENCY' '价格'.
  PERFORM FRM_FIELDCAT USING  'PLANETYPE' 'SFLIGHT' 'PLANETYPE' '飞机类型'.
  PERFORM FRM_FIELDCAT USING  'SEATSMAX' 'SFLIGHT' 'SEATSMAX' '经济舱位'.
  PERFORM FRM_FIELDCAT USING  'SEATSOCC' 'SFLIGHT' 'SEATSOCC' '经济舱占据位'.
  PERFORM FRM_FIELDCAT USING  'PAYMENTSUM' 'SFLIGHT' 'PAYMENTSUM' '当前预定总数'.
  PERFORM FRM_FIELDCAT USING  'SEATSMAX_B' 'SFLIGHT' 'SEATSMAX_B' '商务舱位'.
  PERFORM FRM_FIELDCAT USING  'SEATSOCC_B' 'SFLIGHT' 'SEATSOCC_B' '商务舱占据位'.
  PERFORM FRM_FIELDCAT USING  'SEATSMAX_F' 'SFLIGHT' 'SEATSMAX_F' '头等舱位'.
  PERFORM FRM_FIELDCAT USING  'SEATSOCC_F' 'SFLIGHT' 'SEATSOCC_F' '头等舱占据位'.
ENDFORM.

************************************方法一 END************************************

************************************方法二  BEGIN************************************
*FREE GT_FIELDCAT.
*CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
*  EXPORTING
*    I_PROGRAM_NAME   = SY-REPID
**   I_INTERNAL_TABNAME           =
*    I_STRUCTURE_NAME = 'SFLIGHT'
**   I_CLIENT_NEVER_DISPLAY       = 'X'
**   I_INCLNAME       =
**   I_BYPASSING_BUFFER           =
**   I_BUFFER_ACTIVE  =
*  CHANGING
*    CT_FIELDCAT      = GT_FIELDCAT
*   EXCEPTIONS
*   INCONSISTENT_INTERFACE       = 1
*   PROGRAM_ERROR    = 2
*   OTHERS           = 3
*  .
*IF SY-SUBRC <> 0.
** Implement suitable error handling here
*ENDIF.
*
*DESCRIBE TABLE GT_FIELDCAT LINES GV_COL.
*
**  PERFORM FRM_FIELDCAT USING  'SEATSOCC_F' 'SFLIGHT' 'SEATSOCC_F' '头等舱占据位'.
**  PERFORM FRM_FIELDCAT USING  'SEATSOCC_F' 'SFLIGHT' 'SEATSOCC_F' '头等舱占据位'.
**  PERFORM FRM_FIELDCAT USING  'SEATSOCC_F' 'SFLIGHT' 'SEATSOCC_F' '头等舱占据位'.
*
*LOOP AT GT_FIELDCAT ASSIGNING FIELD-SYMBOL(<FW_FIELDCAT>).
*  IF <FW_FIELDCAT>-FIELDNAME EQ 'CARRID'.
*    "设置单击属性
*    <FW_FIELDCAT>-HOTSPOT = 'X'.
*  ENDIF.
*
*  "设置ALV列颜色属性 EMPHASIZE
*  IF <FW_FIELDCAT>-FIELDNAME EQ 'PRICE'.
*    <FW_FIELDCAT>-EMPHASIZE = 'C710'.
*  ENDIF.
*ENDLOOP.
************************************方法二  END************************************

FORM FRM_SET_LAYO.
  "设置ALV全局属性
  CLEAR GS_LAYOUT.
*  GS_LAYOUT-KEY_HOTSPOT = 'X'. ”主键单击
  GS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'. "自动列宽
  GS_LAYOUT-ZEBRA = 'X'. "斑马纹
  GS_LAYOUT-BOX_FIELDNAME = 'ZBOX'. "选择块
  GS_LAYOUT-INFO_FIELDNAME = 'ROW_COLOR'. "行颜色
  GS_LAYOUT-COLTAB_FIELDNAME = 'CELL_COLOR'. "单元格颜色
ENDFORM.

FORM FRM_SET_SORT.
  "设置排序
  CLEAR GS_SORT.
  GS_SORT-SPOS = '1'. "排序顺序 1优先级最高
  GS_SORT-FIELDNAME = 'FLDATE'. "排序的字段
  GS_SORT-DOWN = 'X'. "升序或降序  UP 升序 、、DOWN 降序
  APPEND GS_SORT TO GT_SORT.

  CLEAR GS_SORT.
  GS_SORT-SPOS = '2'.
  GS_SORT-FIELDNAME = 'CONNID'.
  GS_SORT-UP = 'X'.
  APPEND GS_SORT TO GT_SORT.
ENDFORM.

*FORM FRM_SET_FILTER.
*  CLEAR GS_FILTER.
*  GS_FILTER-FIELDNAME = 'PRICE'.
*  GS_FILTER-SIGN0 = 'E'.
*  GS_FILTER-OPTIO = 'BT'.
*  GS_FILTER-VALUF = 0.
*  GS_FILTER-VALUT = 1000.
*  APPEND GS_FILTER TO GT_FILTER.
*ENDFORM.

FORM FRM_SET_ALV.

  PERFORM FRM_SET_LAYO.
  PERFORM FRM_SET_FIELDCAT.
  PERFORM FRM_SET_SORT.
*  PERFORM FRM_SET_FILTER.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
*     I_INTERFACE_CHECK           = ' '
*     I_BYPASSING_BUFFER          = ' '
*     I_BUFFER_ACTIVE             = ' '
      I_CALLBACK_PROGRAM          = SY-CPROG
      I_CALLBACK_PF_STATUS_SET    = 'FRM_PF_STATUS'
      I_CALLBACK_USER_COMMAND     = 'FRM_USER_COMMAND'
*     I_CALLBACK_TOP_OF_PAGE      = 'FRM_TOP_OF_PAGE'
      I_CALLBACK_HTML_TOP_OF_PAGE = 'FRM_HTML_TOP_OF_PAGE'
*     I_CALLBACK_HTML_END_OF_LIST = ' '
*     I_STRUCTURE_NAME            = 'SFLIGHT'
*     I_BACKGROUND_ID             = ' '
*     I_GRID_TITLE                = '今日航班表'
*     I_GRID_SETTINGS             =
      IS_LAYOUT                   = GS_LAYOUT
      IT_FIELDCAT                 = GT_FIELDCAT
*     IT_EXCLUDING                =
*     IT_SPECIAL_GROUPS           =
      IT_SORT                     = GT_SORT
      IT_FILTER                   = GT_FILTER
*     IS_SEL_HIDE                 =
      I_DEFAULT                   = 'X'
      I_SAVE                      = 'A'
*     IS_VARIANT                  =
*     IT_EVENTS                   =
*     IT_EVENT_EXIT               =
*     IS_PRINT                    =
*     IS_REPREP_ID                =
*     I_SCREEN_START_COLUMN       = 0
*     I_SCREEN_START_LINE         = 0
*     I_SCREEN_END_COLUMN         = 0
*     I_SCREEN_END_LINE           = 0
*     I_HTML_HEIGHT_TOP           = 0
*     I_HTML_HEIGHT_END           = 0
*     IT_ALV_GRAPHICS             =
*     IT_HYPERLINK                =
*     IT_ADD_FIELDCAT             =
*     IT_EXCEPT_QINFO             =
*     IR_SALV_FULLSCREEN_ADAPTER  =
* IMPORTING
*     E_EXIT_CAUSED_BY_CALLER     =
*     ES_EXIT_CAUSED_BY_USER      =
    TABLES
      T_OUTTAB                    = GT_OUTPUT
    EXCEPTIONS
      PROGRAM_ERROR               = 1
      OTHERS                      = 2.
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.

FORM FRM_PF_STATUS USING RT_EXTAB TYPE SLIS_T_EXTAB.
  SET PF-STATUS 'ZSTATUS'.
ENDFORM.

FORM FRM_USER_COMMAND  USING R_UCOMM LIKE SY-UCOMM
                           RS_SELFIELD TYPE SLIS_SELFIELD.
  DATA: LV_MSG TYPE STRING.

  CASE R_UCOMM.
    WHEN '&IC1'.
      READ TABLE GT_OUTPUT ASSIGNING FIELD-SYMBOL(<FW_OUTPUT>) INDEX RS_SELFIELD-TABINDEX.
      IF SY-SUBRC EQ 0.
        CONCATENATE '您选择了' <FW_OUTPUT>-FLDATE <FW_OUTPUT>-CARRID '航线' <FW_OUTPUT>-CONNID '号航班' INTO LV_MSG SEPARATED BY SPACE.
        MESSAGE LV_MSG TYPE 'S'.
      ENDIF.

    WHEN ''.
    WHEN OTHERS.
  ENDCASE.

  RS_SELFIELD-COL_STABLE = 'X'.
  RS_SELFIELD-ROW_STABLE = 'X'.
  RS_SELFIELD-REFRESH = 'X'.
ENDFORM.

FORM FRM_TOP_OF_PAGE.

  DATA: LT_HEADER TYPE SLIS_T_LISTHEADER.
  DATA: LS_HEADER TYPE SLIS_LISTHEADER.

  DATA: LV_DATE TYPE C LENGTH 14.

  CLEAR LS_HEADER.
  LS_HEADER-TYP = 'H'.
*  LS_HEADER-TYP = 'H'.
  LS_HEADER-INFO = '今日航班表'.
  APPEND LS_HEADER TO LT_HEADER.

  WRITE SY-DATUM TO LV_DATE USING EDIT MASK '____年__月__日'.
  CLEAR LS_HEADER.
  LS_HEADER-TYP = 'S'.
  LS_HEADER-KEY = '当前日期'.
  LS_HEADER-INFO = LV_DATE.
  APPEND LS_HEADER TO LT_HEADER.

  CLEAR LS_HEADER.
  LS_HEADER-TYP = 'A'.
*  LS_HEADER-KEY = '当前日期'.
  LS_HEADER-INFO = '感谢您的光临'.
  APPEND LS_HEADER TO LT_HEADER.


  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      IT_LIST_COMMENTARY = LT_HEADER
*     I_LOGO             =
*     I_END_OF_LIST_GRID =
*     I_ALV_FORM         =
    .

ENDFORM.

FORM FRM_HTML_TOP_OF_PAGE USING PR_DOCUMENT TYPE REF TO CL_DD_DOCUMENT..
  CALL METHOD PR_DOCUMENT->ADD_TEXT
    EXPORTING
      TEXT         = '今日航班表'
      SAP_FONTSIZE = CL_DD_DOCUMENT=>LARGE
      SAP_COLOR    = CL_DD_DOCUMENT=>LIST_HEADING.

  CALL METHOD PR_DOCUMENT->NEW_LINE.
  CALL METHOD PR_DOCUMENT->NEW_LINE.

  CALL METHOD PR_DOCUMENT->ADD_TEXT
    EXPORTING
      TEXT         = 'LINK'
      SAP_FONTSIZE = CL_DD_DOCUMENT=>MEDIUM.

  CALL METHOD PR_DOCUMENT->ADD_GAP
    EXPORTING
      WIDTH = 5.

  CALL METHOD PR_DOCUMENT->ADD_LINK
    EXPORTING
      NAME    = '123'
      TOOLTIP = '百度'
      URL     = 'https://www.baidu.com/'
      TEXT    = 'BAIDU'.

ENDFORM.

初学者,如有错误或改进意见,欢迎指出,非常感谢。
over。

你可能感兴趣的:(ABAP,ABAP,FUNCTION,ALV,报表)