ALV List是SAP中的一种报表方式,一般的ALV报表有一个表头,再加上表内容。对报表的设置有排序,分类汇总和合计等预设置。
ALV报表显示方式有2种,一种是Grid方式,一种是List方式,分别调用不同的Function来实现。下面是主要代码及说明。
TYPE-POOLS: slis.
form frm_show_data .
DATA: l_line TYPE slis_listheader.
DATA: hinfo TYPE slis_t_listheader.
DATA: fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
DATA: layout TYPE slis_layout_alv .
data: sortable type SLIS_T_SORTINFO_ALV with header line.
clear fieldcat.
add 1 to fieldcat-col_pos.
fieldcat-fieldname = 'LGORT'. " 对应的内表中的字段名称
fieldcat-seltext_m = '库存地点'. "ALV中显示的标题
fieldcat-outputlen = 15. " 列的显示宽度,可以不设置
append fieldcat.
form frm_show_data .
DATA: l_line TYPE slis_listheader.
DATA: hinfo TYPE slis_t_listheader.
DATA: fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.
DATA: layout TYPE slis_layout_alv .
data: sortable type SLIS_T_SORTINFO_ALV with header line.
clear fieldcat.
add 1 to fieldcat-col_pos.
fieldcat-fieldname = 'LGORT'. " 对应的内表中的字段名称
fieldcat-seltext_m = '库存地点'. "ALV中显示的标题
fieldcat-outputlen = 15. " 列的显示宽度,可以不设置
append fieldcat.
* 按照上面的方式添加所有需要在ALV中显示的字段
clear fieldcat.
add 1 to fieldcat-col_pos.
fieldcat-fieldname = 'CSPEM'.
fieldcat-seltext_m = '待报废数量'.
fieldcat-do_sum = 'X'. "汇总,该字段在ALV的最底部汇总
append fieldcat.
*-------Field List Table Setting--------
*---------------Begin of SortTable info
*--排序字段为分类字段,如果ALV需要自动分类合计,那么合计字段必须设置为排序字段,多个字段按照下面方式逐个添加
*clear sortable.
*sortable-FIELDNAME = 'LGORT'.
*sortable-UP = 'X'.
*sortable-DOWN = SPACE.
*sortable-SUBTOT = 'X'.
*append sortable.
*-----------------End of SortTable Info
layout-colwidth_optimize = 'X'. "列宽度自动根据内容优化
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_interface_check = ''
i_callback_program = sy-repid
is_layout = layout
* i_callback_pf_status_set = 'SET_PF_STATUS' "如果ALV使用自定义的工具栏,如果使用系统工具栏则忽略该行
* i_callback_user_command = 'USER_COMMAND' "自定义工具栏的事件
it_fieldcat = fieldcat[]
* 这里是动态嵌入的代码,用于ALV控制头部和尾部数据
I_CALLBACK_HTML_TOP_OF_PAGE = 'ALV_TOP_OF_PAGE' "如果需要表头
* it_sort = sortable[] "如果有排序和分类汇总,需要该行
* 动态代码结束
TABLES
t_outtab = GI_OUTPUT "ALV数据输出使用的内表
EXCEPTIONS
program_error = 1
OTHERS = 2.
endform. " frm_show_data
*------用于设置ALV的GUI状态
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
* SET PF-STATUS '0100'.
ENDFORM. "SET_PF_STATUS
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN 'Z_EXPORT'.
PERFORM frm_excel_out.
ENDCASE.
ENDFORM. "user_command
*-------ALV Commit Setting of ALV_top_of_page--------
FORM alv_top_of_page USING cl_dd TYPE REF TO cl_dd_document.
DATA: m_p TYPE i.
DATA: m_buff TYPE string.
*表头其实完全可以是一个html文件,自己使用html语言进行格式控制
m_buff = '<html>'.
CALL METHOD cl_dd->html_insert
EXPORTING
contents = m_buff
CHANGING
position = m_p.
m_buff = '<center><H2>配件报废品种汇总表</H2></Center>'.
CALL METHOD CL_DD->HTML_INSERT
EXPORTING
CONTENTS = m_buff
CHANGING
POSITION = m_p.
CONCATENATE '报表日期:' S_DATE-LOW ' TO ' S_DATE-HIGH '<BR>' into m_buff.
CALL METHOD CL_DD->HTML_INSERT
EXPORTING
CONTENTS = m_buff
CHANGING
POSITION = m_p.
m_buff = '</html>'.
CALL METHOD cl_dd->html_insert
EXPORTING
contents = m_buff
CHANGING
position = m_p.
ENDFORM. "ALV_top_of_page
clear fieldcat.
add 1 to fieldcat-col_pos.
fieldcat-fieldname = 'CSPEM'.
fieldcat-seltext_m = '待报废数量'.
fieldcat-do_sum = 'X'. "汇总,该字段在ALV的最底部汇总
append fieldcat.
*-------Field List Table Setting--------
*---------------Begin of SortTable info
*--排序字段为分类字段,如果ALV需要自动分类合计,那么合计字段必须设置为排序字段,多个字段按照下面方式逐个添加
*clear sortable.
*sortable-FIELDNAME = 'LGORT'.
*sortable-UP = 'X'.
*sortable-DOWN = SPACE.
*sortable-SUBTOT = 'X'.
*append sortable.
*-----------------End of SortTable Info
layout-colwidth_optimize = 'X'. "列宽度自动根据内容优化
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_interface_check = ''
i_callback_program = sy-repid
is_layout = layout
* i_callback_pf_status_set = 'SET_PF_STATUS' "如果ALV使用自定义的工具栏,如果使用系统工具栏则忽略该行
* i_callback_user_command = 'USER_COMMAND' "自定义工具栏的事件
it_fieldcat = fieldcat[]
* 这里是动态嵌入的代码,用于ALV控制头部和尾部数据
I_CALLBACK_HTML_TOP_OF_PAGE = 'ALV_TOP_OF_PAGE' "如果需要表头
* it_sort = sortable[] "如果有排序和分类汇总,需要该行
* 动态代码结束
TABLES
t_outtab = GI_OUTPUT "ALV数据输出使用的内表
EXCEPTIONS
program_error = 1
OTHERS = 2.
endform. " frm_show_data
*------用于设置ALV的GUI状态
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
* SET PF-STATUS '0100'.
ENDFORM. "SET_PF_STATUS
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN 'Z_EXPORT'.
PERFORM frm_excel_out.
ENDCASE.
ENDFORM. "user_command
*-------ALV Commit Setting of ALV_top_of_page--------
FORM alv_top_of_page USING cl_dd TYPE REF TO cl_dd_document.
DATA: m_p TYPE i.
DATA: m_buff TYPE string.
*表头其实完全可以是一个html文件,自己使用html语言进行格式控制
m_buff = '<html>'.
CALL METHOD cl_dd->html_insert
EXPORTING
contents = m_buff
CHANGING
position = m_p.
m_buff = '<center><H2>配件报废品种汇总表</H2></Center>'.
CALL METHOD CL_DD->HTML_INSERT
EXPORTING
CONTENTS = m_buff
CHANGING
POSITION = m_p.
CONCATENATE '报表日期:' S_DATE-LOW ' TO ' S_DATE-HIGH '<BR>' into m_buff.
CALL METHOD CL_DD->HTML_INSERT
EXPORTING
CONTENTS = m_buff
CHANGING
POSITION = m_p.
m_buff = '</html>'.
CALL METHOD cl_dd->html_insert
EXPORTING
contents = m_buff
CHANGING
position = m_p.
ENDFORM. "ALV_top_of_page
注: 在ALV中,需要注意所有添加的需要显示的列,都必须在相应的内表中有对应的字段,否则,只要使用合计或者分类汇总都会导致程序的崩溃。
注意:在SAP ABAP程序中,字符串的大小写很重要,在单引号包围的字符串中,一般来说都应该用大写,特别是在调用一些系统的方法时传入字符串参数时,例如在ALV中,Call Function 'REUSE_ALV_GRID_DISPLAY'时,传入的下面的两个参数:
i_callback_pf_status_set = 'SET_PF_STATUS'
i_callback_user_command = 'USER_COMMAND'
就必须使用大写字符串,否则就会出现Perform_not_found的系统错误。
另外,传入到ALV中的列对应的字段名称也必须使用大写字母,否则数据不会显示出来。