SAP根据excel表格数据将数据导入表中
一、创建表
首先你得有一个表,如果没有那就新建一个。
事务代码SE11,输入表名,创建。
交付类选择C,客户表,选择允许显示和维护。
添加字段,数据元素可以选择已有的,也可以新建。
点击保存,如何激活。
数据类根据你的表类型来选,这里我选的是事务数据,透明表,大小范畴根据你的表数据来选。
点击保存。
然后激活表,报警告不用管他。
表建好了。
二、编写程序
首先这是我的excel格式。
关键的代码如下。
FORM GETEXCEL .
G_FILE = P_FILE. "注意两个文件用于不同的函数,所以类型不同
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
FILENAME = G_FILE
I_BEGIN_COL = 1
I_BEGIN_ROW = 1
I_END_COL = 2
I_END_ROW = 10000
TABLES
INTERN = ITAB[].
DELETE ITAB WHERE ROW = 1. "删除字段名行
LOOP AT ITAB.
ON CHANGE OF ITAB-ROW.
IF SY-TABIX NE 1.
APPEND WTAB.
CLEAR WTAB.
ENDIF.
ENDON.
ASSIGN COMPONENT ITAB-COL OF STRUCTURE WTAB TO <FS>. "动态方法将值传到相应的内表
<FS> = ITAB-VALUE.
ENDLOOP.
APPEND WTAB. "这句不要忘记,要把最后一条APPEND到内表中
SORT WTAB BY LIFNR.
ENDFORM. " GETEXCEL
这段代码是获取excel表格里的数据,将其存入创建的内表里。
FORM USER_COMMAND USING RF_UCOMM LIKE SY-UCOMM
RS_SELFIELD TYPE SLIS_SELFIELD. "#EC CALLED
CASE RF_UCOMM.
WHEN '&PRT'.
*****插入数据
DELETE FROM ZIF_MATLFN2SFWMS.
COMMIT WORK AND WAIT.
"去重防错
DELETE ADJACENT DUPLICATES FROM WTAB COMPARING LIFNR.
INSERT ZIF_MATLFN2SFWMS FROM TABLE WTAB." ACCEPTING DUPLICATE KEYS.
DESCRIBE TABLE WTAB LINES COUNT.
MESSAGE S001(00) WITH '成功上传' COUNT '条'.
ENDCASE.
ENDFORM. "USER_COMMAND
点击上传按钮后,将数据上传到表里,设置了一个上传按钮,需要用户自己在GUI屏幕建一个按钮。
保存,激活程序,运行。
输入文件路径,会先显示excel表格里的数据,点击上传按钮后,将数据上传到透明表中。
使用事务代码SE16/SE16N/SE11,可以查看表结构和数据。
数据成功上传。
三、模板文件
上传excel表格数据时,需要严格按照excel的表格格式来设置数据,因为在调用函数获取excel数据时,就已经定义好了表格格式,所以需要一个模板文件,让用户知道怎么设置标准的表格格式。
FORM DOWNLOAD_DATA USING PDESCFILE PMODELFILE .
*&---下载模板
PERFORM FRM_GET_FULLPATH CHANGING GV_FULLPATH GV_PATH GV_NAME PMODELFILE.
*&---路径为空则退出
IF GV_FULLPATH IS INITIAL.
MESSAGE '用户取消操作' TYPE 'S'.
RETURN.
ENDIF.
PERFORM FRM_DOWN USING GV_FULLPATH PMODELFILE.
ENDFORM. " DOWNLOAD_DATA
*&---------------------------------------------------------------------*
*& Form FRM_GET_FULLPATH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GV_FULLPATH text
* <--P_GV_PATH text
* <--P_GV_NAME text
*----------------------------------------------------------------------*
FORM FRM_GET_FULLPATH CHANGING PV_FULLPATH TYPE STRING
PV_PATH TYPE STRING
PV_NAME TYPE STRING
PV_MODEL TYPE STRING.
DATA: LV_INIT_PATH TYPE STRING,
LV_INIT_FNAME TYPE STRING,
LV_PATH TYPE STRING,
LV_FILENAME TYPE STRING,
LV_FULLPATH TYPE STRING.
*&---初始名称(输出的文件名称)
* concatenate 'Material_Doc_' SY-DATUM '.xslx' into L_INIT_FNAME.
LV_INIT_FNAME = PV_MODEL.
* 获取桌面路径
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GET_DESKTOP_DIRECTORY
CHANGING
DESKTOP_DIRECTORY = LV_INIT_PATH
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
*&---用户选择名称、路径
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
EXPORTING
WINDOW_TITLE = PV_NAME
DEFAULT_EXTENSION = 'XLSX'
DEFAULT_FILE_NAME = LV_INIT_FNAME
* FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_EXCEL
* FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_WORD
INITIAL_DIRECTORY = LV_INIT_PATH
PROMPT_ON_OVERWRITE = 'X'
CHANGING
FILENAME = LV_FILENAME
PATH = LV_PATH
FULLPATH = LV_FULLPATH
* USER_ACTION =
* FILE_ENCODING =
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
IF SY-SUBRC = 0.
PV_FULLPATH = LV_FULLPATH.
PV_PATH = LV_PATH.
ENDIF.
ENDFORM. " FRM_GET_FULLPATH
*--------------------------------------------------------------------*
* 下载文件
*--------------------------------------------------------------------*
FORM FRM_DOWN USING PR_FILENAME PMUBAN.
DATA: LV_OBJDATA LIKE WWWDATATAB,
LV_MIME LIKE W3MIME,
LV_DESTINATION LIKE RLGRAP-FILENAME,
LV_OBJNAM TYPE STRING,
LV_RC LIKE SY-SUBRC,
LV_ERRTXT TYPE STRING.
DATA: LV_FILENAME TYPE STRING,
LV_RESULT,
LV_SUBRC TYPE SY-SUBRC.
DATA: LV_OBJID TYPE WWWDATATAB-OBJID .
LV_OBJID = PMUBAN. "上传的模版名称
*&---查找文件是否存在。
SELECT SINGLE RELID OBJID
FROM WWWDATA
INTO CORRESPONDING FIELDS OF LV_OBJDATA
WHERE SRTF2 = 0
AND RELID = 'MI'
AND OBJID = LV_OBJID.
*&---判断模版不存在则报错
IF SY-SUBRC NE 0 OR LV_OBJDATA-OBJID EQ SPACE.
CONCATENATE '模板文件:' LV_OBJID '不存在,请用TCODE:SMW0进行加载'
INTO LV_ERRTXT.
MESSAGE LV_ERRTXT TYPE 'E'.
ENDIF.
LV_FILENAME = PR_FILENAME.
"判断本地地址是否已经存在此文件。
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST
EXPORTING
FILE = LV_FILENAME
RECEIVING
RESULT = LV_RESULT
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
WRONG_PARAMETER = 3
NOT_SUPPORTED_BY_GUI = 4
OTHERS = 5.
IF SY-SUBRC <> 0.
CASE SY-SUBRC.
WHEN 1.
WHEN 2.
WHEN OTHERS.
ENDCASE.
ENDIF.
IF LV_RESULT EQ 'X'. "如果存在则删除原始文件,重新覆盖
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_DELETE
EXPORTING
FILENAME = LV_FILENAME
CHANGING
RC = LV_SUBRC
EXCEPTIONS
FILE_DELETE_FAILED = 1
CNTL_ERROR = 2
ERROR_NO_GUI = 3
FILE_NOT_FOUND = 4
ACCESS_DENIED = 5
UNKNOWN_ERROR = 6
NOT_SUPPORTED_BY_GUI = 7
WRONG_PARAMETER = 8
OTHERS = 9.
IF SY-SUBRC <> 0.
CASE SY-SUBRC.
WHEN 1.
WHEN 2.
WHEN OTHERS.
ENDCASE.
ENDIF.
IF LV_SUBRC <> 0. "如果删除失败,则报错。
CONCATENATE '同名EXCEL文件已打开' '请关闭该EXCEL后重试。'
INTO LV_ERRTXT.
MESSAGE LV_ERRTXT TYPE 'E'.
ENDIF.
ENDIF.
LV_DESTINATION = PR_FILENAME.
*&---下载模版。
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
KEY = LV_OBJDATA
DESTINATION = LV_DESTINATION
IMPORTING
RC = LV_RC.
IF LV_RC NE 0.
CONCATENATE '模板文件' '下载失败' INTO LV_ERRTXT.
MESSAGE LV_ERRTXT TYPE 'E'.
ENDIF.
ENDFORM. "frm_down
要先将模板文件添加到事务代码SMW0中。
选择二进制数据,输入包,然后执行。
点左上角创建新的对象。
输入对象名称和描述,对象名称就是我们在程序里设置的。
CASE SSCRFIELDS-UCOMM.
WHEN 'BT1'.
L_FILENAME = 'ZIF_MATLFN2SFWMS.xlsx'.
L_MUBAN = 'ZIF_MATLFN2SFWMS'.
PERFORM DOWNLOAD_DATA USING L_FILENAME L_MUBAN.
ENDCASE.
L_FILENAME是下载模板文件时默认的文件名,L_MUBAN就是我们需要的对象名。
导入模板文件,记得把excel表格关闭,要不然会报错,然后选择包和请求,创建成功。
回到程序,点击模板下载,发现模板下载成功。
四、传输表和数据
表传输直接添加到请求就可以了,但是要注意这样只会传输表,而不会传输数据。
可以在生产环境运行程序,重新生产表格数据,也可以通过事务代码SM30来传输数据,这里我们试一下第二种方法。
进入SM30表维护,输入表名,选中全部数据。
左上角表视图,选择传输,选择请求将数据传输过去就可以了。
不过要记得表要设置为允许显示和和维护,否则不能使用表维护。
传输完请求后,来的生产环境,打开表,查看数据是否传输成功了。
传输成功。
然后我们再看一下程序运行后模板是否下载成功。
下载成功。
五、完整代码
*&---------------------------------------------------------------------*
*& Report ZSF_WMS_LIFNR
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZSF_WMS_LIFNR.
TABLES:ZIF_MATLFN2SFWMS,SSCRFIELDS.
*
*定义内表类型
*
TYPES: BEGIN OF W_ITAB,
LIFNR LIKE ZIF_MATLFN2SFWMS-LIFNR," 供应商
NAME1 LIKE ZIF_MATLFN2SFWMS-NAME1," 供应商名称
END OF W_ITAB.
FIELD-SYMBOLS: <FS>.
DATA:
FILETAB TYPE FILETABLE WITH HEADER LINE,
RC TYPE I,
G_FILE TYPE RLGRAP-FILENAME,
ITAB TYPE TABLE OF ALSMEX_TABLINE WITH HEADER LINE.
DATA: WTAB TYPE TABLE OF W_ITAB WITH HEADER LINE.
DATA: COUNT TYPE I.
DATA:STDATUM LIKE SY-DATUM,
STUZEIT LIKE SY-UZEIT.
DATA:L_FILENAME TYPE STRING,L_MUBAN TYPE STRING.
DATA:GV_FULLPATH TYPE STRING,GV_PATH TYPE STRING,GV_NAME TYPE STRING.
DATA:RUNNERCNT TYPE I,
SUMRCNT TYPE I.
DATA:PDATE TYPE SY-DATUM.
*&---------------------------------------------------------------------*
*& 选择界面
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME TITLE TXT1 .
PARAMETER: P_FILE TYPE STRING.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON 5(30) BUT1 USER-COMMAND BT1.
* SELECTION-SCREEN PUSHBUTTON 40(30) but2 USER-COMMAND bt2.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK BLK1.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN BEGIN OF BLOCK DESC WITH FRAME TITLE BLKTEXT1.
SELECTION-SCREEN COMMENT 1(78) TXT11.
SELECTION-SCREEN SKIP.
* SELECTION-SCREEN COMMENT 1(78) TXT2.
* SELECTION-SCREEN SKIP.
* SELECTION-SCREEN COMMENT 1(78) TXT3.
* SELECTION-SCREEN SKIP.
* SELECTION-SCREEN COMMENT 1(78) TXT4.
* SELECTION-SCREEN SKIP.
SELECTION-SCREEN COMMENT 1(78) TXT5.
SELECTION-SCREEN END OF BLOCK DESC.
INITIALIZATION.
TXT1 = '请输入条件'.
CALL FUNCTION 'ICON_CREATE'
EXPORTING
NAME = ICON_BW_REF_STRUCTURE_SAP
TEXT = '模板下载'
INFO = '模板下载'
IMPORTING
RESULT = BUT1
EXCEPTIONS
ICON_NOT_FOUND = 1
OUTPUTFIELD_TOO_SHORT = 2
OTHERS = 3.
PDATE = 20200222.CLEAR SUMRCNT.
PERFORM GETRUNSTATIC(ZPRT_FI_SCCB1L) USING PDATE SY-REPID CHANGING RUNNERCNT SUMRCNT.
TXT5 = '运行情况:总共' && RUNNERCNT && '用户运行了' && SUMRCNT && '次,'.
PDATE = SY-DATUM.PDATE+4(4) = '0101'.CLEAR SUMRCNT.
PERFORM GETRUNSTATIC(ZPRT_FI_SCCB1L) USING PDATE SY-REPID CHANGING RUNNERCNT SUMRCNT.
TXT5 = TXT5 && '本年度总共' && RUNNERCNT && '用户运行了' && SUMRCNT && '次,'.
PDATE = SY-DATUM.PDATE+6(2) = '01'.CLEAR SUMRCNT.
PERFORM GETRUNSTATIC(ZPRT_FI_SCCB1L) USING PDATE SY-REPID CHANGING RUNNERCNT SUMRCNT.
TXT5 = TXT5 && '本月度总共' && RUNNERCNT && '用户运行了' && SUMRCNT && '次'.
AT SELECTION-SCREEN.
CASE SSCRFIELDS-UCOMM.
WHEN 'BT1'.
L_FILENAME = 'ZIF_MATLFN2SFWMS.xlsx'.
L_MUBAN = 'ZIF_MATLFN2SFWMS'.
PERFORM DOWNLOAD_DATA USING L_FILENAME L_MUBAN.
ENDCASE.
*&---------------------------------------------------------------------
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
EXPORTING
DEFAULT_FILENAME = P_FILE
CHANGING
RC = RC
FILE_TABLE = FILETAB[].
READ TABLE FILETAB INDEX 1.
IF SY-SUBRC EQ 0.
P_FILE = FILETAB-FILENAME.
ENDIF.
START-OF-SELECTION.
STDATUM = SY-DATUM.
STUZEIT = SY-UZEIT.
IF P_FILE <> ''.
PERFORM GETEXCEL."EXCEL数据获取
ELSE.
PERFORM GETDATA."获取版本描述表中数据
ENDIF.
PERFORM WRITERPTRUNRECORD.
PERFORM DISPLAY."数据ALV输出
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form WRITERPTRUNRECORD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM WRITERPTRUNRECORD.
GET TIME.
CALL FUNCTION 'ZSYS_RPT_RUNRECORD'
EXPORTING
TCODE = SY-TCODE
PROGRAMM = SY-CPROG
* bukrs = '1000'
"werks = werks
DATUMRST = STDATUM
UZEITRST = STUZEIT
BATCH = SY-BATCH
DATUMREND = SY-DATUM
UZEITREND = SY-UZEIT
DATUMC = '20221219'
RUNNER = SY-UNAME
RPTSRC = 'xxx'.
ENDFORM. "WRITERPTRUNRECORD
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM DOWNLOAD_DATA USING PDESCFILE PMODELFILE .
*&---下载模板
PERFORM FRM_GET_FULLPATH CHANGING GV_FULLPATH GV_PATH GV_NAME PMODELFILE.
*&---路径为空则退出
IF GV_FULLPATH IS INITIAL.
MESSAGE '用户取消操作' TYPE 'S'.
RETURN.
ENDIF.
PERFORM FRM_DOWN USING GV_FULLPATH PMODELFILE.
ENDFORM. " DOWNLOAD_DATA
*&---------------------------------------------------------------------*
*& Form FRM_GET_FULLPATH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GV_FULLPATH text
* <--P_GV_PATH text
* <--P_GV_NAME text
*----------------------------------------------------------------------*
FORM FRM_GET_FULLPATH CHANGING PV_FULLPATH TYPE STRING
PV_PATH TYPE STRING
PV_NAME TYPE STRING
PV_MODEL TYPE STRING.
DATA: LV_INIT_PATH TYPE STRING,
LV_INIT_FNAME TYPE STRING,
LV_PATH TYPE STRING,
LV_FILENAME TYPE STRING,
LV_FULLPATH TYPE STRING.
*&---初始名称(输出的文件名称)
* concatenate 'Material_Doc_' SY-DATUM '.xslx' into L_INIT_FNAME.
LV_INIT_FNAME = PV_MODEL.
* 获取桌面路径
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GET_DESKTOP_DIRECTORY
CHANGING
DESKTOP_DIRECTORY = LV_INIT_PATH
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
*&---用户选择名称、路径
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
EXPORTING
WINDOW_TITLE = PV_NAME
DEFAULT_EXTENSION = 'XLSX'
DEFAULT_FILE_NAME = LV_INIT_FNAME
* FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_EXCEL
* FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_WORD
INITIAL_DIRECTORY = LV_INIT_PATH
PROMPT_ON_OVERWRITE = 'X'
CHANGING
FILENAME = LV_FILENAME
PATH = LV_PATH
FULLPATH = LV_FULLPATH
* USER_ACTION =
* FILE_ENCODING =
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
IF SY-SUBRC = 0.
PV_FULLPATH = LV_FULLPATH.
PV_PATH = LV_PATH.
ENDIF.
ENDFORM. " FRM_GET_FULLPATH
*--------------------------------------------------------------------*
* 下载文件
*--------------------------------------------------------------------*
FORM FRM_DOWN USING PR_FILENAME PMUBAN.
DATA: LV_OBJDATA LIKE WWWDATATAB,
LV_MIME LIKE W3MIME,
LV_DESTINATION LIKE RLGRAP-FILENAME,
LV_OBJNAM TYPE STRING,
LV_RC LIKE SY-SUBRC,
LV_ERRTXT TYPE STRING.
DATA: LV_FILENAME TYPE STRING,
LV_RESULT,
LV_SUBRC TYPE SY-SUBRC.
DATA: LV_OBJID TYPE WWWDATATAB-OBJID .
LV_OBJID = PMUBAN. "上传的模版名称
*&---查找文件是否存在。
SELECT SINGLE RELID OBJID
FROM WWWDATA
INTO CORRESPONDING FIELDS OF LV_OBJDATA
WHERE SRTF2 = 0
AND RELID = 'MI'
AND OBJID = LV_OBJID.
*&---判断模版不存在则报错
IF SY-SUBRC NE 0 OR LV_OBJDATA-OBJID EQ SPACE.
CONCATENATE '模板文件:' LV_OBJID '不存在,请用TCODE:SMW0进行加载'
INTO LV_ERRTXT.
MESSAGE LV_ERRTXT TYPE 'E'.
ENDIF.
LV_FILENAME = PR_FILENAME.
"判断本地地址是否已经存在此文件。
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST
EXPORTING
FILE = LV_FILENAME
RECEIVING
RESULT = LV_RESULT
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
WRONG_PARAMETER = 3
NOT_SUPPORTED_BY_GUI = 4
OTHERS = 5.
IF SY-SUBRC <> 0.
CASE SY-SUBRC.
WHEN 1.
WHEN 2.
WHEN OTHERS.
ENDCASE.
ENDIF.
IF LV_RESULT EQ 'X'. "如果存在则删除原始文件,重新覆盖
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_DELETE
EXPORTING
FILENAME = LV_FILENAME
CHANGING
RC = LV_SUBRC
EXCEPTIONS
FILE_DELETE_FAILED = 1
CNTL_ERROR = 2
ERROR_NO_GUI = 3
FILE_NOT_FOUND = 4
ACCESS_DENIED = 5
UNKNOWN_ERROR = 6
NOT_SUPPORTED_BY_GUI = 7
WRONG_PARAMETER = 8
OTHERS = 9.
IF SY-SUBRC <> 0.
CASE SY-SUBRC.
WHEN 1.
WHEN 2.
WHEN OTHERS.
ENDCASE.
ENDIF.
IF LV_SUBRC <> 0. "如果删除失败,则报错。
CONCATENATE '同名EXCEL文件已打开' '请关闭该EXCEL后重试。'
INTO LV_ERRTXT.
MESSAGE LV_ERRTXT TYPE 'E'.
ENDIF.
ENDIF.
LV_DESTINATION = PR_FILENAME.
*&---下载模版。
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
KEY = LV_OBJDATA
DESTINATION = LV_DESTINATION
IMPORTING
RC = LV_RC.
IF LV_RC NE 0.
CONCATENATE '模板文件' '下载失败' INTO LV_ERRTXT.
MESSAGE LV_ERRTXT TYPE 'E'.
ENDIF.
ENDFORM. "frm_down
*&---------------------------------------------------------------------*
*& FORM DISPLAY
*&---------------------------------------------------------------------*
* TEXT 数据展示
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM DISPLAY .
DATA: IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
IS_VARIANT LIKE DISVARIANT,
IT_EVENTS TYPE SLIS_T_EVENT,
IS_LAYOUT TYPE SLIS_LAYOUT_ALV,
TITLE TYPE LVC_TITLE,
I_CALLBACK_PROGRAM TYPE SY-REPID,
IT_SORT TYPE SLIS_T_SORTINFO_ALV WITH HEADER LINE
.
PERFORM FIELDCAT_INIT USING IT_FIELDCAT.
PERFORM LAYOUT_INIT USING IS_LAYOUT.
I_CALLBACK_PROGRAM = SY-REPID.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = I_CALLBACK_PROGRAM
I_GRID_TITLE = TITLE
IS_LAYOUT = IS_LAYOUT
I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS'
I_CALLBACK_USER_COMMAND = 'USER_COMMAND'
IT_FIELDCAT = IT_FIELDCAT
IT_EVENTS = IT_EVENTS
TABLES
T_OUTTAB = WTAB[]
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
ENDIF.
ENDFORM. " DISPLAY
*&--------------------------------------------------------------------- *
*& FORM G_STATUS_FORM 调用自定义屏幕
*&---------------------------------------------------------------------
FORM SET_PF_STATUS USING RT_EXTAB TYPE SLIS_T_EXTAB.
SET PF-STATUS 'STANDARD' EXCLUDING RT_EXTAB.
ENDFORM. "SET_PF_STATUS
*&---------------------------------------------------------------------*
*& FORM FIELDCAT_INIT
*&---------------------------------------------------------------------*
* TEXT 定义输出项
*----------------------------------------------------------------------*
* -->P_IT_FIELDCAT TEXT
*----------------------------------------------------------------------*
FORM FIELDCAT_INIT USING IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
DATA:LOC_REPID LIKE SY-REPID.
LOC_REPID = SY-REPID.
LS_FIELDCAT-FIELDNAME = 'LIFNR'.
LS_FIELDCAT-REPTEXT_DDIC = '供应商'.
APPEND LS_FIELDCAT TO IT_FIELDCAT.
CLEAR LS_FIELDCAT.
LS_FIELDCAT-FIELDNAME = 'NAME1'.
LS_FIELDCAT-REPTEXT_DDIC = '供应商名称'.
APPEND LS_FIELDCAT TO IT_FIELDCAT.
CLEAR LS_FIELDCAT.
ENDFORM. "FIELDCAT_INIT
" FIELDCAT_INIT
*&---------------------------------------------------------------------*
*& FORM LAYOUT_INIT
*&---------------------------------------------------------------------*
* TEXT LAYOUT属性设置,双击响应字段设置
*----------------------------------------------------------------------*
* -->P_IS_LAYOUT TEXT
*----------------------------------------------------------------------*
FORM LAYOUT_INIT USING IS_LAYOUT TYPE SLIS_LAYOUT_ALV.
IS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
ENDFORM. " LAYOUT_INIT
**&--------------------------------------------------------------------*
**& FORM USER_COMMAND
**&--------------------------------------------------------------------*
** TEXT
**---------------------------------------------------------------------*
** -->RF_UCOMM TEXT
** -->RS_SELFIELDTEXT
**---------------------------------------------------------------------*
FORM USER_COMMAND USING RF_UCOMM LIKE SY-UCOMM
RS_SELFIELD TYPE SLIS_SELFIELD. "#EC CALLED
CASE RF_UCOMM.
WHEN '&PRT'.
*****插入数据
DELETE FROM ZIF_MATLFN2SFWMS.
COMMIT WORK AND WAIT.
"去重防错
DELETE ADJACENT DUPLICATES FROM WTAB COMPARING LIFNR.
INSERT ZIF_MATLFN2SFWMS FROM TABLE WTAB." ACCEPTING DUPLICATE KEYS.
DESCRIBE TABLE WTAB LINES COUNT.
MESSAGE S001(00) WITH '成功上传' COUNT '条'.
ENDCASE.
ENDFORM. "USER_COMMAND
*&---------------------------------------------------------------------*
*& FORM GETEXCEL
*&---------------------------------------------------------------------*
* TEXT 获取上传EXCEL文件内相关数据
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM GETEXCEL .
G_FILE = P_FILE. "注意两个文件用于不同的函数,所以类型不同
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
FILENAME = G_FILE
I_BEGIN_COL = 1
I_BEGIN_ROW = 1
I_END_COL = 2
I_END_ROW = 10000
TABLES
INTERN = ITAB[].
DELETE ITAB WHERE ROW = 1. "删除字段名行
LOOP AT ITAB.
ON CHANGE OF ITAB-ROW.
IF SY-TABIX NE 1.
APPEND WTAB.
CLEAR WTAB.
ENDIF.
ENDON.
ASSIGN COMPONENT ITAB-COL OF STRUCTURE WTAB TO <FS>. "动态方法将值传到相应的内表
<FS> = ITAB-VALUE.
ENDLOOP.
APPEND WTAB. "这句不要忘记,要把最后一条APPEND到内表中
SORT WTAB BY LIFNR.
ENDFORM. " GETEXCEL
*&---------------------------------------------------------------------*
*& FORM GETDATA
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM GETDATA .
SELECT ZIF_MATLFN2SFWMS~LIFNR"工厂
ZIF_MATLFN2SFWMS~NAME1"版本
INTO CORRESPONDING FIELDS OF TABLE WTAB
FROM ZIF_MATLFN2SFWMS.
SORT WTAB BY LIFNR.
ENDFORM. " GETDATA