子曰花开2015

导航

BDC 批量数据交换小示例

1.使用SHDB创建BDC录屏记录:

2.开始记录到MM02开始进行录屏操作:更改,物料描述,毛重量,净重量,重量单位数据:

3.录屏结果如下:

 

4.SE38创建程序:

4.1 ZTEST_BDC01页面:

*&---------------------------------------------------------------------*
*& Report ZTEST_BDC01
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTEST_BDC01.
INCLUDE ZTEST_BDC01TOP.
INCLUDE ZTEST_BDC01FORM.

INITIALIZATION.

*SSCRFIELDS-FUNCTXT_01 = '@49@下载模板'.

*或者用以下方法加按钮图标
WRITE ICON_EXPORT AS ICON TO SSCRFIELDS-FUNCTXT_01+0(4).
SSCRFIELDS-FUNCTXT_01+4(*'下载模板'.

*响应屏幕PAI按钮事件
AT SELECTION-SCREEN.
  PERFORM SELECTION_PAI_SCREEN.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILES.
  PERFORM GET_FILENAME.

START-OF-SELECTION.

*取数
*1.从EXCEL上传
*2.手工插入数据

PERFORM GET_DATA.

END-OF-SELECTION.

*转换

*PERFORM APPEND_BDCDATA. "直接执行

PERFORM APPEND_BDCDATA_BATCH"创建session后SM35执行

4.2 ZTEST_BDC01TOP页面:

*&---------------------------------------------------------------------*
*&  包含                ZTEST_BDC01TOP
*&---------------------------------------------------------------------*

TYPESBEGIN OF GTY_MATERIAL,
        MATNR TYPE C LENGTH 18"物料编码
        MAKTX TYPE C LENGTH 40"物料描述
        BRGEW TYPE C LENGTH 20"毛重量
        NTGEW TYPE C LENGTH 20"净重量
        GEWEI TYPE C LENGTH 3,  "重量单位
       END OF GTY_MATERIAL.

DATA: GT_MATERIAL TYPE TABLE OF GTY_MATERIAL,
      GS_MATERIAL TYPE GTY_MATERIAL.

DATABEGIN OF GT_TEMPLATE OCCURS 0,
       TAB1 TYPE C LENGTH 10 VALUE '物料代码' ,
       TAB2 TYPE C LENGTH 10 VALUE '物料描述' ,
       TAB3 TYPE C LENGTH 10 VALUE '毛重量'   ,
       TAB4 TYPE C LENGTH 10 VALUE '净重量'   ,
       TAB5 TYPE C LENGTH 10 VALUE '重量单位' ,
      END OF GT_TEMPLATE.


DATA BDCDATA TYPE TABLE OF BDCDATA WITH HEADER LINE.

DATA MESSTAB LIKE BDCMSGCOLL OCCURS WITH HEADER LINE.

TABLES SSCRFIELDS.

PARAMETERS P_FILES TYPE RLGRAP-FILENAME.

*应用工具栏增加按钮
SELECTION-SCREEN FUNCTION KEY 1"数字1~5,对应界面中可以增加的5个按钮

 

4.3 ZTEST_BDC01FORM页面

*&---------------------------------------------------------------------*
*&  包含                ZTEST_BDC01FORM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  APPEND_BDCDATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM APPEND_BDCDATA .
DATA LV_MESSAGE TYPE C LENGTH 200.

LOOP AT GT_MATERIAL INTO GS_MATERIAL.
CLEAR: BDCDATA, BDCDATA[]"循环执行注意清空
       MESSTAB, MESSTAB[].
PERFORM APPEND_SCREEN USING 'SAPLMGMM' '0060' .

PERFORM APPEND_FIELDS USING 'BDC_CURSOR'  'RMMG1-MATNR'.
PERFORM APPEND_FIELDS USING 'BDC_OKCODE' '=ENTR'.
PERFORM APPEND_FIELDS USING 'RMMG1-MATNR' GS_MATERIAL-MATNR.


PERFORM APPEND_SCREEN USING 'SAPLMGMM' '0070' .

PERFORM APPEND_FIELDS USING 'BDC_CURSOR'  'MSICHTAUSW-DYTXT(01)'.
PERFORM APPEND_FIELDS USING 'BDC_OKCODE' '=ENTR'.
PERFORM APPEND_FIELDS USING 'MSICHTAUSW-KZSEL(01)' 'X'.



PERFORM APPEND_SCREEN USING 'SAPLMGMM' '4004' .

PERFORM APPEND_FIELDS USING 'BDC_OKCODE' '=BU'.
PERFORM APPEND_FIELDS USING 'BDC_CURSOR' 'MAKT-MAKTX'.  "一个界面CURSOR有一个就行了,SUBSCR都不要
PERFORM APPEND_FIELDS USING 'MAKT-MAKTX' GS_MATERIAL-MAKTX.
PERFORM APPEND_FIELDS USING 'MARA-BRGEW' GS_MATERIAL-BRGEW.
PERFORM APPEND_FIELDS USING 'MARA-NTGEW' GS_MATERIAL-NTGEW.
PERFORM APPEND_FIELDS USING 'MARA-GEWEI' GS_MATERIAL-GEWEI.

CALL TRANSACTION 'MM02' USING BDCDATA "使用内表调用
                        UPDATE 'S'    "更新模式: S.同步(执行到这,程序会停下来,等着执行完再往下);A 异步(执行到这就走了,分成两部分分开执行)
                        MODE 'N' "显示模式:A,前台 N, 后台 E,只显示错误信息
                        MESSAGES INTO MESSTAB .
  READ TABLE MESSTAB WITH KEY MSGID 'M3'
                              MSGNR '801'.
  IF SY-SUBRC 0.
    WRITE:/ '物料',GS_MATERIAL-MATNR,'修改成功'.
  ELSE.
    READ TABLE MESSTAB WITH KEY MSGID 'M3'
                              MSGNR '810'.
    IF SY-SUBRC 0.
      WRITE:/ '物料',GS_MATERIAL-MATNR,'修改成功'.
      ELSE.
    WRITE:/'物料',GS_MATERIAL-MATNR,'修改失败,失败原因:'.
    LOOP AT MESSTAB WHERE MSGTYP 'E'.
    CALL FUNCTION 'MESSAGE_TEXT_BUILD'
      EXPORTING
        MSGID                     = MESSTAB-MSGID
        MSGNR                     = MESSTAB-MSGNR
       MSGV1                      = MESSTAB-MSGV1
       MSGV2                      = MESSTAB-MSGV2
       MSGV3                      = MESSTAB-MSGV3
       MSGV4                      = MESSTAB-MSGV4
     IMPORTING
       MESSAGE_TEXT_OUTPUT       = LV_MESSAGE
              .

    WRITE:/ 'E', LV_MESSAGE.

    ENDLOOP.


    ENDIF.
    ENDIF.

CLEAR GS_MATERIAL.
ENDLOOP.
ENDFORM.


FORM APPEND_FIELDS USING VALUE(P_FIELD)
                         VALUE(P_VALUE).
  CLEAR BDCDATA"CLEAR后面是表头加工作区,只清空表头,如果是工作区,清空工作区.REFRESH是清空表头和工作区
  BDCDATA-FNAM = P_FIELD.
  BDCDATA-FVAL = P_VALUE.
  APPEND BDCDATA.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  APPEND_SCRREN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0011   text
*      -->P_0012   text
*----------------------------------------------------------------------*
FORM APPEND_SCREEN  USING    VALUE(P_PROGRAM)
                             VALUE(P_DYNPRO).

  CLEAR BDCDATA.
  BDCDATA-PROGRAM  = P_PROGRAM.
  BDCDATA-DYNPRO   = P_DYNPRO.
  BDCDATA-DYNBEGIN 'X'.
  APPEND BDCDATA.


ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_DATA .

*2.从文件中获取数据
*TXT文件的上传

  DATA LV_FILES TYPE STRING.

  CHECK P_FILES IS NOT INITIAL.
  TRANSLATE P_FILES TO UPPER CASE.

  FIND '.TXT' IN P_FILES.
  IF SY-SUBRC 0.

  LV_FILES = P_FILES.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      FILENAME                      = LV_FILES
*     FILETYPE                      = 'ASC'
     HAS_FIELD_SEPARATOR           'X'
*     HEADER_LENGTH                 = 0
*     READ_BY_LINE                  = 'X'
*     DAT_MODE                      = ' '
*     CODEPAGE                      = ' '
*     IGNORE_CERR                   = ABAP_TRUE
*     REPLACEMENT                   = '#'
*     CHECK_BOM                     = ' '
*     VIRUS_SCAN_PROFILE            =
*     NO_AUTH_CHECK                 = ' '
*   IMPORTING
*     FILELENGTH                    =
*     HEADER                        =
    TABLES
      DATA_TAB                      = GT_MATERIAL
*   CHANGING
*     ISSCANPERFORMED               = ' '
   EXCEPTIONS
     FILE_OPEN_ERROR               1
     FILE_READ_ERROR               2
     NO_BATCH                      3
     GUI_REFUSE_FILETRANSFER       4
     INVALID_TYPE                  5
     NO_AUTHORITY                  6
     UNKNOWN_ERROR                 7
     BAD_DATA_FORMAT               8
     HEADER_NOT_ALLOWED            9
     SEPARATOR_NOT_ALLOWED         10
     HEADER_TOO_LONG               11
     UNKNOWN_DP_ERROR              12
     ACCESS_DENIED                 13
     DP_OUT_OF_MEMORY              14
     DISK_FULL                     15
     DP_TIMEOUT                    16
     OTHERS                        17
            .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.
  ELSE.
  FIND '.XLS' IN P_FILES.
  IF SY-SUBRC 0.
  DATA LT_RAW TYPE TRUXS_T_TEXT_DATA.
*EXCEL文件上传
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
     I_FIELD_SEPERATOR          'X'      I_LINE_HEADER              
'X'       I_TAB_RAW_DATA             
= LT_RAW       I_FILENAME                 
= P_FILES     
TABLES       I_TAB_CONVERTED_DATA       
= GT_MATERIAL    
EXCEPTIONS      CONVERSION_FAILED          
1      
OTHERS                     2             
.   
IF SY-SUBRC <> 0.
* Implement suitable error handling here   
ENDIF.   
ENDIF.
ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  GET_FILENAME
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_FILENAME .
*1.获取本地文件路径   

CALL FUNCTION 'WS_FILENAME_GET'    
EXPORTING
*     DEF_FILENAME           = ' ' ”默认文件名称
*     DEF_PATH               = ' ' “默认文件路径      
MASK                   ',excel.xls,*.xlsx,text.txt,*.txt. '"每两个,一组      
MODE                   'O'"S 保存;O 打开      
TITLE                  '打开文件' "窗口的显示名称    
IMPORTING      FILENAME               
= P_FILES
*     RC                     =    
EXCEPTIONS      INV_WINSYS             
1      NO_BATCH               
2      SELECTION_CANCEL       
3      SELECTION_ERROR        
4      
OTHERS                 5             
.   
IF SY-SUBRC <> 0.
* Implement suitable error handling here   
ENDIF.


ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SELECTION_PAI_SCREEN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SELECTION_PAI_SCREEN .   
CASE SSCRFIELDS-UCOMM.     
WHEN 'FC01'.       
PERFORM DOWNLOAD_TEMPLATE."实行模板下载   
ENDCASE.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DOWNLOAD_TEMPLATE .   
REFRESH GT_TEMPLATE.   
APPEND  GT_TEMPLATE.   

DATA LV_FILE TYPE STRING.
*1.获取文件路径   
CALL FUNCTION 'WS_FILENAME_GET'    
EXPORTING
*     DEF_FILENAME           = ' '
*     DEF_PATH               = ' '      
MASK                   ',EXCEL,*.xls.'      
MODE                   'S '
*     TITLE                  = ' '    
IMPORTING      FILENAME               
= LV_FILE
*     RC                     =    
EXCEPTIONS      INV_WINSYS             
1      NO_BATCH               
2      SELECTION_CANCEL       
3      SELECTION_ERROR        
4      
OTHERS                 5             
.   
IF SY-SUBRC <> 0.
* Implement suitable error handling here   
ENDIF.   

CHECK LV_FILE IS NOT INITIAL.

*2.将内表下载到指定文件路径的文件中   
CALL FUNCTION 'GUI_DOWNLOAD'     
EXPORTING
*     BIN_FILESIZE                    =       FILENAME                        
= LV_FILE
*     FILETYPE                        = 'ASC'
*     APPEND                          = ' ' “如果打X,则标识多次下载同一路经,同一文件名,文件内容会进行叠加      WRITE_FIELD_SEPARATOR           
'X'
*     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                      = ' '
*     WRITE_LF_AFTER_LAST_LINE        = ABAP_TRUE
*     SHOW_TRANSFER_STATUS            = ABAP_TRUE
*     VIRUS_SCAN_PROFILE              = '/SCET/GUI_DOWNLOAD'
*   IMPORTING
*     FILELENGTH                      =     
TABLES       DATA_TAB                        
= GT_TEMPLATE
*     FIELDNAMES                      = GT_HEADER ”每列的描述的内表    
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.
* Implement suitable error handling here   
ENDIF.


ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  APPEND_BDCDATA_BATCH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM APPEND_BDCDATA_BATCH .   
DATA LV_MESSAGE TYPE C LENGTH 200.   
DATA GV_SESSION TYPE APQI-GROUPID VALUE 'ZMM02_HQ02'.
*开启session
CALL FUNCTION 'BDC_OPEN_GROUP'  
EXPORTING
*   CLIENT                    = SY-MANDT
*   DEST                      = FILLER8    
GROUP                     = GV_SESSION
*   HOLDDATE                  = FILLER8    KEEP                      
'X' "是否保存    USER                      
= SY-UNAME "当前用户名
*   RECORD                    = FILLER1
*   PROG                      = SY-CPROG
*   DCPFM                     = '%'
*   DATFM                     = '%'
* IMPORTING
*   QID                       =  
EXCEPTIONS    CLIENT_INVALID            
1    DESTINATION_INVALID       
2    GROUP_INVALID             
3    GROUP_IS_LOCKED           
4    HOLDDATE_INVALID          
5    INTERNAL_ERROR            
6    QUEUE_ERROR               
7    RUNNING                   
8    SYSTEM_LOCK_ERROR         
9    USER_INVALID              
10    
OTHERS                    11           
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here   
EXIT.
ENDIF.

LOOP AT GT_MATERIAL INTO GS_MATERIAL.
CLEAR: BDCDATA, BDCDATA[]"循环执行注意清空        MESSTAB
, MESSTAB[].
PERFORM APPEND_SCREEN USING 'SAPLMGMM' '0060' .

PERFORM APPEND_FIELDS USING 'BDC_CURSOR'  'RMMG1-MATNR'.
PERFORM APPEND_FIELDS USING 'BDC_OKCODE' '=ENTR'.
PERFORM APPEND_FIELDS USING 'RMMG1-MATNR' GS_MATERIAL-MATNR.


PERFORM APPEND_SCREEN USING 'SAPLMGMM' '0070' .

PERFORM APPEND_FIELDS USING 'BDC_CURSOR'  'MSICHTAUSW-DYTXT(01)'.
PERFORM APPEND_FIELDS USING 'BDC_OKCODE' '=ENTR'.
PERFORM APPEND_FIELDS USING 'MSICHTAUSW-KZSEL(01)' 'X'.



PERFORM APPEND_SCREEN USING 'SAPLMGMM' '4004' .

PERFORM APPEND_FIELDS USING 'BDC_OKCODE' '=BU'.
PERFORM APPEND_FIELDS USING 'BDC_CURSOR' 'MAKT-MAKTX'.  "一个界面CURSOR有一个就行了,SUBSCR都不要
PERFORM APPEND_FIELDS USING 'MAKT-MAKTX' GS_MATERIAL-MAKTX.
PERFORM APPEND_FIELDS USING 'MARA-BRGEW' GS_MATERIAL-BRGEW.
PERFORM APPEND_FIELDS USING 'MARA-NTGEW' GS_MATERIAL-NTGEW.
PERFORM APPEND_FIELDS USING 'MARA-GEWEI' GS_MATERIAL-GEWEI.

*向session中插入事务
CALL FUNCTION 'BDC_INSERT'  
EXPORTING    TCODE                  
'MM02'
*   POST_LOCAL             = NOVBLOCAL
*   PRINTING               = NOPRINT
*   SIMUBATCH              = ' '
*   CTUPARAMS              = ' '   
TABLES     DYNPROTAB              
= BDCDATA  
EXCEPTIONS    INTERNAL_ERROR         
1    NOT_OPEN               
2    QUEUE_ERROR            
3    TCODE_INVALID          
4    PRINTING_INVALID       
5    POSTING_INVALID        
6    
OTHERS                 7           
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

ENDLOOP.

*关闭session

CALL FUNCTION 'BDC_CLOSE_GROUP'  
EXCEPTIONS    NOT_OPEN          
1    QUEUE_ERROR       
2    
OTHERS            3           
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ELSE.   
WRITE:/ 'SESSION创建成功!'.
ENDIF.


ENDFORM.

posted on 2019-01-31 11:52  子曰花开2015  阅读(141)  评论(0编辑  收藏  举报