现在的位置: 首页 > 综合 > 正文

使用ABAP程序从EXCEL上传料号至SAP

2013年05月28日 ⁄ 综合 ⁄ 共 23932字 ⁄ 字号 评论关闭

 report zcbm0009 no standard page heading message-id zm001.

include zcmb0001.
tables: klah.

databegin of itabs occurs 0,                "spare parts
         matnr(18),
         btype(5),                    "大分类---不上传
         stype(5),                    "中分类----不上传
         salparts(01),                "戰略管制品(Y/N)
         source(01),                                        "2nd source
         bismt(18),                   "Old part number
         mtart(04),
         matkl(09),
         enmaktx(40),
         zhmaktx(40),
         basictext(72),
         commenttx(72),
         meins(03),
         meinh(03),
         umren(05),
         umrez(05),
*Purchasing View-------------------------------------------------------*
         werks(04),
         sastatus(01),                  "驗證狀況:SA status
         fifteen(1),                                        "是否为15号文
         zmaktx(40),                                        "15号文描述
         bstme(03),                     "order unit
         umren1(05),
         umrez1(05),
         inspectiontype(01),
         inspector(30),
         xchpf(01),                      "是否做批次管理
         mfrna(20),                      "機台設備商名稱
         mfrnr(10),                      "机台设备商代码
         mfrpn(40),                      "機台設備商料號
         makername(10),                  "Parts 製造商名稱
         makerperson(20),                "Parts制造商联络人
         telephone(15),                  "Parts 製造商联络电话
*Storage View----------------------------------------------------------*
         raube(02),                      "Storage condition
*MRP View--------------------------------------------------------------*
         machinetype(20),                "機台型號:Machine type
         equipid1(50),
         equipid2(50),
         equipid3(50),
         equipid4(50),
         equipid5(50),
         equipid6(50),
         equipid7(50),
         equipid8(50),
         equipmentcount(02),             "機台數量:Equipment No.
         consumable(01),                 "消耗性(Y/N)
         plifz(03),                      "Planed delivery time
         period(02),                      "單位期間(月):Period(M)
         usagequantity(17),              "Total usage quantity
         bstrf(17),                      "Rounding value
         eisbe(17),                      "Safety stock
         fdata(8),                       "首批需求日 :不上传
         fqty(12),                       "驗收後領用量 :不上传
         disls(02),                      "lot size
*Accounting view-------------------------------------------------------*
         bklas(04),
         std_price(18),
         waers like ekko-waers,
         end of itabs.

databegin of itabh occurs 0,                           "耗材
         matnr(18),
         flag1(01),                     "是否出貨到客戶/不上传
         flag2(01),                     "是否與產品有殘留性接觸/不上传
         flag3(01),                     " 是否需做GP控管/不上传
         requestor(10),                 "實際申請者 /不上传
         mfrpn(40),                     "参考料号/不上传
         source(01),                    "2nd source/不上传
         bismt(18),                     "1st Source 料號/不上传
         mtart(04),
         matkl(09),
         werks(04),
         enmaktx(40),
         zhmaktx(40),
         basictext(72),
         meins(03),
         meinh(03),
         umren(05),
         umrez(05),
         inspectiontype(01),
         inspector(30),
         raube(02),                      "Storage condition
         inspectiontx(72),               "Inspection Text
         consumable(01),                 "消耗性(Y/N)
         period(02),                      "單位期間(月):Period(M)
         usagequantity(17),              "Total usage quantity
         bstrf(17),                      "Rounding value
         eisbe(17),                      "Safety stock
         makercode(10),                  "製造商代碼/不上传
         makername(10),                  "Parts 製造商名稱
         telephone(15),                  "Parts 製造商联络电话
         makeraddress(20),               "地址/不上传
         makerfax(10),                   "传真/不上传
         makerperson(20),                "Parts制造商联络人
         mfrnr(10),                      "供应商代码
         mfrna(20),                      "供应商名稱/不上传
         mfraddress(20),                 "不上传
         mfrtelephone(15),               "不上传
         mfrfax(10),                     "不上传
         mfrperson(20),                  "不上传
         std_price(18),
         waers like ekko-waers,
         plifz(03),                      "Planed delivery time
         end of itabh.

databegin of basicdatatab occurs 0,
         matnr(18),
         mtart(04),
         enmaktx(40),
         zhmaktx(40),
         matkl(09),
         basictext(72),
         commenttx(72),
         inspectiontx(72),               "Inspection Text
         bismt(18),
         normt(18),
         meins(03),
         umren(05),
         meinh(03),
         umrez(05),
         interface(02),
         resolution(02),
         edid(01),
         inch(05),
         cellmode(01),
         salparts(01),
         modelno(30),
         projname(30),
         greendes(20),
         equipid1(50),
         equipid2(50),
         equipid3(50),
         equipid4(50),
         equipid5(50),
         equipid6(50),
         equipid7(50),
         equipid8(50),
         gewei(3),
         ntgew(13),
         brgew(13),
       end of basicdatatab.
databegin of plantdatatab occurs 0,
         matnr(18),
         werks(04),
*Sales View------------------------------------------------------------*
         vkorg(04),
         vtweg(02),
         spart(02),
         tatyp(04),
         taxkm(01),
         stgma(01),
         mtpos(04),
         mtvf1(02),
         tragr(04),
         ktgrm(02),
         magrv(04),
         ladgr(04),
*Purchasing View-------------------------------------------------------*
         sastatus(01),
         bstme(03),
         umren1(05),
         umrez1(05),
         inspectiontype(01),
         inspector(30),
         mprof(04),
         mfrnr(10),
         mfrpn(40),
         makername(10),         "Add 2009/02/24 by andy
         makerperson(20),
         telephone(15),
         fifteen(1),           "add by andy 2009-06-24
         zmaktx(40),
         taxcode(10),
*Storage View----------------------------------------------------------*
         lgort(04),
         raube(02),
         xchpf(01),
         mhdhb(04),
         mhdrz(04),
         ausme(03),
*MRP View--------------------------------------------------------------*
         disgr(04),
         dismm(02),
         dispo(03),
         disls(02),
         beskz(01),
         sobsl(02),
         lgpro(04),
         lgfsb(04),
         rgekm(01),
         dzeit(03),
         eisbe(17),
         mtvf2(02),
         altsl(01),
         prodprof(06),
         bstrf(17),
         fhori(03),
         webaz(03),
         plifz(03),
         sbdkz(01),
         minbe(17),
         bstma(17),
         sauft(01),
         sfepr(04),
         machinetype(20),
         equipmentcount(02),
         whereused(25),
         consumable(01),
         period(02),
         usagequantity(17),
         bstmi(13),
         ekgrp(03),
         rwpro(03),
         strgr(02),
         miskz(01),
         schgt(01),
*Accounting view-------------------------------------------------------*
         bklas(04),
         vprsv(01),
         std_price like mbew-verpr,
         peinh(05),
         prctr(10),
         hrkft(04),
      end of plantdatatab.

databegin of bdc_data,
         matnr(18),
         brgew(13),
         ntgew(13),
         gewei(3),
      end of bdc_data.

databegin of bdctab occurs 0,              "Add 2009/02/24 by andy
          matnr like mara-matnr,
          mtart like mara-mtart,
          werks like marc-werks,
          maktx like makt-maktx,
          meins like mara-meins,
          matkl like mara-matkl,
          xchpf like marc-xchpf,
          mfrpn like mara-mfrpn,
          mfrnr like mara-mfrnr,
          inspectiontype like marc-inspectiontype,
          inspector like marc-inspector,
          makername like mara-makername,
          makerperson like mara-makerperson,
          telephone   like mara-telephone,
          fifteen     like marc-fifteen,
          zmaktx      like marc-zmaktx,              "十五号文描述
          taxcode     like marc-taxcode,
       end of bdctab.
data:bdcdata like bdcdata occurs 0 with header line,
     it_msg1 type table of bdcmsgcoll with header line.

***V002 add end

data: headdata like bapimathead,
      clientdata like bapi_mara,
      clientdatax like bapi_marax,
      plantdata like bapi_marc,
      plantdatax like bapi_marcx,
      storagelocationdata like bapi_mard,
      storagelocationdatax like bapi_mardx,
      valuationdata like bapi_mbew,
      valuationdatax like bapi_mbewx,
      salesdata like bapi_mvke,
      salesdatax like bapi_mvkex,
      materialdescription like bapi_makt occurs 0 with header line,
      unitsofmeasure like bapi_marm occurs 0 with header line,
      unitsofmeasurex like bapi_marmx occurs 0 with header line,
      materiallongtext like bapi_mltx occurs 0 with header line,
      taxclassifications like bapi_mlan occurs 0 with header line,
      returnmessages like bapi_matreturn2 occurs 0 with header line,
      extensionin like bapiparex occurs 0 with header line,
      extensioninx like bapiparexx occurs 0 with header line.

data: bapi1003_key like bapi1003_key.
data: allocvalueschar like  bapi1003_alloc_values_char
      occurs 0 with header line,
      allocvaluesnum like bapi1003_alloc_values_num
      occurs 0 with header line,
      allocvaluescurr like  bapi1003_alloc_values_curr
      occurs 0 with header line,
      return like bapiret2 occurs 0 with header line,
      classif_status like  bapi1003_key-status.

data: checkmaterialexistindicator type c.                   "Add V002.1

data:bsext like t134-bsext,
     bsint like t134-bsint.
databegin of messagelist occurs 0,
          matnr like mara-matnr,
          type like bapiret2-type,
          message like bapiret2-message,
end of messagelist.

databegin of t_equip occurs 0,
      equipid(30),
end of t_equip.

*&---------------------------------------------------------------------*
selection-screen begin of block blk1 with frame.
selection-screen begin of line.
parameters: flag1 radiobutton group outp default 'X'."spare parts 申请
selection-screen comment 3(20text-010.
selection-screen position 50.
parameters: flag2 radiobutton group outp ."耗材申请
selection-screen comment 53(20text-020.
selection-screen end of line.
parameters: filenam(128type c.
selection-screen end of block blk1.

selection-screen begin of block blk3 with frame.
parameters: mbrsh like mara-mbrsh obligatory default 'L',
            maxline1 type i default 10,
            maxline2 type i default 10,
            message as checkbox default 'X',
            testrun as checkbox default 'X'.
***********shadow 2007/05/21
selection-screen end of block blk3.
selection-screen begin of line.
selection-screen comment (79) txt-006.
selection-screen end of line.
selection-screen begin of line.
selection-screen comment (79) txt-003.
selection-screen end of line.
selection-screen begin of line.
selection-screen comment (79) txt-004.
selection-screen end of line.
selection-screen begin of line.
selection-screen comment (79) txt-005.
selection-screen end of line.

at selection-screen on value-request for filenam.
  perform choose_input_file using filenam.

at selection-screen.
  perform check_file_whether_exist using filenam.

  if maxline1 > 65536 or maxline1 < 3.
    message e000 with 'Maximum >= 3/Maximum <= 65536 please!!!'.
  endif.
  if maxline2 > 65536 or maxline2 < 3.
    message e000 with 'Maximum >= 3/Maximum <= 65536 please!!!'.
  endif.

*
start-of-selection.
  perform upload_data_from_file.
  perform proces_update_data.
  perform list_return_message.
*
*end-of-selection.
**&---------------------------------------------------------------------*
**&      Form  choose_input_file
**&---------------------------------------------------------------------*
form choose_input_file using pfile.
  call function 'WS_FILENAME_GET'
    exporting
      def_filename     = '*.xls'
      def_path         = 'C:/'
      mask             = ',*.xls.'
      mode             = 'O'
      title            = 'Choose Input file'(002)
    importing
      filename         = pfile
    exceptions
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      others           = 5.
endform.                    " choose_input_file
**&---------------------------------------------------------------------*
**&      Form  check_file_whether_exist
**&---------------------------------------------------------------------*
form check_file_whether_exist using filename.
  data: inputfile type string,
        result type c.

  inputfile = filename.
  call method cl_gui_frontend_services=>file_exist
    exporting
      file                 = inputfile
    receiving
      result               = result
    exceptions
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      others               = 5.

  if result is initial.
    message e000 with 'File' inputfile '  does not exist'.
  endif.
endform.                    " check_file_whether_exist
*
*&---------------------------------------------------------------------*
*&      Form  upload_data_from_file
*&---------------------------------------------------------------------*
form upload_data_from_file.
  if flag1 = 'X'.
    perform get_data_from_file tables itabs using filenam maxline1 2 4.
  else.
    perform get_data_from_file tables itabh using filenam maxline1 2 4.
  endif.
endform.                    " upload_data_from_file
*
*&---------------------------------------------------------------------*
*&      Form  get_data_from_file
*&---------------------------------------------------------------------*
form get_data_from_file tables datatab using inputfile maxline col row .
  data: filename like rlgrap-filename,
        datacont like alsmex_tabline occurs 0 with header line,
        maxcolno type i.
  field-symbols <fs> type any.

  refresh datatab. clear datatab.

  do.
    assign component sy-index of structure datatab to <fs>.
    if sy-subrc ne 0.
      maxcolno = sy-index .
      exit.
    endif.
  enddo.

  filename = inputfile.
  call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    exporting
      filename                = filename
      i_begin_col             = col
      i_begin_row             = row
      i_end_col               = maxcolno
      i_end_row               = maxline
    tables
      intern                  = datacont
    exceptions
      inconsistent_parameters = 1
      upload_ole              = 2
      others                  = 3.

  loop at datacont.
    at new row.
      clear datatab.
    endat.
    assign component datacont-col of structure datatab to <fs>.
    <fs> = datacont-value.
    at end of row.
      append datatab. clear datatab.
    endat.
  endloop.
endform.                    " get_data_from_file
*
*&---------------------------------------------------------------------*
*&      Form  proces_update_data
*&---------------------------------------------------------------------*
form proces_update_data.
  data:curr_rate type curr_rate.

  if flag1 = 'X'.
    loop at itabs.

      perform convert_to_other_currency(zbx0001) using itabs-waers
                                                       sy-datum
                                                       'RMB'
                                                   changing itabs-std_price curr_rate .

      move-corresponding itabs to plantdatatab.
      move-corresponding itabs to basicdatatab.
      append plantdatatab.append basicdatatab.
    endloop.
  else.
    loop at itabh.
      perform convert_to_other_currency(zbx0001) using itabh-waers
                                                     sy-datum
                                                     'RMB'
                                                 changing itabh-std_price curr_rate .
      move-corresponding itabh to plantdatatab.
      move-corresponding itabh to basicdatatab.
      append plantdatatab.append basicdatatab.
    endloop.
  endif.
  perform process_basic_plant_data.
*  perform process_only_basic_data.
endform.                    " proces_update_data
*

*&---------------------------------------------------------------------*
*&      Form  process_basic_plant_data
*&---------------------------------------------------------------------*
form process_basic_plant_data.
  data: mtart like mara-mtart,
        exitindicator type c.

  loop at plantdatatab.
    perform clear_variables.
    read table basicdatatab with key matnr = plantdatatab-matnr.
    call function 'CONVERSION_EXIT_ALPHA_INPUT'
      exporting
        input  = plantdatatab-matnr
      importing
        output = plantdatatab-matnr.

    perform convert_header_data.
    perform convert_client_data.
    perform convert_matl_description.
    perform convert_unit_of_measure.
    perform convert_bdc_data.                               "V002 add
*----------------------------------------------------------------------*
    perform convert_plant_data.
    perform convert_storagedata_data.
    perform convert_valuation_data.
    perform convert_sales_data.
    perform update_material_master.
****************************************"Add 2009/02/24 by andy***********************************************
    refresh bdctab.
    select a~matnr mtart werks maktx meins matkl c~xchpf mfrpn mfrnr inspectiontype inspector
                     into corresponding fields of bdctab from mara
                     as a inner join marc as c on a~matnr = c~matnr
                          inner join makt as t on a~matnr = t~matnr
                                            where a~matnr = plantdatatab-matnr
                                              and c~werks = plantdatatab-werks
                                              and t~spras = 'E'.
      call function 'CONVERSION_EXIT_CUNIT_OUTPUT'
        exporting
          input          = bdctab-meins
          language       = sy-langu
        importing
          output         = bdctab-meins
        exceptions
          unit_not_found = 1
          others         = 2.
      if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      endif.

      bdctab-makerperson = plantdatatab-makerperson.
      bdctab-makername   = plantdatatab-makername.
      bdctab-telephone   = plantdatatab-telephone.
      bdctab-fifteen     = plantdatatab-fifteen.
      bdctab-zmaktx      = plantdatatab-zmaktx.
      bdctab-taxcode     = plantdatatab-taxcode.
      append bdctab.clear bdctab.
    endselect.

    loop at bdctab.
      perform bdc_dynpro(zbx0001) tables bdcdata  using 'SAPLMGMM' '0060'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_CURSOR'
                                                        'RMMG1-MATNR'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_OKCODE'
                                                        '=ENTR'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'RMMG1-MATNR'
                                                         bdctab-matnr.
      perform bdc_dynpro(zbx0001) tables bdcdata  using 'SAPLMGMM'
                                                        '0070'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_CURSOR'
                                                        'MSICHTAUSW-DYTXT(03)'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_OKCODE'
                                                        '=SCHL'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MSICHTAUSW-KZSEL(03)'
                                                        'X'.

      perform bdc_dynpro(zbx0001) tables bdcdata  using 'SAPLMGMM'
                                                        '0080'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_CURSOR'
                                                        'RMMG1-WERKS'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_OKCODE'
                                                        '=ENTR'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'RMMG1-WERKS'
                                                         bdctab-werks.
      perform bdc_dynpro(zbx0001) tables bdcdata  using 'SAPLMGMM'
                                                        '4000'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_OKCODE'
                                                        '=BU'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MAKT-MAKTX'
                                                         bdctab-maktx.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MARA-MEINS'
                                                         bdctab-meins.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MARA-MATKL'
                                                         bdctab-matkl.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MARC-XCHPF'
                                                         bdctab-xchpf.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MARA-MFRPN'
                                                         bdctab-mfrpn.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MARA-MFRNR'
                                                         bdctab-mfrnr.

      perform bdc_field(zbx0001)  tables bdcdata  using 'MARC-INSPECTIONTYPE'
                                                         bdctab-inspectiontype.

      perform bdc_field(zbx0001)  tables bdcdata  using 'MARC-INSPECTOR'
                                                         bdctab-inspector.
      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_CURSOR'
                                                        'MARA-MAKERNAME'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MARA-MAKERNAME'
                                                         bdctab-makername.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MARA-MAKERPERSON'
                                                         bdctab-makerperson.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MARA-TELEPHONE'
                                                         bdctab-telephone.

      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_CURSOR'
                                                        'MARC-FIFTEEN'.
      perform bdc_field                           using 'MARC-FIFTEEN'
                                                         bdctab-fifteen.

      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_CURSOR'
                                                        'MARC-ZMAKTX'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MARC-ZMAKTX'
                                                         bdctab-zmaktx.

      perform bdc_field(zbx0001)  tables bdcdata  using 'BDC_CURSOR'
                                                'MARC-TAXCODE'.
      perform bdc_field(zbx0001)  tables bdcdata  using 'MARC-TAXCODE'
                                                         bdctab-taxcode.

      call transaction 'MM02' using bdcdata mode 'N' update 'S'
                                   messages into it_msg1.
      if sy-subrc ne 0.
        loop at it_msg1.
          concatenate 'Error:' it_msg1-msgv1 into message separated by ' '.
        endloop.
      endif.
      refresh bdcdata.
    endloop.

    perform update_material_classify.
  endloop.
endform.                    " process_basic_plant_data
*
*&---------------------------------------------------------------------*
*&      Form  process_only_basic_data
*&---------------------------------------------------------------------*
*form process_only_basic_data.
*  loop at basicdatatab.
*    perform clear_variables.
*    perform convert_header_data.
*    perform convert_client_data.
*    perform convert_matl_description.
*    perform convert_unit_of_measure.
*    perform convert_bdc_data.                               "V002 add
**----------------------------------------------------------------------*
*    perform update_material_master.
*    perform update_material_classify. " add 2005/08/19
*  endloop.
*endform.                    " process_only_basic_data
*
*&---------------------------------------------------------------------*
*&      Form 

抱歉!评论已关闭.