项目中,静态数据BOM批导是项目上线时,必须的步骤,下面代码是在CX项目中利用函数CS_BI_BOM_CREATE_BATCH_INPUT1做的BOM批导程序,程序中用EXCEL表格作为导入模板,并把批到结果以txt格式保存下来,分享一下,希望对需要的兄弟有帮助。
*----------------------------------------------------------------------*
* Program Name : BOM批导入
* Purpose :
* Project Name :
* Created by : liuxy
* Create on :
* Functional Consultant :
* Description :
*此程序主要功能为BOM批导入
*客户使用固定的EXCEL格式的文件格式,通过批导程序,导入相关表中。导入结果可以查询
*----------------------------------------------------------------------*
* Modification Log
*Date Programmer Corr. # Description
*
*----------------------------------------------------------------------*
REPORT ZPP_BOM_BATCH_CREATE
MESSAGE-ID 00
LINE-COUNT 50
LINE-SIZE 132
NO STANDARD PAGE HEADING.
*----------------------------------------------------------------------*
* Table Work Areas
*----------------------------------------------------------------------*
TYPE-POOLS: SLIS.
TYPE-POOLS: KCDE.
*&---------------------------------------------------------------------*
*& TABLES
*&---------------------------------------------------------------------*
TABLES: MAST,
STKO,
STPO.
*----------------------------------------------------------------------*
* Global Internal Tables Declaration
*----------------------------------------------------------------------*
DATA: IT_FILE TYPE FILETABLE.
TYPES:BEGIN OF TY_IT_UPLOAD,
WERKS LIKE MAST-WERKS, "工厂
MATNR LIKE MAST-MATNR, "物料
STLAL LIKE STKO-STLAL, "可选BOM
BMENG(13), "数量
POSNR LIKE STPO-POSNR, "组件项目号
IDNRK LIKE STPO-IDNRK, "SAP组件物料号
MENGE(13), "组件数量
MEINS LIKE STPO-MEINS, "组件单位
AUSCH(5), "部件废品百分数
KZKUP LIKE STPO-KZKUP, "是否为联产品(指示符:联合产品)
LGORT LIKE STPO-LGORT, "生产仓储地点
VERTI LIKE STPO-VERTI, "分配代码
ALPGR LIKE STPO-ALPGR, "替代项目组
ALPRF LIKE STPO-ALPRF, "优先级
ALPST LIKE STPO-ALPST, "策略
EWAHR(3), "使用可能性
END OF TY_IT_UPLOAD.
**定义一个带有Header line的内存表IT
DATA:IT_UPLOAD TYPE TABLE OF TY_IT_UPLOAD WITH HEADER LINE.
DATA:WA_IT_UPLOAD TYPE TY_IT_UPLOAD.
*----------------------------------------------------------------------*
* Global Variants Declaration
*----------------------------------------------------------------------*
DATA: G_REPID TYPE SY-REPID.
DATA: IT_FIELD TYPE SLIS_T_FIELDCAT_ALV.
*定义读入EXCEL的内表
DATA GIT_EXCEL TYPE KCDE_INTERN_STRUC OCCURS 0 WITH HEADER LINE.
TYPES:BEGIN OF T_OUT.
INCLUDE STRUCTURE IT_UPLOAD.
TYPES FLAG(1).
TYPES MESSAGE(200).
TYPES:END OF T_OUT.
DATA:IT_OUT TYPE TABLE OF T_OUT WITH HEADER LINE.
DATA: L_NAME LIKE WWWDATATAB.
DATA: L_FULLPATH TYPE STRING.
*----------------------------------------------------------------------*
* Selection Screen
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK FRAME2 WITH FRAME TITLE T2.
PARAMETER: P_RB_1 RADIOBUTTON GROUP G1 USER-COMMAND F1 DEFAULT ‘X‘,
P_RB_2 RADIOBUTTON GROUP G1.
SELECTION-SCREEN END OF BLOCK FRAME2.
*****选择条件
SELECTION-SCREEN BEGIN OF BLOCK BL01 WITH FRAME TITLE TEXT-001.
PARAMETERS: P_FILE LIKE RLGRAP-FILENAME MODIF ID M1.
SELECTION-SCREEN END OF BLOCK BL01.
SELECTION-SCREEN BEGIN OF BLOCK BL02 WITH FRAME TITLE TEXT-001.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (79) text-004.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (79) text-005.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK BL02.
*&---------------------------------------------------------------------*
*& Event AT INITIALIZATION
*&---------------------------------------------------------------------*
INITIALIZATION.
*&---------------------------------------------------------------------*
*& Event AT SELECTION-SCREEN
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF ( P_RB_1 = ‘X‘ OR P_RB_2 = ‘X‘ ) AND ( SCREEN-GROUP1 = ‘M2‘ OR SCREEN-GROUP1 = ‘M3‘ OR SCREEN-GROUP1 = ‘M4‘).
SCREEN-ACTIVE = ‘0‘.
ENDIF.
IF ( P_RB_2 = ‘X‘ ) AND SCREEN-GROUP1 = ‘M1‘.
SCREEN-ACTIVE = ‘0‘.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
*********************************************************************
* AT SELECTION-SCREEN
*********************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
PERFORM FRM_OPEN_FILE.
*----------------------------------------------------------------------*
* Event Occurs After The Selection Screen Has Been Processed
*----------------------------------------------------------------------*
START-OF-SELECTION.
IF P_RB_1 = ‘X‘.
PERFORM FRM_AUTH_CHECK.
PERFORM FRM_CHECK.
* PERFORM FRM_ALV.
PERFORM FRM_DOWNLOAD.
ENDIF.
IF P_RB_2 = ‘X‘.
L_NAME-RELID = ‘MI‘.
L_NAME-OBJID = ‘Z_BOM_TEMPLATE‘.
PERFORM GET_TEMPLATE CHANGING L_NAME L_FULLPATH.
ENDIF.
"ALV
*----------------------------------------------------------------------*
* The Last Of The Events Called By The Runtime Environment To Occur
*----------------------------------------------------------------------*
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form frm_open_file
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM FRM_OPEN_FILE.
CALL FUNCTION ‘WS_FILENAME_GET‘
EXPORTING
mask = ‘,Excel Files,*.xls,All Files,*.*.‘(101)
title = ‘选择文件‘(100)
IMPORTING
filename = p_file
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
ENDFORM. "frm_open_file
*&---------------------------------------------------------------------*
*& Form frm_auth_check
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM FRM_AUTH_CHECK.
* DATA: LV_BUKRS TYPE STRING.
* DATA: VKORG TYPE TVKO-VKORG.
*
* SELECT SINGLE VKORG
* FROM TVKO
* INTO VKORG
* WHERE BUKRS = P_BUKRS.
*
* AUTHORITY-CHECK OBJECT ‘V_VBAK_VKO‘
* ID ‘VKORG‘ FIELD VKORG.
* IF SY-SUBRC NE 0.
* CONCATENATE ‘没有权限对公司‘ VKORG ‘进行操作!‘ INTO LV_BUKRS.
* MESSAGE LV_BUKRS TYPE ‘S‘ DISPLAY LIKE ‘E‘ .
* LEAVE LIST-PROCESSING.
* ENDIF.
ENDFORM. "frm_auth_check
*&---------------------------------------------------------------------*
*& Form frm_check
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM FRM_CHECK.
PERFORM FRM_TIDY.
PERFORM FRM_SAVE.
ENDFORM. "frm_check
*&---------------------------------------------------------------------*
*& Form frm_tidy
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM FRM_TIDY.
*从已知文件名读入内表
CALL FUNCTION ‘KCD_EXCEL_OLE_TO_INT_CONVERT‘
EXPORTING
FILENAME = P_FILE
I_BEGIN_COL = 1
I_BEGIN_ROW = 1
I_END_COL = 20
I_END_ROW = 65535
TABLES
INTERN = GIT_EXCEL[]
EXCEPTIONS
INCONSISTENT_PARAMETERS = 1
UPLOAD_OLE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE ‘打开文件错误,请检查文件,确保关闭文件!‘ TYPE ‘E‘.
STOP.
ENDIF.
REFRESH IT_UPLOAD.
CLEAR IT_UPLOAD.
LOOP AT GIT_EXCEL.
CASE GIT_EXCEL-COL.
WHEN ‘001‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-WERKS.
WHEN ‘002‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-MATNR.
WHEN ‘003‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-BMENG.
WHEN ‘004‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-STLAL.
WHEN ‘005‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-POSNR.
WHEN ‘006‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-IDNRK.
WHEN ‘007‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-MENGE.
WHEN ‘008‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-MEINS.
WHEN ‘009‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-AUSCH.
WHEN ‘010‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-KZKUP.
WHEN ‘011‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-LGORT.
WHEN ‘012‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-VERTI.
WHEN ‘013‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-ALPGR.
WHEN ‘014‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-ALPRF.
WHEN ‘015‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-ALPST.
WHEN ‘016‘.
WRITE GIT_EXCEL-VALUE TO IT_UPLOAD-EWAHR.
ENDCASE.
AT END OF ROW.
APPEND IT_UPLOAD.
CLEAR IT_UPLOAD.
ENDAT.
ENDLOOP.
* 删除表头
DELETE IT_UPLOAD INDEX 1.
ENDFORM. "frm_tidy
*&---------------------------------------------------------------------*
*& Form frm_save
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM FRM_SAVE.
DATA: L_BOM_HEADER LIKE BICSK,
L_GROUP_DATA LIKE BGR00,
L_MSGID LIKE T100-ARBGB,
L_MSGNO LIKE T100-MSGNR,
P_MSGNO LIKE SY-MSGNO,
L_MSGTY LIKE SY-MSGTY,
L_MSGV1 LIKE SY-MSGV1,
L_MSGV2 LIKE SY-MSGV2,
L_MSGV3 LIKE SY-MSGV3,
L_MSGV4 LIKE SY-MSGV4,
L_POSNR LIKE BICSP-POSNR.
DATA: LT_BOM_ITEM LIKE BICSP OCCURS 0 WITH HEADER LINE,
LT_SUB_ITEM LIKE BICSU OCCURS 0 WITH HEADER LINE.
DATA: GS_MSG LIKE MESSAGE,
L_ERROR(1).
SORT IT_UPLOAD BY WERKS MATNR STLAL POSNR.
CLEAR IT_OUT.
REFRESH IT_OUT.
LOOP AT IT_UPLOAD.
CLEAR WA_IT_UPLOAD.
MOVE-CORRESPONDING IT_UPLOAD TO WA_IT_UPLOAD.
AT NEW STLAL.
* 创建之前先删除
CALL FUNCTION ‘CSAP_MAT_BOM_DELETE‘
* DESTINATION V_RFC_DES
EXPORTING
MATERIAL = WA_IT_UPLOAD-MATNR
PLANT = WA_IT_UPLOAD-WERKS
ALTERNATIVE = WA_IT_UPLOAD-STLAL
BOM_USAGE = ‘1‘
FL_NO_CHANGE_DOC = ‘X‘
FL_COMMIT_AND_WAIT = ‘X‘
EXCEPTIONS
ERROR = 1
OTHERS = 2.
CLEAR L_BOM_HEADER.
CLEAR LT_BOM_ITEM.
REFRESH LT_BOM_ITEM.
L_BOM_HEADER-TCODE = ‘CS01‘.
L_BOM_HEADER-WERKS = WA_IT_UPLOAD-WERKS.
L_BOM_HEADER-MATNR = WA_IT_UPLOAD-MATNR.
L_BOM_HEADER-BMENG = WA_IT_UPLOAD-BMENG.
L_BOM_HEADER-STLAL = WA_IT_UPLOAD-STLAL.
L_BOM_HEADER-STLAN = ‘1‘.
L_BOM_HEADER-DATUV = SY-DATUM.
L_BOM_HEADER-LOSBS = ‘99999999‘.
L_BOM_HEADER-STLST = ‘01‘.
ENDAT.
LT_BOM_ITEM-POSTP = ‘L‘.
LT_BOM_ITEM-SANKA = ‘X‘.
LT_BOM_ITEM-XLINE = ‘1‘.
LT_BOM_ITEM-POSNR = WA_IT_UPLOAD-POSNR.
LT_BOM_ITEM-IDNRK = WA_IT_UPLOAD-IDNRK.
LT_BOM_ITEM-MENGE = WA_IT_UPLOAD-MENGE.
LT_BOM_ITEM-MEINS = WA_IT_UPLOAD-MEINS.
LT_BOM_ITEM-AUSCH = WA_IT_UPLOAD-AUSCH.
LT_BOM_ITEM-KZKUP = WA_IT_UPLOAD-KZKUP.
LT_BOM_ITEM-LGORT = WA_IT_UPLOAD-LGORT.
LT_BOM_ITEM-VERTI = WA_IT_UPLOAD-VERTI.
LT_BOM_ITEM-ALPGR = WA_IT_UPLOAD-ALPGR.
LT_BOM_ITEM-ALPRF = WA_IT_UPLOAD-ALPRF.
LT_BOM_ITEM-ALPST = WA_IT_UPLOAD-ALPST.
LT_BOM_ITEM-EWAHR = WA_IT_UPLOAD-EWAHR.
APPEND LT_BOM_ITEM.
CLEAR LT_BOM_ITEM.
AT END OF STLAL.
CALL FUNCTION ‘CS_BI_BOM_CREATE_BATCH_INPUT1‘
EXPORTING
BOM_HEADER = L_BOM_HEADER
* CLOSE_GROUP = ‘X‘
COMMIT_WORK = ‘X‘
GROUP_DATA = L_GROUP_DATA
* NEW_GROUP = ‘X‘
TCODE_MODE = ‘E‘
TCODE_UPDATE = ‘S‘
IMPORTING
MSGID = L_MSGID
MSGNO = L_MSGNO
MSGTY = L_MSGTY
MSGV1 = L_MSGV1
MSGV2 = L_MSGV2
MSGV3 = L_MSGV3
MSGV4 = L_MSGV4
TABLES
BOM_ITEM = LT_BOM_ITEM
BOM_SUB_ITEM = LT_SUB_ITEM.
L_MSGID = L_MSGID.
P_MSGNO = L_MSGNO.
* **get message string
CALL FUNCTION ‘WRITE_MESSAGE‘
EXPORTING
MSGID = L_MSGID
MSGNO = P_MSGNO
MSGTY = L_MSGTY
MSGV1 = L_MSGV1
MSGV2 = L_MSGV2
MSGV3 = L_MSGV3
MSGV4 = L_MSGV4
MSGV5 = SPACE
IMPORTING
ERROR = L_ERROR
MESSG = GS_MSG
EXCEPTIONS
OTHERS = 1.
IT_OUT-WERKS = WA_IT_UPLOAD-WERKS.
IT_OUT-MATNR = WA_IT_UPLOAD-MATNR.
IT_OUT-BMENG = WA_IT_UPLOAD-BMENG.
IT_OUT-STLAL = WA_IT_UPLOAD-STLAL.
IF GS_MSG-MSGTY = ‘E‘ OR NOT ( L_ERROR IS INITIAL ).
IT_OUT-FLAG = ‘E‘.
IT_OUT-MESSAGE = GS_MSG-MSGTX.
ELSE.
IT_OUT-FLAG = ‘S‘.
IT_OUT-MESSAGE = ‘创建成功!‘.
ENDIF.
APPEND IT_OUT.
CLEAR IT_OUT.
ENDAT.
ENDLOOP.
ENDFORM. "frm_save
*&---------------------------------------------------------------------*
*& Form GET_TEMPLATE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_L_NAME text
* <--P_L_FULLPATH text
*----------------------------------------------------------------------*
FORM GET_TEMPLATE CHANGING NAME FULLPATH.
DATA: TITLE TYPE STRING.
DATA: MIME LIKE W3MIME OCCURS 10.
DATA: FILENAME TYPE STRING.
DATA: PATH TYPE STRING.
TITLE = ‘BOM批量创建模版‘.
CALL FUNCTION ‘WWWDATA_IMPORT‘
EXPORTING
KEY = NAME
TABLES
MIME = MIME
EXCEPTIONS
WRONG_OBJECT_TYPE = 1
IMPORT_ERROR = 2
OTHERS = 3.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
EXPORTING
WINDOW_TITLE = TITLE
DEFAULT_EXTENSION = ‘xls‘
DEFAULT_FILE_NAME = TITLE
FILE_FILTER = ‘(电子表格EXCEL)‘
CHANGING
FILENAME = FILENAME
PATH = PATH
FULLPATH = FULLPATH
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
STOP.
ENDIF.
CALL FUNCTION ‘GUI_DOWNLOAD‘
EXPORTING
FILENAME = FULLPATH
FILETYPE = ‘BIN‘
TABLES
DATA_TAB = MIME.
ENDFORM. " GET_TEMPLATE
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_DOWNLOAD .
CALL FUNCTION ‘GUI_DOWNLOAD‘
EXPORTING
* BIN_FILESIZE =
FILENAME = ‘C:/BOM批量创建程序结果表.TXT‘
FILETYPE = ‘ASC‘
APPEND = ‘ ‘
* WRITE_FIELD_SEPARATOR = ‘ ‘
* HEADER = ‘00‘
* TRUNC_TRAILING_BLANKS = ‘ ‘
* WRITE_LF = ‘X‘
* COL_SELECT = ‘ ‘
* COL_SELECT_MASK = ‘ ‘
* DAT_MODE = ‘ ‘
* CONFIRM_OVERWRITE = ‘ ‘
* NO_AUTH_CHECK = ‘ ‘
* CODEPAGE = ‘ ‘
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = ‘#‘
* WRITE_BOM = ‘ ‘
* TRUNC_TRAILING_BLANKS_EOL = ‘X‘
* WK1_N_FORMAT = ‘ ‘
* WK1_N_SIZE = ‘ ‘
* WK1_T_FORMAT = ‘ ‘
* WK1_T_SIZE = ‘ ‘
* IMPORTING
* FILELENGTH =
TABLES
DATA_TAB = IT_OUT
* FIELDNAMES =
* 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
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
WRITE:‘BOM批量创建程序结果表保存到C盘根目录下,请查看!‘.
ENDFORM. " FRM_DOWNLOAD
*Text elements
*----------------------------------------------------------
* 004 批导注意事项:1、导入前模版中黄色列必须删除。
* 005 2、导入前只保留一行标题。
*Selection texts
*----------------------------------------------------------
* P_FILE 上传文件路径
* P_RB_1 BOM批量创建
* P_RB_2 下载BOM文件模板