业务需求:
在SAP项目中,会经常碰到如下需求:
:: ALV的选择屏幕界面中,需要添加如下功能
: 自定义按钮模板下载
: 选择屏幕文件上载
实现步骤:模板下载
一、选择屏幕工具栏按钮声明
二、设置按钮参数(文本描述等)
三、AT SELECTION-SCREEN事件添加响应功能
主要功能:
1:点击模板下载按钮后,触发功能码FC01,运行FROM(FRM_EXPORT_TEMPLATE)。
2:弹出路径选择框,选择模板(SMWO已导入模板) 下载路径,下载模板到本地。
实现步骤:数据上载
1:模板下载完毕后,操作人员按照模板要求填充数据后,点击选择屏幕的文件路径;弹出文件选择对话框,选择文件。
2:点击执行后,数据上载到程序。
实现代码:
*----------------------------------------------------------------------*
* DESC: 自定义按钮
*----------------------------------------------------------------------*
TABLES: SSCRFIELDS.
SELECTION-SCREEN FUNCTION KEY 1. "模板下载按钮
*----------------------------------------------------------------------*
* DESC: INITIALIZATION 事件
*----------------------------------------------------------------------*
INITIALIZATION.
**初始化操作
SSCRFIELDS-FUNCTXT_01 = '模板下载'.
*----------------------------------------------------------------------*
* DESC: AT SELECTION-SCREEN 事件
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
IF SSCRFIELDS-UCOMM EQ 'FC01'.
****下载模板
PERFORM FRM_EXPORT_TEMPLATE.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
**获取数据文件路径
PERFORM FRM_GET_FILE.
*----------------------------------------------------------------------*
* DESC: START-OF-SELECTION 事件
*----------------------------------------------------------------------*
START-OF-SELECTION.
**获取数据
PERFORM FRM_GET_DATA.
*&---------------------------------------------------------------------*
*& Form FRM_GET_FILE
*&---------------------------------------------------------------------*
*& 获取数据文件路径
*&---------------------------------------------------------------------*
FORM FRM_GET_FILE .
DATA: L_RC TYPE I,
L_FILETABLE TYPE FILETABLE.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
EXPORTING
WINDOW_TITLE = '请选择导入文件'
FILE_FILTER = CNS_OPEN_FILTER
MULTISELECTION = SPACE
CHANGING
FILE_TABLE = L_FILETABLE
RC = L_RC
EXCEPTIONS
FILE_OPEN_DIALOG_FAILED = 1
CNTL_ERROR = 2
ERROR_NO_GUI = 3
NOT_SUPPORTED_BY_GUI = 4
OTHERS = 5.
IF SY-SUBRC = 0 AND L_RC = 1.
READ TABLE L_FILETABLE INTO P_FILE INDEX 1.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& 数据上载
*&---------------------------------------------------------------------*
FORM FRM_GET_DATA .
DATA: LT_EXC_DATA TYPE TABLE OF ALSMEX_TABLINE,
LS_EXC_DATA TYPE ALSMEX_TABLINE.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
FILENAME = P_FILE
I_BEGIN_COL = 2 "开始列
I_BEGIN_ROW = 4 "开始行
I_END_COL = 12 "结束列
I_END_ROW = 100 "结束行
TABLES
INTERN = LT_EXC_DATA[]
EXCEPTIONS
INCONSISTENT_PARAMETERS = 1
UPLOAD_OLE = 2
OTHERS = 3.
IF SY-SUBRC <> 0. "上传失败
MESSAGE '读取数据失败,请重选择文件!' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ELSE.
IF LT_EXC_DATA[] IS INITIAL.
MESSAGE '读取数据成功,单文件中无数据!' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
SORT LT_EXC_DATA BY ROW ASCENDING "行
COL ASCENDING."列
CLEAR GS_ITEM.
LOOP AT LT_EXC_DATA INTO LS_EXC_DATA.
CASE LS_EXC_DATA-COL.
WHEN 1."虚拟订单号
GS_ITEM-ZXNDD = LS_EXC_DATA-VALUE.
WHEN 2."订单类型
GS_ITEM-BSART = LS_EXC_DATA-VALUE.
WHEN 3."公司代码
GS_ITEM-BUKRS = LS_EXC_DATA-VALUE.
WHEN 4."采购组织
GS_ITEM-EKORG = LS_EXC_DATA-VALUE.
WHEN 5."采购组
GS_ITEM-EKGRP = LS_EXC_DATA-VALUE.
WHEN 6."供应商
GS_ITEM-LIFNR = LS_EXC_DATA-VALUE.
WHEN 7."行项目
GS_ITEM-EBELP = LS_EXC_DATA-VALUE.
WHEN 8."物料
GS_ITEM-MATNR = LS_EXC_DATA-VALUE.
WHEN 9."数量
GS_ITEM-MENGE = LS_EXC_DATA-VALUE.
WHEN 10."单位
GS_ITEM-MEINS = LS_EXC_DATA-VALUE.
WHEN 11."工厂
GS_ITEM-WERKS = LS_EXC_DATA-VALUE.
ENDCASE.
****在每一行最后一列
AT END OF ROW.
APPEND GS_ITEM TO GT_ITEM.
CLEAR GS_ITEM.
ENDAT.
ENDLOOP.
ENDFORM.