"先申明ALV需要的变量
TYPE-POOLS:SLIS.
DATA : LT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV,
LS_LAYOUT TYPE SLIS_LAYOUT_ALV. "alv的显示样式
"定义结构:
TYPES:BEGIN OF T_MATNR,
MATNR TYPE MARC-MATNR,
MAKTX TYPE MAKT-MAKTX,
END OF T_MATNR.
"定义内表以及工作区
DATA:LT_MATNR TYPE TABLE OF T_MATNR,
LS_MATNR TYPE T_MATNR.
"把要显示的数据读取到内表中去
SELECT MARC~MATNR,MAKTX
INTO TABLE @LT_MATNR
"INTO CORRESPONDING FIELDS OF TABLE LT_MATNR 获取的列比内表的列少时,通过这个语法进行列名的匹配,把值放入内表
FROM MARC
JOIN MAKT ON MARC~MATNR = MAKT~MATNR
WHERE MARC~MATNR = '140800743-001'.
"设置alv显示的样式
LS_LAYOUT-ZEBRA = 'X'.
LS_LAYOUT-DETAIL_POPUP = 'X'.
LS_LAYOUT-DETAIL_TITLEBAR = '详细信息'.
LS_LAYOUT-F2CODE = '&ETA'.
LS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
"设置alv要显示的列名
LS_FIELDCAT-FIELDNAME = 'MATNR'.
LS_FIELDCAT-COL_POS = '1'.
LS_FIELDCAT-KEY = 'X'.
LS_FIELDCAT-DATATYPE = 'CHAR'.
LS_FIELDCAT-OUTPUTLEN = '18'.
LS_FIELDCAT-SELTEXT_M = '物料号'.
APPEND LS_FIELDCAT TO LT_FIELDCAT.
LS_FIELDCAT-FIELDNAME = 'MAKTX'.
LS_FIELDCAT-COL_POS = '2'.
LS_FIELDCAT-KEY = 'X'.
LS_FIELDCAT-DATATYPE = 'CHAR'.
LS_FIELDCAT-OUTPUTLEN = '100'.
LS_FIELDCAT-SELTEXT_M = '物料描述'.
APPEND LS_FIELDCAT TO LT_FIELDCAT.
"调用alv函数方法显示
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IS_LAYOUT = LS_LAYOUT "样式
IT_FIELDCAT = LT_FIELDCAT "显示的列
TABLES
T_OUTTAB = LT_MATNR "存储数据的内表
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.