*&---------------------------------------------------------------------*
*& Report ZMM_03
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZMM_03 NO STANDARD PAGE HEADING
MESSAGE-ID ZMM.
TABLES:MARA.
DATA:BEGIN OF BDCDATA OCCURS 0.
INCLUDE STRUCTURE BDCDATA.
DATA:END OF BDCDATA.
DATA: MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
*DATA: GT_UP LIKE DRAW OCCURS 100 WITH HEADER LINE.
DATA:BEGIN OF GT_UP OCCURS 0.
INCLUDE STRUCTURE DRAW.
DATA:OLD_DOKNR LIKE DRAW-DOKNR.
DATA:END OF GT_UP.
data: gt_temp like DRAD OCCURS 100 WITH HEADER LINE.
DATA: INPUT TYPE KCDE_INTERN_STRUC OCCURS 100 WITH HEADER LINE.
DATA gt_fieldcat TYPE lvc_t_fcat .
DATA:T_C(2) TYPE C,T_TEMP(20) TYPE C,T_SIGN(1) TYPE C.
DATA: L_MSTRING(480).
DATA: LS_DOKNR LIKE DRAD-DOKNR.
PARAMETERS:MATNR LIKE MARA-MATNR OBLIGATORY.
PARAMETERS:P_MODE TYPE C DEFAULT 'N' NO-DISPLAY.
PARAMETERS: P_LFILE LIKE RLGRAP-FILENAME OBLIGATORY.
PARAMETERS R_INSERT RADIOBUTTON GROUP CTU DEFAULT 'X'.
PARAMETERS R_UPDATE RADIOBUTTON GROUP CTU.
PARAMETERS R_DELETE RADIOBUTTON GROUP CTU.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_lfile.
PERFORM GET_FILENAME USING P_LFILE
CHANGING P_LFILE.
START-OF-SELECTION.
T_SIGN = '3'.
if p_lfile <> ''.
PERFORM get_file.
ENDIF.
IF GT_UP[] IS INITIAL.
ELSE.
****取记录
PERFORM get_data.
PERform PROCESS_CC02.
PERform PROCESS_CC03.
PERform PROCESS_CC04.
PERFORM PROCESS_CC06.
IF R_INSERT = 'X'. "新增
T_C = LINES( GT_TEMP ).
LOOP AT GT_UP.
T_C = T_C + 1.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = T_C
IMPORTING
OUTPUT = T_C.
perform bdc_dynpro using 'SAPLMGMM' '4300'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
CONCATENATE 'DRAW-DOKNR(' T_C ')' INTO T_TEMP.
perform bdc_field using 'BDC_CURSOR'
T_TEMP.
CONCATENATE 'DRAW-DOKAR(' T_C ')' INTO T_TEMP.
perform bdc_field using T_TEMP
GT_UP-DOKAR.
CONCATENATE 'DRAW-DOKNR(' T_C ')' INTO T_TEMP.
perform bdc_field using T_TEMP
GT_UP-DOKNR.
CONCATENATE 'DRAW-DOKTL(' T_C ')' INTO T_TEMP.
perform bdc_field using T_TEMP
GT_UP-DOKTL.
CONCATENATE 'DRAW-DOKVR(' T_C ')' INTO T_TEMP.
perform bdc_field using T_TEMP
GT_UP-DOKVR.
perform bdc_field using 'GF_ALLE'
'X'.
* perform bdc_transaction using 'MM02'.
ENDLOOP.
perform bdc_dynpro using 'SAPLMGMM' '4300'.
perform bdc_field using 'BDC_OKCODE'
'=BU'.
CONCATENATE 'DRAW-DOKVR(' T_C ')' INTO T_TEMP.
perform bdc_field using 'BDC_CURSOR'
T_TEMP.
perform bdc_field using 'GF_ALLE'
'X'.
endif.
IF R_DELETE = 'X'. "删除
LOOP AT GT_UP.
LS_DOKNR = GT_UP-DOKNR.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = LS_DOKNR
IMPORTING
OUTPUT = LS_DOKNR.
LOOP AT GT_TEMP WHERE DOKNR = LS_DOKNR.
T_C = sy-tabix.
ENDLOOP.
ENDLOOP.
* T_C = T_C + 1.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = T_C
IMPORTING
OUTPUT = T_C.
perform bdc_dynpro using 'SAPLMGMM' '4300'.
perform bdc_field using 'BDC_OKCODE'
'=CV140_DELZ'.
CONCATENATE 'DRAW-DOKAR(' T_C ')' INTO T_TEMP.
perform bdc_field using 'BDC_CURSOR'
T_TEMP.
perform bdc_field using 'GF_ALLE'
'X'.
LOOP AT GT_UP.
LS_DOKNR = GT_UP-DOKNR.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = LS_DOKNR
IMPORTING
OUTPUT = LS_DOKNR.
LOOP AT GT_TEMP WHERE DOKNR = LS_DOKNR.
T_C = sy-tabix.
ENDLOOP.
* T_C = T_C + 1.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = T_C
IMPORTING
OUTPUT = T_C.
CONCATENATE 'MARK(' T_C ')' INTO T_TEMP.
perform bdc_field using T_TEMP
'X'.
* perform bdc_dynpro using 'SAPLSPO1' '0500'.
* perform bdc_field using 'BDC_OKCODE'
* '=OPT1'.
ENDLOOP.
perform bdc_dynpro using 'SAPLMGMM' '4300'.
perform bdc_field using 'BDC_OKCODE'
'=BU'.
CONCATENATE 'DRAW-DOKVR(' T_C ')' INTO T_TEMP.
perform bdc_field using 'BDC_CURSOR'
T_TEMP.
perform bdc_field using 'GF_ALLE'
'X'.
ENDIF.
IF R_UPDATE = 'X'. "修改
LOOP AT GT_UP.
LS_DOKNR = GT_UP-OLD_DOKNR.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = LS_DOKNR
IMPORTING
OUTPUT = LS_DOKNR.
LOOP AT GT_TEMP WHERE DOKNR = LS_DOKNR.
T_C = sy-tabix.
ENDLOOP.
ENDLOOP.
* T_C = T_C + 1.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = T_C
IMPORTING
OUTPUT = T_C.
perform bdc_dynpro using 'SAPLMGMM' '4300'.
perform bdc_field using 'BDC_OKCODE'
'=BU'.
CONCATENATE 'DRAW-DOKNR(' T_C ')' INTO T_TEMP.
perform bdc_field using 'BDC_CURSOR'
T_TEMP.
perform bdc_field using 'GF_ALLE'
'X'.
LOOP AT GT_UP.
LS_DOKNR = GT_UP-OLD_DOKNR.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = LS_DOKNR
IMPORTING
OUTPUT = LS_DOKNR.
LOOP AT GT_TEMP WHERE DOKNR = LS_DOKNR.
T_C = sy-tabix.
ENDLOOP.
* T_C = T_C + 1.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = T_C
IMPORTING
OUTPUT = T_C.
CONCATENATE 'DRAW-DOKNR(' T_C ')' INTO T_TEMP.
perform bdc_field using T_TEMP
GT_UP-DOKNR.
* perform bdc_dynpro using 'SAPLSPO1' '0500'.
* perform bdc_field using 'BDC_OKCODE'
* '=OPT1'.
ENDLOOP.
LOOP AT GT_UP.
perform bdc_dynpro using 'SAPLSPO1' '0500'.
perform bdc_field using 'BDC_OKCODE'
'=OPT1'.
ENDLOOP.
ENDIF.
* PERFORM PROCESS_CC07.
REFRESH MESSTAB.
CALL TRANSACTION 'MM02' USING BDCDATA MODE P_MODE UPDATE 'S' MESSAGES INTO MESSTAB.
LOOP AT MESSTAB.
MESSAGE ID MESSTAB-MSGID
TYPE MESSTAB-MSGTYP
NUMBER MESSTAB-MSGNR
INTO L_MSTRING
WITH MESSTAB-MSGV1
MESSTAB-MSGV2
MESSTAB-MSGV3
MESSTAB-MSGV4.
IF MESSTAB-MSGTYP = 'E'.
CONCATENATE MATNR ' ' L_MSTRING INTO L_MSTRING.
ENDIF.
WRITE: / MESSTAB-MSGTYP, L_MSTRING(250).
ENDLOOP.
* CALL TRANSACTION 'MM02' USING BDCDATA MODE P_MODE UPDATE 'S'.
endif.
END-OF-SELECTION.
FORM GET_FILENAME USING P_PATH LIKE RLGRAP-FILENAME
CHANGING P_FILE LIKE RLGRAP-FILENAME.
DATA:L_WRK_FILENAME LIKE RLGRAP-FILENAME,
L_WRK_MASK LIKE LGRWO-EXP_FILE.
* mask
L_WRK_MASK = ',*.xls,*.xls.'.
* get file name
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
DEF_PATH = P_FILE
MASK = L_WRK_MASK
MODE = 'O'
IMPORTING
FILENAME = L_WRK_FILENAME
EXCEPTIONS
INV_WINSYS = 01
NO_BATCH = 02
SELECTION_CANCEL = 03
SELECTION_ERROR = 04
OTHERS = 05.
IF SY-SUBRC = 0.
P_FILE = L_WRK_FILENAME.
ELSE.
ENDIF.
ENDFORM. " get_filename
FORM get_file.
CALL FUNCTION 'KCD_EXCEL_OLE_TO_INT_CONVERT'
EXPORTING
FILENAME = P_LFILE
I_BEGIN_COL = 1
I_BEGIN_ROW = 1
I_END_COL = 256
I_END_ROW = 65536
TABLES
INTERN = INPUT
EXCEPTIONS
INCONSISTENT_PARAMETERS = 1
UPLOAD_OLE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE E000(zqm) WITH '文件路径有误!'.
ENDIF.
LOOP AT INPUT.
IF input-row = '0001'.
CONTINUE. "第一行是字段名,直接跳过
ELSE.
CASE INPUT-COL.
WHEN '0001'.
GT_UP-DOKAR = INPUT-VALUE.
WHEN '0002'.
GT_UP-OLD_DOKNR = INPUT-VALUE.
WHEN '0003'.
GT_UP-DOKNR = INPUT-VALUE.
WHEN '0004'.
GT_UP-DOKTL = INPUT-VALUE.
WHEN '0005'.
GT_UP-DOKVR = INPUT-VALUE.
ENDCASE.
AT END OF ROW.
APPEND GT_UP.
CLEAR GT_UP.
ENDAT.
ENDIF.
ENDLOOP.
*** 检测
loop at gt_up.
IF R_INSERT = 'X' OR R_DELETE = 'X'.
IF GT_UP-OLD_DOKNR = GT_UP-DOKNR.
ELSE.
WRITE: / '新增,删除,两列文档编号必须一致', SY-SUBRC.
STOP.
ENDIF.
ELSE.
IF GT_UP-OLD_DOKNR = GT_UP-DOKNR.
WRITE: / '修改,两列文档编号必须不一致', SY-SUBRC.
STOP.
ELSE.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM.
form PROCESS_CC02.
perform bdc_dynpro using 'SAPLMGMM' '0060'.
perform bdc_field using 'BDC_CURSOR'
'RMMG1-MATNR'.
perform bdc_field using 'BDC_OKCODE'
'ENTR'.
perform bdc_field using 'RMMG1-MATNR'
matnr.
ENDform. "process_cc02
form PROCESS_CC03.
perform bdc_dynpro using 'SAPLMGMM' '0070'.
perform bdc_field using 'BDC_CURSOR'
'MSICHTAUSW-DYTXT(01)'.
perform bdc_field using 'BDC_OKCODE'
'=ENTR'.
perform bdc_field using 'MSICHTAUSW-KZSEL(01)'
'X'.
ENDform. "process_cc03
form PROCESS_CC04.
perform bdc_dynpro using 'SAPLMGMM' '4004'.
perform bdc_field using 'BDC_OKCODE'
'=ZU01'.
perform bdc_field using 'BDC_CURSOR'
'MAKT-MAKTX'.
* perform bdc_field using 'MAKT-MAKTX'
* 'X'.
* perform bdc_field using 'MARA-MEINS'
* 'X'.
* perform bdc_field using 'MARA-MATKL'
* 'X'.
* perform bdc_field using 'MARA-EXTWG'
* 'X'.
* perform bdc_field using 'MARA-SPART'
* 'X'.
* perform bdc_field using 'MARA-MTPOS_MARA'
* 'X'.
* perform bdc_field using 'MARA-BRGEW'
* 'X'.
* perform bdc_field using 'MARA-GEWEI'
* 'X'.
* perform bdc_field using 'MARA-NTGEW'
* 'X'.
* perform bdc_field using 'MARA-VOLUM'
* 'X'.
* perform bdc_field using 'MARA-VOLEH'
* 'X'.
ENDform. "process_cc04
form PROCESS_CC05.
perform bdc_dynpro using 'SAPLMGMM' '4300'.
perform bdc_field using 'BDC_OKCODE'
'=BU'.
perform bdc_field using 'BDC_CURSOR'
'DRAW-DOKNR(01)'.
perform bdc_field using 'DRAW-DOKAR(01)'
GT_UP-DOKAR.
perform bdc_field using 'DRAW-DOKNR(01)'
GT_UP-DOKNR.
perform bdc_field using 'DRAW-DOKTL(01)'
GT_UP-DOKTL.
perform bdc_field using 'DRAW-DOKVR(01)'
GT_UP-DOKVR.
perform bdc_field using 'GF_ALLE'
'X'.
ENDform. "process_cc05
FORM PROCESS_CC06.
perform bdc_dynpro using 'SAPLMGMM' '4300'.
perform bdc_field using 'BDC_OKCODE'
'=ZU04'.
perform bdc_field using 'BDC_CURSOR'
'SKTEXT-SPRAS(01)'.
ENDFORM.
FORM PROCESS_CC07.
perform bdc_dynpro using 'SAPLMGMM' '4300'.
perform bdc_field using 'BDC_OKCODE'
'=BU'.
perform bdc_field using 'BDC_CURSOR'
'DRAW-DOKNR(02)'.
perform bdc_field using 'GF_ALLE'
'X'.
ENDFORM.
form BDC_DYNPRO USING PROGRAM DYNPRO.
CLEAR BDCDATA.
BDCDATA-PROGRAM = PROGRAM.
BDCDATA-DYNPRO = DYNPRO.
BDCDATA-DYNBEGIN = 'X'.
APPEND BDCDATA.
ENDform.
form BDC_FIELD USING FNAM FVAL.
CLEAR BDCDATA.
BDCDATA-FNAM = FNAM.
BDCDATA-FVAL = FVAL.
APPEND BDCDATA.
ENDform.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_DATA .
select * into CORRESPONDING FIELDS OF TABLE gt_temp
from DRAD
where DOKOB = 'MARA' AND
OBJKY = matnr.
SORT GT_TEMP BY DOKNR.
ENDFORM. " GET_DATA
对导入的EXCEL数据批量处理物料文档信息,包括新增,修改,删除