ABAPExcel剪贴板内容批量上传实现

Posted SAP剑客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABAPExcel剪贴板内容批量上传实现相关的知识,希望对你有一定的参考价值。

【ABAP】Excel剪贴板内容批量上传实现

为了快速导入数据实现某类操作,可以使用ABAP类“CL_GUI_FRONTEND_SERVICES”中的“CLIPBOARD_IMPORT”方法从剪切板复制到内表。

比如有下面这样一个小需求,Excel内有1000多个PO和PR需要批量更改其交货日期,若不采用Excel上传的方式,则可以通过上述方式快速处理。

   CALL METHOD CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_IMPORT
    IMPORTING
      DATA                 = ITAB[]
*     LENGTH               =
    EXCEPTIONS
      CNTL_ERROR           = 1
      ERROR_NO_GUI         = 2
      NOT_SUPPORTED_BY_GUI = 3
      OTHERS               = 4.
  IF SY-SUBRC <> 0.
  ENDIF.

若直接通过 “CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_IMPORT”方法上传,而不做任何处理,则默认以“#”分割。

其运行结果当然是不尽如人意的。

 通过修正后的完整代码如下,后续可Copy直接使用。 


DATA : BEGIN OF ITAB OCCURS 0,
          EBELN TYPE EBELN,
          EBELP TYPE EBELP,
          LFDAT	TYPE EINDT,
       END OF ITAB.
TYPES : BEGIN OF TY_ITAB,
          EBELN TYPE EBELN,
          EBELP TYPE EBELP,
          LFDAT	TYPE EINDT,
          STATUS_ICON TYPE ICON_D,
          MSG TYPE CHAR50,
        END OF TY_ITAB.
DATA : GT_ITAB TYPE STANDARD TABLE OF TY_ITAB,
       GW_ITAB LIKE LINE OF GT_ITAB,
       GT_RET  TYPE TABLE OF BAPIRET2,
       GS_RET  LIKE LINE OF  GT_RET.

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

  "获取Excel剪贴板数据
  PERFORM CLIPTOITAB TABLES  ITAB.

  "确认数据
  WRITE : / 'Excel剪切板数据:'.
  LOOP AT ITAB.
    WRITE : / ITAB-EBELN,ITAB-EBELP,ITAB-LFDAT.
  ENDLOOP.

  LOOP AT ITAB.
    CLEAR GW_ITAB.
    MOVE-CORRESPONDING ITAB TO GW_ITAB.
    SELECT COUNT(*) FROM EBAN
      WHERE BANFN = ITAB-EBELN.
    IF SY-SUBRC = 0.
      PERFORM CHANGE_PR USING GW_ITAB.
    ELSE.
      SELECT COUNT(*) FROM EKKO
        WHERE EBELN = ITAB-EBELN.
      IF SY-SUBRC = 0.
        PERFORM CHANGE_PO USING GW_ITAB.
      ELSE.
        GW_ITAB-MSG = 'Not PR/PO'.
        GW_ITAB-STATUS_ICON = ICON_INCOMPLETE.
      ENDIF.
    ENDIF.
    APPEND GW_ITAB TO GT_ITAB.
  ENDLOOP.


  WRITE : /.
  WRITE : / '处理后的数据:'.

  "确认数据
  LOOP AT GT_ITAB INTO GW_ITAB.
    WRITE : / GW_ITAB-EBELN,GW_ITAB-EBELP,GW_ITAB-LFDAT,GW_ITAB-MSG,GW_ITAB-STATUS_ICON.
  ENDLOOP.


*&---------------------------------------------------------------------*
*&      Form  CLIPTOITAB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ITAB       text
*----------------------------------------------------------------------*
FORM CLIPTOITAB  TABLES   ITAB.

  DATA : HTAB TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
  DATA : LT_CLIP TYPE TABLE OF CHAR2048 WITH HEADER LINE,
         LT_FLD TYPE TABLE OF CHAR2048 WITH HEADER LINE .
  DATA : CXROOT TYPE REF TO CX_ROOT,
         EXCMSG TYPE STRING.
  FIELD-SYMBOLS : <FS_FLD>,
                  <FS_TAB>.

  ASSIGN ITAB TO <FS_TAB>.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_IMPORT
    IMPORTING
      DATA                 = LT_CLIP[]
*     LENGTH               =
    EXCEPTIONS
      CNTL_ERROR           = 1
      ERROR_NO_GUI         = 2
      NOT_SUPPORTED_BY_GUI = 3
      OTHERS               = 4.
  IF SY-SUBRC <> 0.
  ELSE.
    CALL METHOD CL_GUI_CFW=>FLUSH.
    LOOP AT LT_CLIP.
      REFRESH LT_FLD[].
      SPLIT LT_CLIP AT HTAB INTO TABLE LT_FLD.
      LOOP AT LT_FLD.
        ASSIGN COMPONENT SY-TABIX OF STRUCTURE <FS_TAB> TO <FS_FLD>.
        CHECK SY-SUBRC = 0.
        TRY.
            LT_FLD = TRANSLATE( VAL = LT_FLD FROM = |\\r| TO = '' ).
            LT_FLD = TRANSLATE( VAL = LT_FLD FROM = |\\n| TO = '' ).
            <FS_FLD> = LT_FLD.
          CATCH CX_ROOT INTO CXROOT.
            EXCMSG = CXROOT->GET_TEXT( ).
        ENDTRY.
      ENDLOOP.
      APPEND ITAB.
      CLEAR ITAB.
    ENDLOOP.
    IF EXCMSG IS NOT INITIAL.
      MESSAGE W000(OO) WITH 'Data Conversion Error'.
    ENDIF.
  ENDIF.
ENDFORM.                    " CLIPTOITAB

*&---------------------------------------------------------------------*
*&      Form  CHANGE_PR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PS_DATA    text
*----------------------------------------------------------------------*
FORM CHANGE_PR USING PS_DATA TYPE TY_ITAB.

  DATA: LT_ITEM TYPE TABLE OF BAPIMEREQITEMIMP,
        LS_ITEM LIKE LINE OF LT_ITEM.
  DATA: LT_ITEMX TYPE TABLE OF BAPIMEREQITEMX,
        LS_ITEMX LIKE LINE OF LT_ITEMX.

  CLEAR: LS_ITEM.
  LS_ITEM-PREQ_ITEM = PS_DATA-EBELP.
  LS_ITEM-DELIV_DATE = PS_DATA-LFDAT.
  APPEND LS_ITEM TO LT_ITEM.
  CLEAR: LS_ITEMX.
  LS_ITEMX-PREQ_ITEM = PS_DATA-EBELP.
  LS_ITEMX-PREQ_ITEMX = 'X'.
  LS_ITEMX-DELIV_DATE = 'X'.
  APPEND LS_ITEMX TO LT_ITEMX.

  EXPORT L = 'X' TO MEMORY ID 'IGNORE_VAATZ'.
  CLEAR GT_RET.
  CLEAR GS_RET.
  CALL FUNCTION 'BAPI_PR_CHANGE'
    EXPORTING
      NUMBER                       = PS_DATA-EBELN
*       PRHEADER                     =
*       PRHEADERX                    =
*       TESTRUN                      =
*     IMPORTING
*       PRHEADEREXP                  =
    TABLES
      RETURN                       = GT_RET
      PRITEM                       = LT_ITEM
      PRITEMX                      = LT_ITEMX
*        PRITEMEXP
*       PRITEMSOURCE                 =
*       PRACCOUNT                    =
*       PRACCOUNTPROITSEGMENT        =
*       PRACCOUNTX                   =
*       PRADDRDELIVERY               =
*       PRITEMTEXT                   =
*       PRHEADERTEXT                 =
*       EXTENSIONIN                  =
*       EXTENSIONOUT                 =
*       PRVERSION                    =
*       PRVERSIONX                   =
*       ALLVERSIONS                  =
*       PRCOMPONENTS                 =
*       PRCOMPONENTSX                =
*       SERVICEOUTLINE               =
*       SERVICEOUTLINEX              =
*       SERVICELINES                 =
*       SERVICELINESX                =
*       SERVICELIMIT                 =
*       SERVICELIMITX                =
*       SERVICECONTRACTLIMITS        =
*       SERVICECONTRACTLIMITSX       =
*       SERVICEACCOUNT               =
*       SERVICEACCOUNTX              =
*       SERVICELONGTEXTS             =
*       SERIALNUMBER                 =
*       SERIALNUMBERX                =
            .
  FREE MEMORY ID 'IGNORE_VAATZ'.
  READ TABLE GT_RET TRANSPORTING NO FIELDS
                    WITH KEY ID = '06'
                             NUMBER = '403'.
  IF SY-SUBRC = 0.
    CALL FUNCTION  'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      WAIT           = 'X'
*       IMPORTING
*         RETURN        =
              .
    PS_DATA-STATUS_ICON = ICON_CHECKED.
    PS_DATA-MSG = 'OK'.
  ELSE.
    CALL FUNCTION  'BAPI_TRANSACTION_ROLLBACK'
*       IMPORTING
*         RETURN        =
              .
    PS_DATA-STATUS_ICON = ICON_INCOMPLETE.
    PS_DATA-MSG = 'Error'.
*    PERFORM DISPLAY_BAPI_RETURN.
  ENDIF.
ENDFORM.                    " CHANGE_PR
*&---------------------------------------------------------------------*
*&      Form  CHANGE_PO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PS_DATA    text
*----------------------------------------------------------------------*
FORM CHANGE_PO USING PS_DATA TYPE TY_ITAB.

  DATA: LT_SCHE  TYPE TABLE OF BAPIMEPOSCHEDULE,
        LS_SCHE LIKE LINE OF  LT_SCHE.
  DATA: LT_SCHEX  TYPE TABLE OF BAPIMEPOSCHEDULX,
        LS_SCHEX LIKE LINE OF  LT_SCHEX.

  CLEAR: LS_SCHE.
  LS_SCHE-PO_ITEM = PS_DATA-EBELP.
  LS_SCHE-DELIVERY_DATE = PS_DATA-LFDAT.
  APPEND LS_SCHE TO LT_SCHE.
  CLEAR: LS_SCHEX.
  LS_SCHEX-PO_ITEM = PS_DATA-EBELP.
  LS_SCHEX-PO_ITEMX = 'X'.
  LS_SCHEX-DELIVERY_DATE = 'X'.
  APPEND LS_SCHEX TO LT_SCHEX.

  CLEAR: GT_RET.
  CLEAR: GS_RET.
  CALL FUNCTION 'BAPI_PO_CHANGE'
    EXPORTING
      PURCHASEORDER                = PS_DATA-EBELN
*       POHEADER                     =
*       POHEADERX                    =
*       POADDRVENDOR                 =
*       TESTRUN                      =
*       MEMORY_UNCOMPLETE            =
*       MEMORY_COMPLETE              =
*       POEXPIMPHEADER               =
*       POEXPIMPHEADERX              =
*       VERSIONS                     =
*       NO_MESSAGING                 =
*       NO_MESSAGE_REQ               =
*       NO_AUTHORITY                 =
*       NO_PRICE_FROM_PO             =
*       PARK_UNCOMPLETE              =
*       PARK_COMPLETE                =
*     IMPORTING
*       EXPHEADER                    =
*       EXPPOEXPIMPHEADER            =
    TABLES
      RETURN                       = GT_RET
*       POITEM                       =
*       POITEMX                      =
*       POADDRDELIVERY               =
      POSCHEDULE                   = LT_SCHE
      POSCHEDULEX                  = LT_SCHEX
*       POACCOUNT                    =
*       POACCOUNTPROFITSEGMENT       =
*       POACCOUNTX                   =
*       POCONDHEADER                 =
*       POCONDHEADERX                =
*       POCOND                       =
*       POCONDX                      =
*       POLIMITS                     =
*       POCONTRACTLIMITS             =
*       POSERVICES                   =
*       POSRVACCESSVALUES            =
*       POSERVICESTEXT               =
*       EXTENSIONIN                  =
*       EXTENSIONOUT                 =
*       POEXPIMPITEM                 =
*       POEXPIMPITEMX                =
*       POTEXTHEADER                 =
*       POTEXTITEM                   =
*       ALLVERSIONS                  =
*       POPARTNER                    =
*       POCOMPONENTS                 =
*       POCOMPONENTSX                =
*       POSHIPPING                   =
*       POSHIPPINGX                  =
*       POSHIPPINGEXP                =
*       POHISTORY                    =
*       POHISTORY_TOTALS             =
*       POCONFIRMATION               =
*       SERIALNUMBER                 =
*       SERIALNUMBERX                =
*       INVPLANHEADER                =
*       INVPLANHEADERX               =
*       INVPLANITEM                  =
*       INVPLANITEMX                 =
*       POHISTORY_MA                 =
            .

  READ TABLE GT_RET TRANSPORTING NO FIELDS
                    WITH KEY ID = '06'
                             NUMBER = '023'.
  IF SY-SUBRC = 0.
    CALL FUNCTION  'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      WAIT           = 'X'
*       IMPORTING
*         RETURN        =
              .
    PS_DATA-STATUS_ICON = ICON_CHECKED.
    PS_DATA-MSG = 'OK'.
  ELSE.
    CALL FUNCTION  'BAPI_TRANSACTION_ROLLBACK'
*       IMPORTING
*         RETURN        =
              .
    PS_DATA-STATUS_ICON = ICON_INCOMPLETE.
    PS_DATA-MSG = 'Error'.
*    PERFORM DISPLAY_BAPI_RETURN.
  ENDIF.

ENDFORM.                    " CHANGE_PO
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_BAPI_RETURN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM DISPLAY_BAPI_RETURN .
*   Reset message
  CALL FUNCTION 'MESSAGES_INITIALIZE'.

  LOOP AT GT_RET INTO GS_RET.
*   Send message
    CALL FUNCTION 'MESSAGE_STORE'
      EXPORTING
        ARBGB = GS_RET-ID
        MSGTY = GS_RET-TYPE
        MSGV1 = GS_RET-MESSAGE_V1
        MSGV2 = GS_RET-MESSAGE_V2
        MSGV3 = GS_RET-MESSAGE_V3
        MSGV4 = GS_RET-MESSAGE_V4
        TXTNR = GS_RET-NUMBER.
  ENDLOOP.

*   Show message
  CALL FUNCTION 'MESSAGES_SHOW'
    EXPORTING
      OBJECT = 'Error'."전기에러

*   Reset message
  CALL FUNCTION 'MESSAGES_INITIALIZE'.
ENDFORM. " DISPLAY_BAPI_RETURN

比如我们复制Excel内的5条记录,其运行结果如下:

 

 

以上是关于ABAPExcel剪贴板内容批量上传实现的主要内容,如果未能解决你的问题,请参考以下文章

批量上传图片到 s-s-rS

独具匠心的采采Lazada批量采集上货管理软件-100个理由选择它

umeditor实现ctrl+v粘贴word图片并上传

网页是如何实现从剪贴板从读取图片并上传到server的

使用boto3批量上传图片到S3

Nexus 支持批量上传工件吗?