【转】
REPORT zbdc_test_by_shir.

* 定义个BDC格式的内表
**************************************************
DATA : BEGIN OF i_bdcdata OCCURS 0,
*BDC的程序名字,比如事物码MM01的程序名字为SAPLMGMM(sap标准程序)
         program LIKE bdcdata-program,
*BDC Dynpro 号码
         dynpro LIKE bdcdata-dynpro,
*BDC BDC Dynpro 開始
         dynbegin LIKE bdcdata-dynbegin,
*BDC 项目名字
         fnam LIKE bdcdata-fnam,
*BDC 项目名字的附加值
         fval LIKE bdcdata-fval,
       END OF i_bdcdata.
*BDC
DATA: t_bdcmsg LIKE bdcmsgcoll  OCCURS 0 WITH HEADER LINE.
*****此方法可以用excel,txt等文件导入
DATA: BEGIN OF itab OCCURS 0,
        maktx  LIKE makt-maktx,
        meins  LIKE mara-meins,
        matkl  LIKE mara-matkl,
      END   OF itab.

DATA: tab LIKE itab OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF t_log OCCURS 0,
         information(289) TYPE c,   "返回信息
      END OF t_log.
DATA:BEGIN OF iexcel OCCURS 0.
        INCLUDE STRUCTURE alsmex_tabline.
DATA:END OF iexcel.
FIELD-SYMBOLS: <fs>.
SELECTION-SCREEN BEGIN OF BLOCK text WITH FRAME TITLE text-001.
PARAMETERS:p_fname LIKE rlgrap-filename .
SELECTION-SCREEN END OF BLOCK text.
*itab-a = 'A0010000000'.
*APPEND itab.
*itab-a = 'A0010000001'.
*APPEND itab.
*itab-maktx = '测试BDC导入数据'.
*itab-meins = 'BAG'.
*itab-matkl = '100106'.
*
*APPEND itab.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.
  PERFORM upload USING p_fname .

* START-OF-SELECTION
START-OF-SELECTION.

LOOP AT itab.
    PERFORM append_mm01 USING itab-maktx itab-meins itab-matkl.
    PERFORM run_bdc.
  ENDLOOP.

*---------------------------------------------------------------------*
*       FORM APPEND_DATA_MM01                                         *
*---------------------------------------------------------------------*
*       定义一个子程序里面,用来存放操作的各个步骤                     *
*---------------------------------------------------------------------*
FORM append_mm01 USING fval LIKE itab-maktx
                       fval1 LIKE itab-meins
                       fval2  LIKE itab-matkl.
  CLEAR i_bdcdata.
  CLEAR i_bdcdata[].
*开始新的画面事件
*0173画面
*把程序名字和 BDC DYnpro号码,开始标志 写入内表表头,
  PERFORM bdc_dynpro USING 'SAPLMGMM' '0060'.
  PERFORM bdc_field  USING 'BDC_CURSOR' 'RMMG1-MATNR'.
  PERFORM bdc_field  USING 'BDC_OKCODE' '/00'.
  PERFORM bdc_field  USING 'RMMG1-MATNR' ''.
  PERFORM bdc_field  USING 'RMMG1-MBRSH' 'Z'.
  PERFORM bdc_field  USING 'RMMG1-MTART'  'Z001'.
*开始新的画面事件
  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'.
*开始新的画面事件
  PERFORM bdc_dynpro USING 'SAPLMGMM' '4004'.
  PERFORM bdc_field  USING 'BDC_OKCODE' '=BU'.
  PERFORM bdc_field  USING 'BDC_CURSOR' 'MAKT-MAKTX'.
  PERFORM bdc_field  USING  'MAKT-MAKTX' itab-maktx.
  PERFORM bdc_field  USING 'MARA-MEINS'  itab-meins.
  PERFORM bdc_field  USING  'MARA-MATKkL' itab-matkl.

ENDFORM.                    "APPEND_MM01

*---------------------------------------------------------------------*
*       FORM RUN_BDC                                *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM run_bdc.
*开始调用事务MM01,把做好的bdc对象传递过去,然后把模式设置为A,
*就是说所有窗口,均可以看见.
* N 表示不看见吧
  REFRESH t_bdcmsg.
  CLEAR t_bdcmsg.

CALL TRANSACTION 'MM01' USING i_bdcdata MODE 'N' MESSAGES INTO t_bdcmsg.

READ TABLE t_bdcmsg WITH KEY msgtyp = 'S'
                               msgid = 'M3'
                               msgnr = '800'.
  IF sy-subrc EQ 0.
    CONCATENATE  '物料号:' t_bdcmsg-msgv1  '创建成功' INTO t_log.
  ELSE.
    LOOP AT t_bdcmsg.
      IF t_bdcmsg-msgtyp = 'E'
          OR t_bdcmsg-msgv1 <> ''
          OR t_bdcmsg-msgv2 <> ''
          OR t_bdcmsg-msgv3 <> ''
          OR t_bdcmsg-msgv4 <> ''.
        MESSAGE ID t_bdcmsg-msgid                   "收集错误信息,并写入自定义的表t_log中。
                        TYPE 'S'
                        NUMBER t_bdcmsg-msgnr
                        WITH t_bdcmsg-msgv1
            t_bdcmsg-msgv2
            t_bdcmsg-msgv3
            t_bdcmsg-msgv4
            INTO t_log-information.

CONCATENATE
                                t_bdcmsg-msgv1
*                               itab-posnr
                               '出错:'
                               t_log-information
          INTO t_log-information.
        APPEND t_log.
        CLEAR t_log.
      ENDIF.
    ENDLOOP.
  ENDIF.

LOOP AT t_log.                          "显示出错信息

WRITE:/ t_log-information.

ENDLOOP.

ENDFORM.                    "RUN_BDC
*&---------------------------------------------------------------------*
*&      Form  BDC_DYNPRO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0121   text
*      -->P_0122   text
*----------------------------------------------------------------------*
FORM bdc_dynpro  USING    program
                          dynpro.
  i_bdcdata-program = program.
  i_bdcdata-dynpro = dynpro.
  i_bdcdata-dynbegin = 'X'.
  APPEND i_bdcdata.
  CLEAR i_bdcdata.
ENDFORM.                    " BDC_DYNPRO
*&---------------------------------------------------------------------*
*&      Form  BDC_FIELD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0126   text
*      -->P_0127   text
*----------------------------------------------------------------------*
FORM bdc_field  USING  fnam fval.
  i_bdcdata-fnam = fnam .
  i_bdcdata-fval = fval.
  APPEND i_bdcdata.
  CLEAR i_bdcdata.
ENDFORM.                    " BDC_FIELD
*&---------------------------------------------------------------------*
*&      Form  UPLOAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM upload  USING p_fname.
  DATA:cc TYPE i.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      mask             = ',Excel Files,*.xls,All Files,*.*.'(101)
      title            = '选择文件'(100)
    IMPORTING
      filename         = p_fname
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.
  IF sy-subrc <> 0 AND sy-subrc <> 3.
    MESSAGE e398(00) WITH '选择文件出错!'(007).
  ENDIF.

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_fname
      i_begin_col             = 1
      i_begin_row             = 1
      i_end_col               = 100
      i_end_row               = 1000
    TABLES
      intern                  = iexcel
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

IF sy-subrc <> 0.
    WRITE:/'文件读入错误!',p_fname.
  ELSE.
    cc = 1.
    LOOP AT iexcel.
      ASSIGN COMPONENT cc  OF
            STRUCTURE itab TO <fs>.

<fs> = iexcel-value(25).
      cc = cc + 1.
      AT END OF row.
        IF iexcel-row <> '0001'.
          APPEND itab.
        ENDIF.
        CLEAR itab.
        cc = 1.
      ENDAT.

ENDLOOP.
  ENDIF.
ENDFORM.                    " UPLOAD

test_bdc的更多相关文章

随机推荐

  1. 编程算法 - 食物链 并查集 代码(C)

    食物链 并查集 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有N仅仅动物, 分别编号为1,2,...,N. 全部动物都属于A,B,C中的一种 ...

  2. java web 中文乱码

    开发环境:设设置idea的VM options:-Dfile.encoding=UTF-8 生产环境:在tomcat部署目录bin\catalina.bat中,set "JAVA_OPTS= ...

  3. Android 使用SwipeBackLayout实现滑动返回上一级页面——实战来袭

    我们知道.APP在设计上习惯性的把返回button放在屏幕的左上角,那么,在非常多时候(尤其是大屏幕手机),操作改返回button,就会有诸多不便了.为了更加方便实现"返回"功能. ...

  4. 用VS2010创建三层架构开发模式及三层架构的研究

    三层架构的研究 三层体系结构的概念 用户界面表示层(USL) 业务逻辑层(BLL) 数据访问层(DAL) BLL将USL与DAL隔开了,并且加入了业务规则   各层的作用 1:数据数据访问层:主要是对 ...

  5. 【问题记录】web项目访问时出现404

    请一定检查一下项目的Context root是否是你访问时使用的. Context root设置为/时,可以直接用ip+端口访问. Context root设置为项目名的,访问时请带上项目名. 设置方 ...

  6. secureCrt 开启Linux上的oracle服务

    IP   :  192.168.0.21 user:  root pwd:     123456 --------------------------------------------------- ...

  7. k8s集群日志

    硬件环境: 三台虚拟机, 10.10.20.203 部署docker.etcd.flannel.kube-apiserver.kube-controller-manager.kube-schedule ...

  8. saltstack内置state模块file之managed

    managed管理一个模板文件,载入到各个节点并运行相应配置 salt.states.file.managed(name, source=None, source_hash='', user=None ...

  9. CAShapeLayer实现圆形进度条效果

    一.CAShapeLayer简单介绍: .CAShapeLayer继承至CALayer,能够使用CALayer的全部属性值 2.CAShapeLayer须要与贝塞尔曲线配合使用才有意义 3.使用CAS ...

  10. DICOM:C-GET服务

    背景: 之前博文对照过多次C-MOVE与C-GET服务的差别,两者最大的差别在于C-GET是基于单个TCP连接的点对点的双方服务.而C-MOVE是基于两个TCP连接的三方服务(详情參见:<DIC ...