SMARTFORMS函数调用完整示例:打印工厂物料信息
首先规划想要打印的效果和信息,这里示例的是想要打印物料数据库里面的信息。
①对相关数据表创建表结构:SE11
注意数据类型,数量quan需要参考类型:
创建表结构:
②创建一个REPORT程序,并对相关的字段、变量、内表进行声明,并写一个SELECTION界面和一些基础的取数等,具体代码如下:
DATA GT_MARD TYPE ZMARD_TTY . "取数/向SMARTFORMS传输,用表类型定义一个内表,类型和smartform接口中的类型一致
DATA GS_MARD TYPE ZMARD_STR .
*SELECTION SCREEN
PARAMETERS P_WERKS LIKE GS_MARD-WERKS OBLIGATORY .
SELECT-OPTIONS S_LGORT FOR GS_MARD-LGORT .
SELECT-OPTIONS S_MATNR FOR GS_MARD-MATNR .
START-OF-SELECTION .
PERFORM GET_DATA .
END-OF-SELECTION .
SET PF-STATUS 'STATUS' .
IF GT_MARD IS NOT INITIAL .
PERFORM DISPLAY_DATA .
ELSE .
MESSAGE S001 DISPLAY LIKE 'E'.
ENDIF.
TOP-OF-PAGE ."表头事件
PERFORM TOP_OF_PAGE .
AT USER-COMMAND .
PERFORM USER_COMMAND .
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM get_data .
SELECT WERKS LGORT MATNR LABST
FROM MARD
INTO CORRESPONDING FIELDS OF TABLE GT_MARD
WHERE WERKS = P_WERKS
AND LGORT IN S_LGORT
AND MATNR IN S_MATNR .
SORT GT_MARD BY WERKS LGORT .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form DISPLAY_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM display_data .
WRITE:/1(65) SY-ULINE .
LOOP AT GT_MARD INTO GS_MARD .
WRITE:/1 SY-VLINE NO-GAP,(10) GS_MARD-WERKS NO-GAP,
SY-VLINE NO-GAP,(10) GS_MARD-LGORT NO-GAP,
SY-VLINE NO-GAP,(20) GS_MARD-MATNR NO-GAP,
SY-VLINE NO-GAP,(20) GS_MARD-LABST NO-GAP,
SY-VLINE .
WRITE:/1(65) SY-ULINE .
ENDLOOP .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form TOP_OF_PAGE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM top_of_page .
WRITE:/1(65) SY-ULINE .
WRITE:/1 SY-VLINE NO-GAP,(10) '工厂' NO-GAP,
SY-VLINE NO-GAP,(10) '仓库' NO-GAP,
SY-VLINE NO-GAP,(20) '物料' NO-GAP,
SY-VLINE NO-GAP,(20) '数量' NO-GAP,
SY-VLINE .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form USER_COMMAND
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM user_command .
CASE SY-UCOMM .
WHEN 'BACK' .
LEAVE TO SCREEN 0 .
WHEN 'EXIT' OR 'CANCEL' .
LEAVE PROGRAM .
WHEN 'SORTUP' .
"升序
WHEN 'SORTDOWN' .
"降序
WHEN 'DOWNLOAD' .
WHEN 'CALLSF' ."调用smartforms
PERFORM CALL_SF .
ENDCASE .
ENDFORM.
③在T-CODE:SMARTFORMS 里面创建打印的样式设计:
这里用到强制分页,当物料库位不同时分页。首先对需要强制分页的变量进行全局定义。
注意这里创建好后激活执行测试,记下函数名:
④在程序里面调用
*SMARTFORMS 调用
DATA GV_FM_NAME TYPE RS38L_FNAM . "函数名称,函数模块中的导出参数类型一致
FORM call_sf .
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = 'ZMATERIAL_01' "SMARTFORMS 的名称
* VARIANT = ' '
* DIRECT_CALL = ' '
IMPORTING
FM_NAME = GV_FM_NAME
EXCEPTIONS
NO_FORM = 1
NO_FUNCTION_MODULE = 2
OTHERS = 3
.
IF sy-subrc <> 0.
* Implement suitable error handling here
MESSAGE S000 WITH 'SMARTFORMS未被调用!' .
EXIT .
ENDIF.
CALL FUNCTION GV_FM_NAME " '/1BCDWB/SF00000074'
* EXPORTING
* ARCHIVE_INDEX =
* ARCHIVE_INDEX_TAB =
* ARCHIVE_PARAMETERS =
* CONTROL_PARAMETERS =
* MAIL_APPL_OBJ =
* MAIL_RECIPIENT =
* MAIL_SENDER =
* OUTPUT_OPTIONS =
* USER_SETTINGS = 'X'
* IMPORTING
* DOCUMENT_OUTPUT_INFO =
* JOB_OUTPUT_INFO =
* JOB_OUTPUT_OPTIONS =
TABLES
it_mard = GT_MARD
EXCEPTIONS
FORMATTING_ERROR = 1
INTERNAL_ERROR = 2
SEND_ERROR = 3
USER_CANCELED = 4
OTHERS = 5
.
IF sy-subrc <> 0.
* Implement suitable error handling here
MESSAGE S000 WITH 'SMARTFORMS未被调用!' .
EXIT .
ENDIF.
ENDFORM.
这里提一个额外的,如果不想出现下面的提示栏,可以在程序里面写上默认。
*给form修改打印参数
DATA: LS_SSFCTRLOP TYPE SSFCTRLOP."定义CONTROL_PARAMETERS参数
DATA: LS_SSFCOMPOP TYPE SSFCOMPOP."定义OUTPUT_OPTIONS参数
*定义变量接受function名字
DATA: LV_FORM TYPE RS38L_FNAM."函数模块中的导出参数类型一致
*给smartform的打印参数赋值
LS_SSFCTRLOP-NO_DIALOG = 'X'."无弹窗
LS_SSFCTRLOP-PREVIEW = 'X'."预览
LS_SSFCOMPOP-TDDEST = 'LP01'."output参数