ALV List是SAP中的一种报表方式,一般的ALV报表有一个表头,再加上表内容。对报表的设置有排序,分类汇总和合计等预设置。

ALV报表显示方式有2种,一种是Grid方式,一种是List方式,分别调用不同的Function来实现。下面是主要代码及说明。

 

ABAP:ALV List报表_职场TYPE-POOLS: slis.
ABAP:ALV List报表_职场
ABAP:ALV List报表_职场form frm_show_data .
ABAP:ALV List报表_职场  DATA: l_line TYPE slis_listheader.
ABAP:ALV List报表_职场  DATA: hinfo TYPE slis_t_listheader.
ABAP:ALV List报表_职场  DATA: fieldcat TYPE slis_t_fieldcat_alv 
WITH HEADER LINE.
ABAP:ALV List报表_职场  DATA: layout TYPE slis_layout_alv .
ABAP:ALV List报表_职场  data: sortable type SLIS_T_SORTINFO_ALV 
with header line.
ABAP:ALV List报表_职场
ABAP:ALV List报表_职场  clear fieldcat.
ABAP:ALV List报表_职场  add 
1 to fieldcat-col_pos.
ABAP:ALV List报表_职场  fieldcat
-fieldname = 'LGORT'. " 对应的内表中的字段名称
ABAP:ALV List报表_职场
  fieldcat-seltext_m =  '库存地点'. "ALV中显示的标题
ABAP:ALV List报表_职场
  fieldcat-outputlen = 15. " 列的显示宽度,可以不设置
ABAP:ALV List报表_职场  append fieldcat.
*   按照上面的方式添加所有需要在ALV中显示的字段
ABAP:ALV List报表_职场
ABAP:ALV List报表_职场  clear fieldcat.
ABAP:ALV List报表_职场  add 
1 to fieldcat-col_pos.
ABAP:ALV List报表_职场  fieldcat
-fieldname = 'CSPEM'.
ABAP:ALV List报表_职场
  fieldcat-seltext_m =  '待报废数量'.
ABAP:ALV List报表_职场
  fieldcat-do_sum = 'X'.  "汇总,该字段在ALV的最底部汇总
ABAP:ALV List报表_职场
  append fieldcat.
ABAP:ALV List报表_职场
ABAP:ALV List报表_职场
*-------Field List Table Setting--------
ABAP:ALV List报表_职场
ABAP:ALV List报表_职场
*---------------Begin of SortTable info
ABAP:ALV List报表_职场
*--排序字段为分类字段,如果ALV需要自动分类合计,那么合计字段必须设置为排序字段,多个字段按照下面方式逐个添加
ABAP:ALV List报表_职场
*clear sortable.
ABAP:ALV List报表_职场
*sortable-FIELDNAME = 'LGORT'.
ABAP:ALV List报表_职场
*sortable-UP = 'X'.
ABAP:ALV List报表_职场
*sortable-DOWN = SPACE.
ABAP:ALV List报表_职场
*sortable-SUBTOT = 'X'.
ABAP:ALV List报表_职场
*append sortable.
ABAP:ALV List报表_职场
*-----------------End of SortTable Info
ABAP:ALV List报表_职场
ABAP:ALV List报表_职场layout
-colwidth_optimize = 'X'. "列宽度自动根据内容优化
ABAP:ALV List报表_职场

ABAP:ALV List报表_休闲_38    
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
ABAP:ALV List报表_职场_39
    EXPORTING
ABAP:ALV List报表_职场_39      i_interface_check        
= ''
ABAP:ALV List报表_职场_39
      i_callback_program       = sy-repid
ABAP:ALV List报表_职场_39      is_layout                
= layout
ABAP:ALV List报表_职场_39
*      i_callback_pf_status_set = 'SET_PF_STATUS' "如果ALV使用自定义的工具栏,如果使用系统工具栏则忽略该行
ABAP:ALV List报表_职场_39
*      i_callback_user_command  = 'USER_COMMAND' "自定义工具栏的事件
ABAP:ALV List报表_职场_39
      it_fieldcat              = fieldcat[]
ABAP:ALV List报表_职场_39
* 这里是动态嵌入的代码,用于ALV控制头部和尾部数据
ABAP:ALV List报表_职场_39      I_CALLBACK_HTML_TOP_OF_PAGE       
= 'ALV_TOP_OF_PAGE' "如果需要表头
ABAP:ALV List报表_职场_39
*    it_sort = sortable[] "如果有排序和分类汇总,需要该行
ABAP:ALV List报表_职场_39
* 动态代码结束
ABAP:ALV List报表_职场_39    TABLES
ABAP:ALV List报表_职场_39      t_outtab                 
= GI_OUTPUT "ALV数据输出使用的内表
ABAP:ALV List报表_职场_39    EXCEPTIONS
ABAP:ALV List报表_职场_39      program_error            
= 1
ABAP:ALV List报表_职场_39      OTHERS                   
= 2.
ABAP:ALV List报表_职场_39
ABAP:ALV List报表_职场_39endform.                    
" frm_show_data
ABAP:ALV List报表_职场_39
ABAP:ALV List报表_职场_39

ABAP:ALV List报表_职场_39
*------用于设置ALV的GUI状态
ABAP:ALV List报表_职场_39FORM set_pf_status USING rt_extab TYPE slis_t_extab.
ABAP:ALV List报表_职场_39
*  SET PF-STATUS '0100'.
ABAP:ALV List报表_职场_39
ENDFORM.                    "SET_PF_STATUS
ABAP:ALV List报表_职场_39

ABAP:ALV List报表_职场_39
FORM user_command  USING r_ucomm LIKE sy-ucomm
ABAP:ALV List报表_职场_39                                   rs_selfield TYPE slis_selfield.
ABAP:ALV List报表_职场_39  
CASE r_ucomm.
ABAP:ALV List报表_职场_39    WHEN 
'Z_EXPORT'.
ABAP:ALV List报表_职场_39
      PERFORM frm_excel_out.
ABAP:ALV List报表_职场_39  ENDCASE.
ABAP:ALV List报表_职场_39ENDFORM.                    
"user_command
ABAP:ALV List报表_职场_39
ABAP:ALV List报表_职场_39

ABAP:ALV List报表_职场_39
*-------ALV Commit Setting of ALV_top_of_page--------
ABAP:ALV List报表_职场_39FORM alv_top_of_page USING cl_dd TYPE REF 
TO cl_dd_document.
ABAP:ALV List报表_职场_39  DATA: m_p TYPE i.
ABAP:ALV List报表_职场_39  DATA: m_buff TYPE 
string.
ABAP:ALV List报表_职场_39*表头其实完全可以是一个html文件,自己使用html语言进行格式控制
ABAP:ALV List报表_职场_39  m_buff 
= '<html>'.
ABAP:ALV List报表_职场_39
  CALL METHOD cl_dd->html_insert
ABAP:ALV List报表_职场_39    EXPORTING
ABAP:ALV List报表_职场_39      contents 
= m_buff
ABAP:ALV List报表_职场_39    CHANGING
ABAP:ALV List报表_职场_39      position 
= m_p.
ABAP:ALV List报表_职场_39
ABAP:ALV List报表_职场_39m_buff 
= '<center><H2>配件报废品种汇总表</H2></Center>'.
ABAP:ALV List报表_职场_39
CALL METHOD CL_DD->HTML_INSERT
ABAP:ALV List报表_职场_39EXPORTING
ABAP:ALV List报表_职场_39  CONTENTS 
= m_buff
ABAP:ALV List报表_职场_39CHANGING
ABAP:ALV List报表_职场_39  POSITION 
= m_p.
ABAP:ALV List报表_职场_39
ABAP:ALV List报表_职场_39CONCATENATE 
'报表日期:' S_DATE-LOW ' TO ' S_DATE-HIGH  '<BR>' into m_buff.
ABAP:ALV List报表_职场_39
CALL METHOD CL_DD->HTML_INSERT
ABAP:ALV List报表_职场_39EXPORTING
ABAP:ALV List报表_职场_39  CONTENTS 
= m_buff
ABAP:ALV List报表_职场_39CHANGING
ABAP:ALV List报表_职场_39  POSITION 
= m_p.
ABAP:ALV List报表_职场_39
ABAP:ALV List报表_职场_39
ABAP:ALV List报表_职场_39  m_buff 
= '</html>'.
ABAP:ALV List报表_职场_39
  CALL METHOD cl_dd->html_insert
ABAP:ALV List报表_职场_39    EXPORTING
ABAP:ALV List报表_职场_39      contents 
= m_buff
ABAP:ALV List报表_职场_39    CHANGING
ABAP:ALV List报表_职场_39      position 
= m_p.
ABAP:ALV List报表_职场_39ENDFORM.                    
"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中的列对应的字段名称也必须使用大写字母,否则数据不会显示出来。