打印入门参照博客:
SAP ABAP smartforms 创建并实现调用源码(上)
SAP ABAP smartforms 创建并实现调用源码(下)
PS:非必要的需求 ,导出 PDF 文件可以点击 打印预览》转到》PDF 预览 ,弹出保存 PDF 文件。
一:核心代码
打印需要打开和关闭打印窗口。
导出不需要这两个方法。
CALL FUNCTION lv_fm_name
EXPORTING
control_parameters = gs_ssfctrlop
output_options = ls_output_options
ls_student = gs_student
IMPORTING
job_output_info = job_output_info "导出需要,打印不需要
TABLES
it_student = it_student
EXCEPTIONS
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
OTHERS = 5.
二:全部源码
*&---------------------------------------------------------------------*
*& Report ZTEST_EXPORT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTEST_EXPORT.
*----------------------------------------------------------------------*
* Type-pools/定义类型池
*----------------------------------------------------------------------*
TYPE-POOLS: slis.
*----------------------------------------------------------------------*
* Tables/声明数据库表
*----------------------------------------------------------------------*
TABLES:ZMMTEST.
*----------------------------------------------------------------------*
* Type/自定义类型
*----------------------------------------------------------------------*
TYPES:BEGIN OF ty_alv,
sel type char1, "是否被选择
id type zmmtest-id,
name type zmmtest-name,
age type zmmtest-age,
sex type zmmtest-sex,
hobby type zmmtest-hobby,
"include TYPE zmmtest,
times type numc4, "打印次数
END OF ty_alv.
DATA:gt_alv TYPE STANDARD TABLE OF ty_alv,
gs_alv TYPE ty_alv,
gt_print type standard table of ty_alv,
gs_student type zmmtest,
it_student type TABLE OF zmmtest.
DATA:gv_fm_name TYPE rs38l_fnam,
uv_name type tdsfname,
p_flag type char1.
data:ls_ssfcompop type ssfcompop,
ls_ssfctrlop type ssfctrlop,
ls_ssfcrescl type ssfcrescl.
*-------------------------------------*
* Define the Macros/定义
*----------------------------------------------------------------------*
DEFINE set_fieldcat.
CLEAR gs_fcat_lvc .
gs_fcat_lvc-fieldname = &1."内表的字段
gs_fcat_lvc-outputlen = &2."输出长度
gs_fcat_lvc-scrtext_l = &3."在ALV里面显示的名字
gs_fcat_lvc-just = &4."水平对齐方式,L左对齐,R右对齐。
gs_fcat_lvc-no_zero = &5."去除前导零
APPEND gs_fcat_lvc TO gt_fcat_lvc .
END-OF-DEFINITION.
*----------------------------------------------------------------------*
* 声明ALV对象
*----------------------------------------------------------------------*
DATA:gs_layout_lvc TYPE lvc_s_layo,
gt_fcat_lvc TYPE lvc_t_fcat,
gs_fcat_lvc TYPE lvc_s_fcat.
*----------------------------------------------------------------------*
* Internal table and work area/定义内表和工作区
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Selection screen/定义屏幕
*----------------------------------------------------------------------*
SELECTION-SCREEN FUNCTION KEY: 1 .
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE TEXT-t01.
SELECT-OPTIONS: s_id FOR zmmtest-id,
s_name FOR zmmtest-name,
s_age FOR zmmtest-age,
s_sex FOR zmmtest-sex,
s_hobby FOR zmmtest-hobby.
SELECTION-SCREEN END OF BLOCK block1.
*----------------------------------------------------------------------*
* Initialization/初始事件
*----------------------------------------------------------------------*
INITIALIZATION.
*----------------------------------------------------------------------*
* At selection-screen/屏幕事件
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN OUTPUT
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
*----------------------------------------------------------------------*
* Start-of-selection/开始选择事件
*----------------------------------------------------------------------*
START-OF-SELECTION.
*取数据
PERFORM frm_get_data.
*ALV展示
PERFORM frm_display_alv.
*----------------------------------------------------------------------*
* End-of-selection/结束选择事件
*----------------------------------------------------------------------*
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM frm_get_data .
SELECT *
INTO CORRESPONDING FIELDS OF TABLE gt_alv
from zmmtest
where id in s_id
AND name in s_name
AND age in s_age
AND sex in s_sex
AND hobby in s_hobby.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM frm_display_alv .
set_fieldcat:'ID' '' '学号' '' '',
'NAME' '' '学生姓名' '' '',
'AGE' '' '年龄' '' '',
'SEX' '' '性别' '' '',
'HOBBY' '' '兴趣爱好' '' ''.
CLEAR:gs_layout_lvc.
gs_layout_lvc-zebra = 'X'.
gs_layout_lvc-cwidth_opt = 'X'.
gs_layout_lvc-box_fname = 'SEL'. "选择框
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'FRM_PF_STATUS'
i_callback_user_command = 'FRM_USER_COMMAND'
is_layout_lvc = gs_layout_lvc
it_fieldcat_lvc = gt_fcat_lvc
i_save = 'A'
TABLES
t_outtab = gt_alv
EXCEPTIONS
program_error = 1
OTHERS = 2.
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_PF_STATUS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_pf_status USING pt_extab TYPE slis_t_extab .
SET PF-STATUS 'STATUS' .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_USER_COMMAND
*&---------------------------------------------------------------------*
* 自定义的用户命令处理
*----------------------------------------------------------------------*
* -->PV_UCOMM 用户命令类型
* -->PS_SELFIELD 用户命令信息
*----------------------------------------------------------------------*
form frm_user_command using pv_ucomm type sy-ucomm
ps_selfield type slis_selfield.
data: lr_grid type ref to cl_gui_alv_grid.
* ps_selfield-refresh = 'X'.
* ps_selfield-col_stable = 'X'.
* ps_selfield-row_stable = 'X'.
data : lv_stable type lvc_s_stbl.
lv_stable-row = '1'.
lv_stable-col = '1'.
call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'
importing
e_grid = lr_grid.
call method lr_grid->check_changed_data.
CLEAR:p_flag.
case pv_ucomm.
when '&PRINT'.
p_flag = 'X'.
perform frm_print_data.
when 'PDF'.
perform frm_print_data.
when 'ALL'.
perform frm_select_all.
when 'SAL'.
perform frm_deselect_all.
when '&IC1'.
read table gt_alv index ps_selfield-tabindex into gs_alv. "获取单击行
if ps_selfield-fieldname = 'SEL' and gs_alv-sel = ''.
gs_alv-sel = 'X'.
modify gt_alv from gs_alv transporting sel where id = gs_alv-id.
elseif ps_selfield-fieldname = 'SEL' and gs_alv-sel = 'X'.
gs_alv-sel = ''.
modify gt_alv from gs_alv transporting sel where id = gs_alv-id.
endif.
endcase.
*刷新
call method lr_grid->refresh_table_display
exporting
is_stable = lv_stable.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_SELECT_ALL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_select_all .
loop at gt_alv into gs_alv.
gs_alv-sel = 'X'.
modify gt_alv from gs_alv index sy-tabix transporting sel.
endloop.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_DESELECT_ALL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_deselect_all .
loop at gt_alv into gs_alv.
gs_alv-sel = ''.
modify gt_alv from gs_alv index sy-tabix transporting sel .
endloop.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_PRINT_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_print_data .
refresh gt_print.
loop at gt_alv into gs_alv where sel = 'X'.
append gs_alv to gt_print.
endloop.
if sy-langu = '1'.
if gt_print is initial.
message '请选择需打印的凭证' type 'E' display like 'S'.
endif.
else.
if gt_print is initial.
message 'Please select the certificate to print' type 'E' display like 'S'.
endif.
endif.
*获取数据
perform frm_get_print_data.
IF p_flag = 'X'.
perform frm_close_print_window.
ENDIF.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_SELECT_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_select_data .
"根据实际查询需要
endform.
*&---------------------------------------------------------------------*
*& Form FRM_OPEN_PRINT_WINDOW
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_open_print_window .
ls_ssfctrlop-no_open = 'X'.
ls_ssfctrlop-no_close = 'X'.
ls_ssfctrlop-langu = sy-langu.
*****打开打印窗口
call function 'SSF_OPEN'
exporting
user_settings = ''
output_options = ls_ssfcompop
control_parameters = ls_ssfctrlop
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.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_CLOSE_PRINT_WINDOW
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_close_print_window .
*****关闭打印窗口
call function 'SSF_CLOSE'
importing
job_output_info = ls_ssfcrescl
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.
*统计打印次数
if ls_ssfcrescl-outputdone = 'X'.
loop at gt_print into gs_alv.
*更改ALV数据
gs_alv-times = gs_alv-times + 1.
modify gt_alv from gs_alv transporting times where id = gs_alv-id.
if sy-langu = '1'.
message '打印成功' type 'S'.
else.
message 'Print success' type 'S'.
endif.
endloop.
endif.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_GET_PRINT_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_get_print_data .
IF gt_print is initial.
IF sy-langu = '1'.
message '请选择需打印的凭证' type 'E' display like 'S'.
ELSE.
message 'Please select the certificate to print' type 'E' display like 'S'.
ENDIF.
ENDIF.
IF p_flag = 'X'.
perform frm_open_print_window.
ENDIF.
loop at gt_print into gs_alv.
CLEAR:uv_name.
*打印
gs_student = CORRESPONDING #( gs_alv ).
it_student = CORRESPONDING #( gt_alv ).
"这里根据条件赋值不同模板
if sy-langu = '1'.
uv_name = 'ZMMFTEST'.
else.
uv_name = 'ZMMFTEST_EN'.
endif.
IF p_flag = 'X'.
perform frm_print.
ELSE.
perform frm_print_pdf.
ENDIF.
endloop.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_PRINT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_print.
****打印
* 根据SmartForm 名称获得Form的 Function Names
call function 'SSF_FUNCTION_MODULE_NAME'
exporting
formname = uv_name
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
control_parameters = ls_ssfctrlop
ls_student = gs_student
tables
it_student = it_student
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.
endform.
FORM frm_print_pdf.
DATA:gs_ssfctrlop TYPE ssfctrlop,
lv_fm_name TYPE rs38l_fnam,
gv_filter TYPE string.
DATA:len TYPE i. "pdf文件大小
DATA:job_output_info TYPE ssfcrescl.
DATA:lv_bin_filesize TYPE i,
it_docs TYPE STANDARD TABLE OF docs,
lv_fullpath TYPE string,
it_lines TYPE STANDARD TABLE OF tline,
lv_filename TYPE string,
ls_output_options TYPE ssfcompop.
DATA:lv_guiobj TYPE REF TO cl_gui_frontend_services,
lv_filter TYPE string,
lv_name TYPE string,
lv_path TYPE string,
lv_uact TYPE i,
lv_language TYPE sflangu VALUE 'E',
lv_e_devtype TYPE rspoptype,
lv_output_options TYPE ssfcompop.
CREATE OBJECT lv_guiobj.
DATA BEGIN OF pdf OCCURS 10.
INCLUDE STRUCTURE tline.
DATA END OF pdf.
gs_ssfctrlop-getotf = 'X'.
gs_ssfctrlop-no_dialog = 'X'.
gv_filter = 'PDF (*.pdf)'.
CALL FUNCTION 'SSF_GET_DEVICE_TYPE'
EXPORTING
i_language = lv_language
i_application = 'SAPDEFAULT'
IMPORTING
e_devtype = lv_e_devtype.
ls_output_options-tddest = 'LP01'.
ls_output_options-tdprinter = lv_e_devtype.
* ***打印
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = uv_name
IMPORTING
fm_name = lv_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 lv_fm_name
EXPORTING
control_parameters = gs_ssfctrlop
output_options = ls_output_options
ls_student = gs_student
IMPORTING
job_output_info = job_output_info
TABLES
it_student = it_student
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.
* convert smartforms to PDF
CALL FUNCTION 'CONVERT_OTF_2_PDF'
IMPORTING
bin_filesize = lv_bin_filesize
TABLES
otf = job_output_info-otfdata
doctab_archive = it_docs
lines = it_lines
EXCEPTIONS
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
OTHERS = 4.
lv_filename = gs_student-name.
CALL METHOD lv_guiobj->file_save_dialog
EXPORTING
default_extension = 'PDF'
default_file_name = lv_filename
file_filter = gv_filter
CHANGING
filename = lv_name
path = lv_path
fullpath = lv_fullpath
user_action = lv_uact.
IF lv_uact = lv_guiobj->action_cancel.
EXIT.
ELSE.
lv_filename = lv_fullpath.
ENDIF.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
bin_filesize = lv_bin_filesize
filename = lv_filename
filetype = 'BIN'
TABLES
data_tab = it_lines
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
OTHERS = 22.
MESSAGE '下载PDF成功(Download successfully)' TYPE 'S'.
ENDFORM.
版权声明:本文为qq_41757790原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。