SAP ABAP ALV+SMARTFORS 表分页 报表打印程序

选中 ALV屏幕支持选中、全选或取消全选交货单行项目
打印预览: ALV屏幕:选中交货单行项目,点击打印预览,按打印样式输出交货单打印预览样式
打印 ALV屏幕选中交货单行项目,点击打印,调用打印机,按打印样式输出交货单
大致需求就是:在alv中选中数据然后做出打印.

该程序分:
1.结构定义
2.逻辑取数
3.数据alv显示
4.根据alv选中的数据对应的打印数据
5.调用smartforms
6.输出.

取数alv展示这一块就不再说了.就很简单的.
主要讲解一下smartforms中的一些参数作用.

*smartforms调用

  DATA: 
  *这个参数主要是为了控制结构的.
  		ls_control TYPE ssfctrlop,
  *在传输过程中所做的选项
        ls_output  TYPE ssfcompop,
  *表格在打印开始时返回的值
        ls_jobout  TYPE ssfcresop,
  *这个参数主要是为了动态获取smartforms所调用的程序
        gv_fm_name    TYPE rs38l_fnam.

在打印smartforms一般会用到四件套

FORM frm_print_data .

  DATA: ls_control TYPE ssfctrlop,
        ls_output  TYPE ssfcompop,
        ls_jobout  TYPE ssfcresop,
        gv_fm_name    TYPE rs38l_fnam.

  DATA:ls_return TYPE ssfcrescl.    "打印返回值

*  ls_output-tddest   = 'LP01'."为弹出的窗口设置默认的打印设备
  ls_output-tdiexit  = 'X'. "立刻退出迟于打印预览中的打印/传真
  ls_output-tdimmed  = 'X'."立即假脱机
  ls_output-tddelete = 'X'."立即假脱机删除
  ls_output-tdnoprev = ''."无打印预览,null的话就是打印预览,给X才是无

  ls_control-preview   = 'X'. " 打印预览
  ls_control-no_dialog = ' '."这个三个都是通用的标识符,见名知意
  ls_control-no_open   = 'X'.
  ls_control-no_close  = 'X'.

  CALL FUNCTION 'SSF_OPEN'
    EXPORTING
      user_settings      = ''   "默认是为X的,复选框
      output_options     = ls_output
      control_parameters = ls_control
    IMPORTING
      job_output_options = ls_jobout
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname           = 'ZFIFM002'  " 所创建smartforms的名称
*     VARIANT            = ' '
*     DIRECT_CALL        = ' '
    IMPORTING
      fm_name            = gv_fm_name "输出名称
    EXCEPTIONS
      no_form            = 1
      no_function_module = 2
      OTHERS             = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

*因为执行smartforms所要使用的函数模块的名称是系统自定的给出的,如:/1BCDWB/SF00000002
主要是为了防止以后在维护smartforms是会自动更改该函数模块名称,因此尽量自定义一下.
:如:图一
*把那个名字改一下换成你自己的就可以了
  CALL FUNCTION gv_fm_name
    EXPORTING
*     ARCHIVE_INDEX      =
*     ARCHIVE_INDEX_TAB  =
*     ARCHIVE_PARAMETERS =
      control_parameters = ls_control
*     MAIL_APPL_OBJ      =
*     MAIL_RECIPIENT     =
*     MAIL_SENDER        =
      output_options     = ls_output
      user_settings      = ''
*    IMPORTING
*     DOCUMENT_OUTPUT_INFO       =
*     JOB_OUTPUT_INFO    =
*     job_output_options = sf_job_output_options
    TABLES
      gt_sf              = gt_sf "行
      gt_head            = gt_sf2 "头
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.

  CLEAR: gt_sf,gt_sf2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  CALL FUNCTION 'SSF_CLOSE'
*    IMPORTING
*      job_output_info  = ls_return
    EXCEPTIONS
      formatting_error = 1
      internal_error   = 2
      send_error       = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.

图一
在这里插入图片描述
对smartforms的调用就这些,接下来看看smartforms是如何做的.
输入:tcodesmartforms

全局设置

1.表格属性
在表格属性当中一般在输出选项中页格式会用的比较多一点,根据业务需求可能要打印的格式会有所不同,在这可以自定义设置.页格式如何自定义:参考该链接
在这里插入图片描述
2.表格属性
这里的导入/导出/表/例外的操作你完全可以按照函数模块的方式经行处理,
因为在这定义的参数实际上都是给到系统自动生成的那个函数模块当中去了.
表:这里面的参数是只可以使用表类型结构的参数,可以se11中去定义然后在定义一个结构

在这里插入图片描述
但是要在表类型中行类型添加一个结构
在这里插入图片描述

在这里插入图片描述
3.全局定义
全局数据:他的意义是为了接收表接口传来的数据
货币/数量字段:这个可以参考这个
在这里插入图片描述

页和窗口

同一页中只能有一个主窗口
1.窗口
有四种窗口,常用的就是主次窗口
在这里插入图片描述

输出选项:在这里设置的就是这个窗口的总的长和宽,在该窗口下的模板和表不能超过它.
在这里插入图片描述

依照我这个smartforms结构
我这里是给抬头行和行数据都做了分页.(根据发货单号打印数据,一个发货单号对应诺干个行项目)
在这里插入图片描述
这里的第一层循环是抬头
在这里插入图片描述

第二次循环的表
在这里插入图片描述
控制分页
在这里插入图片描述
以下代码仅供个人参考.

*&---------------------------------------------------------------------*
*& Report ZSDR0002
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zsdr0001 MESSAGE-ID z_sd_01.

TYPES: BEGIN OF gty_alv,"原型字段
         erdat TYPE likp-erdat,
         vbeln TYPE lips-vbeln,
         zzdr  TYPE c LENGTH 12,
         posnr TYPE lips-posnr,
         werks TYPE lips-werks,
         kunnr TYPE likp-kunnr,
         name1 TYPE kna1-name1,
         matnr TYPE lips-matnr,
         arktx TYPE lips-arktx,
         lfimg TYPE lips-lfimg,
         vrkme TYPE lips-vrkme,
         charg TYPE lips-charg,
         lgmng TYPE lips-lgmng,
         meins TYPE lips-meins,
         zdycs TYPE i,
         sel   TYPE c LENGTH 1,
       END OF gty_alv.

DATA: gt_print TYPE TABLE OF gty_alv,
      gs_print TYPE gty_alv.

*行
DATA: gt_sf TYPE TABLE OF zssd0008,
      gs_sf TYPE  zssd0008.

*头
DATA : gt_sf2 TYPE TABLE OF zssd0011,
       gs_sf2 TYPE zssd0011.

*" ALV 显示
DATA: gs_layout   TYPE lvc_s_layo,
      gt_fieldcat TYPE lvc_t_fcat,
      gs_fieldcat TYPE lvc_s_fcat.

**smartforms调用
*DATA: gv_fm_name TYPE rs38l_fnam . "函数名
*DATA : sf_output_options TYPE ssfcompop.
*DATA : sf_control_parameters TYPE ssfctrlop.
*DATA : sf_job_output_options TYPE ssfcresop.

DATA legth TYPE i.


"宏定义
DEFINE m_fcat.
  CLEAR gs_fieldcat.

  gs_fieldcat-fieldname = &1. "字段
  gs_fieldcat-scrtext_l = &2. "字段描述
  "gs_fieldcat-checkbox =  &3 . "是否复选框


  APPEND gs_fieldcat TO gt_fieldcat .
END-OF-DEFINITION.

SELECTION-SCREEN BEGIN OF BLOCK bk1 WITH FRAME TITLE TEXT-001.
  SELECT-OPTIONS: s_erdat FOR gs_print-erdat NO-EXTENSION ,"凭证日期
                  s_vbeln FOR gs_print-vbeln NO-EXTENSION. " 交货订单号

  PARAMETERS p_zzdr LIKE gs_print-zzdr . "制单人

  SELECTION-SCREEN SKIP 2 . "2行

  PARAMETERS r_floor RADIOBUTTON GROUP gp1 .  "地板业务
  PARAMETERS r_home RADIOBUTTON GROUP gp1 .   "家居业务

SELECTION-SCREEN END OF BLOCK bk1.

INITIALIZATION.


START-OF-SELECTION.

  IF r_floor = 'X'.
    PERFORM  frm_get_data."获取数据
  ELSEIF r_home = 'X'.

    MESSAGE '家居业务暂时不可打印,谢谢' TYPE 'W'.
    EXIT.
  ENDIF.


END-OF-SELECTION.
* 显示数据
  IF gt_print[] IS NOT INITIAL.
    PERFORM frm_show_data.
  ELSE.
    MESSAGE '未查询到数据,请检查输入!' TYPE 'S' DISPLAY LIKE 'E'.
  ENDIF.
*&---------------------------------------------------------------------*
*& Form frm_get_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_data .
  SELECT lips~matnr
         lips~arktx
         lips~lfimg
         lips~vrkme
         lips~charg
         lips~lgmng
         lips~meins
         lips~vbeln
         lips~posnr
         lips~werks
    likp~erdat
    likp~kunnr
         kna1~name1
    FROM likp
    INNER JOIN lips ON  likp~vbeln = lips~vbeln
    INNER JOIN kna1 ON  kna1~kunnr = likp~kunnr
    INTO CORRESPONDING FIELDS OF TABLE gt_print
      WHERE likp~erdat IN s_erdat
         OR lips~vbeln IN s_vbeln.


  IF  sy-subrc <> 0.
    MESSAGE e000 DISPLAY LIKE 'E'.
  ENDIF.


ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_show_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_show_data .
  " 显示数据
  PERFORM frm_init_layout.    " 设置输出屏幕的ALV显示格式
  PERFORM frm_init_fieldcat.  " 设置输出屏幕的ALV输出字段
  PERFORM frm_display_alv.    " 调用ALV显示函数显示数据
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_init_layout
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_init_layout .
  gs_layout-cwidth_opt = 'X'.
  gs_layout-zebra  = 'X'." 颜色交替.
  " gs_layout-sel_mode = 0.
  gs_layout-box_fname = 'SEL'. "复选框

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_init_fieldcat
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_init_fieldcat .
  "  设置输出屏幕的ALV输出字段
CLEAR gs_fieldcat.
  m_fcat:
        'ERDAT'   '凭证日期'               ,
        'VBELN'   '交货单号'               ,
        'ZZDR'    '制单人'                  ,
        'POSNR'   '行项目号'                ,
        'WERKS'   '交货工厂'                ,
        'KUNNR'   '送达方'                  ,
        'NAME1'   '送达方名称'              ,
        'MATNR'   '物料编码'                ,
        'ARKTX'   '物料描述'                ,
        'LFIMG'   '交货数量'                ,
        'VRKME'   '单位'                    ,
        'CHARG'   '批次'                    ,
        'LGMNG'   '以库存单位的交货数量'    ,
        'MEINS'   '单位'                    ,
        'ZDYCS'   '打印次数'                .

  APPEND gs_fieldcat TO gt_fieldcat.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_display_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_display_alv .
  "调用显示函数.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fieldcat
      i_callback_user_command  = 'USER_CLICK' "响应按钮事件
      i_callback_pf_status_set = 'SET_PF_STATUS' "列表按钮
    TABLES
      t_outtab                 = gt_print
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.

  IF sy-subrc <> 0.
* Implement suitable error handling here
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.

FORM set_pf_status USING i_extab TYPE slis_t_extab.
  SET PF-STATUS '101'.  " gui状态栏名称
ENDFORM.                    "SET_PF_STATUS

FORM user_click USING i_ucomm LIKE sy-ucomm i_selfield TYPE slis_selfield.

  CASE  sy-ucomm.
    WHEN 'PRINT'.

      PERFORM frm_get_print_data.    "获取打印数据
      PERFORM frm_print_data. "打印数据

  ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_print_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_print_data .

  DATA: ls_control TYPE ssfctrlop,
        ls_output  TYPE ssfcompop,
        ls_jobout  TYPE ssfcresop,
        gv_fm_name TYPE rs38l_fnam.

*  ls_output-tddest   = 'LP01'.
  ls_output-tdiexit  = 'X'.
  ls_output-tdimmed  = 'X'.
  ls_output-tddelete = 'X'.
  ls_output-tdnoprev = ''.

  ls_control-preview   = 'X'. " 打印预览
  ls_control-no_dialog = 'X'.
  ls_control-no_open   = 'X'.
  ls_control-no_close  = 'X'.

  CALL FUNCTION 'SSF_OPEN'
    EXPORTING
      user_settings      = ''
      output_options     = ls_output
      control_parameters = ls_control
    IMPORTING
      job_output_options = ls_jobout
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname           = 'ZFIFM002'
*     VARIANT            = ' '
*     DIRECT_CALL        = ' '
    IMPORTING
      fm_name            = gv_fm_name
    EXCEPTIONS
      no_form            = 1
      no_function_module = 2
      OTHERS             = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


  CALL FUNCTION gv_fm_name
    EXPORTING
*     ARCHIVE_INDEX      =
*     ARCHIVE_INDEX_TAB  =
*     ARCHIVE_PARAMETERS =
      control_parameters = ls_control
*     MAIL_APPL_OBJ      =
*     MAIL_RECIPIENT     =
*     MAIL_SENDER        =
      output_options     = ls_output
      user_settings      = ''
*    IMPORTING
*     DOCUMENT_OUTPUT_INFO       =
*     JOB_OUTPUT_INFO    =
*     job_output_options = sf_job_output_options
    TABLES
      gt_sf              = gt_sf "行
      gt_head            = gt_sf2 "头
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.

  CLEAR: gt_sf,gt_sf2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.



  CALL FUNCTION 'SSF_CLOSE'
*    IMPORTING
*      job_output_info  = ls_return
    EXCEPTIONS
      formatting_error = 1
      internal_error   = 2
      send_error       = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_get_print_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_print_data .

  DATA : gt_st  TYPE TABLE OF zssd0008,
         gs_st  TYPE zssd0008,
         gt_st2 TYPE TABLE OF zssd0008,
         gs_st2 TYPE zssd0008,
         gt_st3 TYPE TABLE OF zssd0008,
         gs_st3 TYPE zssd0008.
  DATA : gt_print_2 TYPE TABLE OF gty_alv,
         gs_print_2 TYPE gty_alv.

  DATA num TYPE i VALUE IS INITIAL.

  LOOP AT  gt_print INTO gs_print WHERE sel = 'X'.

    READ TABLE gt_print INTO gs_print_2 INDEX sy-tabix .
    APPEND gs_print_2 TO gt_print_2.

    legth = lines( gt_print_2 ).

    CLEAR: gs_print_2,gs_print.
  ENDLOOP.

  CLEAR gt_print.
  IF legth = 0.
    MESSAGE e011 DISPLAY LIKE 'E'.
    EXIT.
  ENDIF.

  LOOP AT gt_print_2 INTO gs_print_2 .
*行项目
    SELECT  likp~lfdat
            lips~kdmat
            mara~zbmcl
            mara~zggxh
            mara~zbzfs
      FROM likp
      INNER JOIN lips ON likp~vbeln =  lips~vbeln
      INNER JOIN mara ON lips~matnr =  mara~matnr
      INTO CORRESPONDING FIELDS OF TABLE gt_st2
      WHERE lips~vbeln   =  gs_print_2-vbeln
      AND   lips~matnr   =  gs_print_2-matnr .

    SELECT bztxt FROM t171t
         INNER JOIN knvv ON knvv~bzirk = t171t~bzirk
         INTO CORRESPONDING FIELDS OF TABLE gt_st3
         WHERE kunnr = gs_print_2-kunnr.

    SELECT  stras telf1 FROM  kna1
     INTO  CORRESPONDING FIELDS OF TABLE gt_st
     WHERE kunnr = gs_print_2-kunnr.

    READ TABLE gt_st2 INTO gs_st2  INDEX 1.
    MOVE-CORRESPONDING gs_st2 TO gs_sf.

    READ TABLE gt_st INTO gs_st INDEX 1.
    gs_sf-stras = gs_st-stras.
    gs_sf-telf1 = gs_st-telf1.

    READ TABLE gt_st3 INTO gs_st3 INDEX 1.
    gs_sf-bztxt = gs_st3-bztxt.

    MOVE-CORRESPONDING gs_print_2 TO gs_sf .
    APPEND gs_sf TO gt_sf.

*抬头
    MOVE-CORRESPONDING gs_sf TO gs_sf2 .
    APPEND gs_sf2 TO gt_sf2.
    DELETE ADJACENT DUPLICATES FROM gt_sf2 COMPARING vbeln.

    CLEAR: gs_sf ,gt_st ,gt_st2,gs_st,gs_st2,gs_st3,gt_st3,gs_sf2.
  ENDLOOP.

  SORT gt_sf BY  name1.
  SORT gt_sf2 BY vbeln.

ENDFORM.
  • 2
    点赞
  • 4
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:黑客帝国 设计师:我叫白小胖 返回首页
评论

打赏作者

雨天行舟

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值