SAPbdc例⼦----详细
REPORT Z_30715_BDC.
*定义表内容
TYPES:
BEGIN OF GTY_MATERIAL ,
MATNR TYPE C LENGTH 18, "物料代码
MAKTX TYPE C LENGTH 80, "物料描述
BRGEW TYPE C LENGTH 20, "⽑总重
NTGEW TYPE C LENGTH 20, "净重量
GEWEI TYPE C LENGTH 3,  "单位
END OF GTY_MATERIAL.
*定义下载的模板
DATA: BEGIN 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: GT_MATERIAL TYPE TABLE OF GTY_MATERIAL,
GS_MATERIAL TYPE GTY_MATERIAL.
DATA BDCDATA TYPE TABLE OF BDCDATA WITH HEADER LINE.
DATA MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE."提⽰消息
DATA LV_MESSAGE TYPE C LENGTH 200.
DATA GV_SESSION TYPE APQI-GROUPID VALUE 'ZMM02_J02'."⽤session获取数据时定义的 SM35 名称
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
*应⽤⼯具栏添加下载模板按钮
SELECTION-SCREEN FUNCTION KEY 1. "1~5 数字 可以增加5个按钮
TABLES SSCRFIELDS.
"按钮
*选择屏幕  定义⽂件选择框
PARAMETERS P_FILES TYPE RLGRAP-FILENAME.
INITIALIZATION .
TYPE-POOLS ICON.
SSCRFIELDS-FUNCTXT_01 = '@48@下载模板'. "下载模板按钮名称
AT SELECTION-SCREEN. "处理与selection screen相关的⼀些输⼊输出。
PERFORM SELECT_SCREEN_API. "定义 下载模板按钮 功能
*为选择框添加选择界⾯
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILES.
PERFORM GET_FILENAME. " 获取路径名称
START-OF-SELECTION.
* 取数
* 1.从excel上传
* 2. ⼿⼯插⼊数据
*  CLEAR GS_MATERIAL.
*  GS_MATERIAL-MATNR = '4274' .
*  GS_MATERIAL-MAKTX = 'testbdc' .
*  APPEND GS_MATERIAL TO GT_MATERIAL.
*
*  CLEAR GS_MATERIAL.
*  GS_MATERIAL-MATNR = '4275' .
*  GS_MATERIAL-MAKTX = 'testbdcaaa' .
*  APPEND GS_MATERIAL TO GT_MATERIAL.
*
*  CLEAR GS_MATERIAL.
*  GS_MATERIAL-MATNR = '4276' .
*  GS_MATERIAL-MAKTX = 'testbdcbbbb' .
*  APPEND GS_MATERIAL TO GT_MATERIAL.
PERFORM GET_DATA. "获取数据
END-OF-SELECTION.
* 转换及调⽤
" 第1种⽅法
" PERFORM APPEND_BDCDATA.
" 2种⽅法,利⽤session
PERFORM APPEND_BDCDATA_BATCH.
*&---------------------------------------------------------------------*
*& Form APPEND_BDCDATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------* FORM APPEND_BDCDATA .
LOOP AT GT_MATERIAL INTO GS_MATERIAL .
CLEAR: BDCDATA, BDCDATA[], "循环执⾏时 清空原有数据          MESSTAB, MESSTAB[]. "清空提⽰消息
PERFORM APPEND_SCREEN USING 'SAPLMGMM' '0060'.
*1.  CLEAR BDCDATA.
*    BDCDATA-PROGRAM = ''.
*    BDCDATA-DYNPRO = ''.
*    BDCDATA-DYNBEGIN = ''.
*    BDCDATA-FNAM = 'BDC_CURSOR'.
*    BDCDATA-FVAL = 'RMMG1-MATNR'.
"2 代码格式优化
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 'MAKT-MAKTX' GS_MATERIAL-MAKTX .
PERFORM APPEND_FIELDS USING 'MARA-MEINS' 'EA'.
PERFORM APPEND_FIELDS USING 'MARA-MATKL' '23000000'.
PERFORM APPEND_FIELDS USING 'MARA-SPART' '00'.
PERFORM APPEND_FIELDS USING 'MARA-GEWEI' 'KG'.
PERFORM APPEND_FIELDS USING 'MARA-NTGEW' '200'.
CALL TRANSACTION 'MM01' USING BDCDATA "使⽤内表 bdcdata 调⽤事务代码 mm02                                UPDATE 'S' " 更新模式:S 同步 A 异步
MODE 'A'  "显⽰模式 A 前台 N 后台 E 只显⽰错误
MESSAGES INTO MESSTAB."消息提⽰
IF SY-SUBRC = 0.
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 = '801'. "数据未修改的时候  物料修改成功
IF SY-SUBRC = 0.
WRITE: / '物料修改失败'.
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.
ENDIF.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form APPEND_SCREEN
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM APPEND_FIELDS USING VALUE(P_PROGRAM) VALUE(P_DYNPRO)  .  CLEAR BDCDATA.
BDCDATA-FNAM = P_PROGRAM.
BDCDATA-FVAL = P_DYNPRO.
APPEND BDCDATA.
ENDFORM.
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
typec转dp
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM GET_DATA .
*  1.获取本地⽂件路径
*  2.从⽂件中获取数据
"⽂件类型转换  FILENAME是String类型
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.
* txt⽂件的上传
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 '.XLSX' IN P_FILES.
IF SY-SUBRC = 0.
DATA LA_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      = LA_RAW

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。