货铺QQ群号:834508274
进群统一修改群名片,例如BJ_ABAP_森林木。群内禁止发广告及其他一切无关链接,小程序等,进群看公告,谢谢配合
不修改昵称会被不定期踢除,谢谢配合
有批导需求的时候,一般是首先下载模板,然后再上载。
下载模板的方式,通常使用SMW0先上载,再在程序中下载使用。
另外还可以考虑下面的方式:
首先建一个模板字段结构:(也可以使用其他方式实现,我偷懒都是建结构)
Excel模板如下:
执行
参考代码:
*&---------------------------------------------------------------------*
*& Report ZLM_DOWM_TMP01
*&
*&---------------------------------------------------------------------*
*&
*& 下载模板demo sap干货铺 sapliumeng
*&---------------------------------------------------------------------*
REPORT ZLM_DOWM_TMP02.
TABLES:SSCRFIELDS.
TYPE-POOLS:SLIS,ISOC.
SELECTION-SCREEN:FUNCTION KEY 1.
PARAMETERS :P_FILE LIKE RLGRAP-FILENAME .
INITIALIZATION.
SSCRFIELDS-FUNCTXT_01 = '下载模板'.
AT SELECTION-SCREEN.
CASE SSCRFIELDS-UCOMM.
WHEN 'FC01'.
PERFORM FRM_DOWN_TMPLT USING 'ZLMS_ALV'.
WHEN 'ONLI'.
WHEN OTHERS.
ENDCASE.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
PERFORM FRM_FILE_OPEN CHANGING P_FILE.
FORM FRM_DOWN_TMPLT USING PV_TMPNAME.
FIELD-SYMBOLS: <F01> TYPE ANY.
FIELD-SYMBOLS: <F02> TYPE ANY.
DATA:
L_FLAGS TYPE C,
L_STR TYPE STRING,
L_COLID TYPE I,
L_FILE TYPE RLGRAP-FILENAME,
L_DEFAULT_FILENAME TYPE STRING,
L_RESULT TYPE ABAP_BOOL,
L_INFO TYPE STRING,
L_ANSWER TYPE C,
L_RC TYPE I,
L_USER_FILENAME TYPE STRING,
L_USER_FILEPATH TYPE STRING,
L_USER_FULLPATH TYPE STRING,
L_USER_ACTION TYPE I,
L_TEMPLATE_PATH TYPE RLGRAP-FILENAME.
DATA:
BEGIN OF LS_DD04T,
FIELDNAME TYPE DD03L-FIELDNAME,
POSITION TYPE DD03L-POSITION,
DDTEXT TYPE DD04T-DDTEXT,
END OF LS_DD04T,
LT_DD04T LIKE TABLE OF LS_DD04T,
BEGIN OF LS_TITLE,
TITLE1 TYPE DD04T-DDTEXT,
TITLE2 TYPE DD04T-DDTEXT,
TITLE3 TYPE DD04T-DDTEXT,
TITLE4 TYPE DD04T-DDTEXT,
TITLE5 TYPE DD04T-DDTEXT,
TITLE6 TYPE DD04T-DDTEXT,
TITLE7 TYPE DD04T-DDTEXT,
END OF LS_TITLE,
LT_TITLE LIKE TABLE OF LS_TITLE.
L_DEFAULT_FILENAME = 'SAP干货铺导入模板.xlsx'.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST
EXPORTING
FILE = L_DEFAULT_FILENAME
RECEIVING
RESULT = L_RESULT.
IF L_RESULT EQ 'X'.
CONCATENATE '[' L_DEFAULT_FILENAME ']已经存在,要替换吗?' INTO L_INFO SEPARATED BY SPACE.
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
TITLEBAR = '已存在'
TEXT_QUESTION = L_INFO
TEXT_BUTTON_1 = '是'
DISPLAY_CANCEL_BUTTON = ''
IMPORTING
ANSWER = L_ANSWER
EXCEPTIONS
TEXT_NOT_FOUND = 1
OTHERS = 2.
IF L_ANSWER EQ '1'.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_DELETE
EXPORTING
FILENAME = L_DEFAULT_FILENAME
CHANGING
RC = L_RC.
ELSE.
RETURN.
ENDIF.
ENDIF.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
EXPORTING
WINDOW_TITLE = '请选择保存的路径及文件名'
DEFAULT_EXTENSION = '.xlsx'
DEFAULT_FILE_NAME = L_DEFAULT_FILENAME
FILE_FILTER = '.xlsx'
CHANGING
FILENAME = L_USER_FILENAME
PATH = L_USER_FILEPATH
FULLPATH = L_USER_FULLPATH
USER_ACTION = L_USER_ACTION.
IF L_USER_ACTION = 0.
REPLACE '..' WITH '.' INTO L_USER_FILENAME.
REPLACE '..' WITH '.' INTO L_USER_FULLPATH.
L_TEMPLATE_PATH = L_USER_FULLPATH.
ENDIF.
CHECK L_TEMPLATE_PATH IS NOT INITIAL.
CLEAR: LT_TITLE[],LT_DD04T[].
SELECT
DD03L~FIELDNAME
DD03L~POSITION
DD04T~DDTEXT INTO TABLE LT_DD04T[]
FROM DD03L
INNER JOIN DD04T ON DD03L~ROLLNAME = DD04T~ROLLNAME
WHERE DD03L~TABNAME = PV_TMPNAME AND
DD03L~FIELDNAME <> 'MANDT' AND
DD04T~DDLANGUAGE = SY-LANGU AND
DD03L~AS4LOCAL = 'A'.
SORT LT_DD04T[] BY POSITION.
****&下载字段名称
*** L_COLID = 1.
*** LOOP AT LT_DD04T[] INTO LS_DD04T.
*** L_STR = L_COLID.
*** CONDENSE: L_STR.
*** CONCATENATE 'LS_TITLE-TITLE' L_STR INTO L_STR.
*** ASSIGN (L_STR) TO <F01>.
***
*** <F01> = LS_DD04T-FIELDNAME.
*** L_COLID = L_COLID + 1.
*** ENDLOOP.
***
*** APPEND LS_TITLE TO LT_TITLE[].
*&下载字段描述
L_COLID = 1.
LOOP AT LT_DD04T[] INTO LS_DD04T.
L_STR = L_COLID.
CONDENSE: L_STR.
CONCATENATE 'LS_TITLE-TITLE' L_STR INTO L_STR.
ASSIGN (L_STR) TO <F01>.
<F01> = LS_DD04T-DDTEXT.
L_COLID = L_COLID + 1.
ENDLOOP.
APPEND LS_TITLE TO LT_TITLE[].
IF LT_TITLE[] IS NOT INITIAL.
CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
EXPORTING
I_FILENAME = L_TEMPLATE_PATH
TABLES
I_TAB_SAP_DATA = LT_TITLE[].
MESSAGE L_TEMPLATE_PATH TYPE 'S'.
ENDIF.
ENDFORM. " FRM_DOWN_TMPLT