Z 테이블 데이터 EXCEL 가져오기

12281 단어
많은 항목이 이런 수요를 가지고 있다. 비록 다른 사람이 쓰는 것은 각기 다르지만, 한가할 때는 일이 없고, 그래도 스스로 하나 만들었다.EXCEL 기반 가져오기.
 
 
*&---------------------------------------------------------------------*
*& Report  ZLY_UPLOAD_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZUPLOAD_TABLE.
TABLES:RSRD1.
TYPE-POOLS:ABAP.

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
               <DYN_WA>,
               <DYN_FIELD>.
DATA: DY_TABLE TYPE REF TO DATA,
      DY_LINE  TYPE REF TO DATA,
      XFC      TYPE        LVC_S_FCAT,
      IFC      TYPE        LVC_T_FCAT.

DATA : GT_EXCEL_T TYPE         ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE,
       GS_EXCEL_T LIKE LINE OF GT_EXCEL_T.
DATA: GC_FIELDVALUE TYPE FIELDVALUE,  " 
      GC_FLDNAME    TYPE FIELDNAME.   " 

SELECTION-SCREEN:BEGIN OF BLOCK BLK01 WITH FRAME TITLE TEXT-001.
PARAMETERS:P_TBMA TYPE RSRD1-TBMA_VAL,
           P_FILE LIKE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK BLK01.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
  PERFORM FRM_GET_FILEPATH.

START-OF-SELECTION.
  CHECK P_TBMA IS NOT INITIAL.
  CHECK P_FILE IS NOT INITIAL.
  PERFORM PRM_GET_TAB_STRC.
  PERFORM PRM_GET_EXCEL.
  PERFORM PRM_UPDATA_TAB.
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_GET_FILEPATH .
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      MASK             = ',Excel(*.xls),*.XLS,*.XLSX,'
      TITLE            = ' '(100)
    IMPORTING
      FILENAME         = P_FILE
    EXCEPTIONS
      INV_WINSYS       = 1
      NO_BATCH         = 2
      SELECTION_CANCEL = 3
      SELECTION_ERROR  = 4
      OTHERS           = 5.
  IF SY-SUBRC <> 0.
    "      MESSAGE e100(zdev) WITH ' !'(007).
  ENDIF.
ENDFORM.                    " FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*&      Form  PRM_GET_TAB_STRC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM PRM_GET_TAB_STRC .
  DATA:GT_TABLE TYPE DDFIELDS,
       GS_TABLE TYPE DFIES.

  CALL FUNCTION 'CATSXT_GET_DDIC_FIELDINFO'
    EXPORTING
      IM_STRUCTURE_NAME = P_TBMA
    IMPORTING
      EX_DDIC_INFO      = GT_TABLE
    EXCEPTIONS
      FAILED            = 1
      OTHERS            = 2.
  IF SY-SUBRC <> 0.
  ENDIF.

  LOOP AT GT_TABLE INTO GS_TABLE.
    CLEAR XFC.
    XFC-FIELDNAME = GS_TABLE-FIELDNAME .
    XFC-INTTYPE = GS_TABLE-INTTYPE.
    XFC-INTLEN = GS_TABLE-LENG.
    XFC-DECIMALS = GS_TABLE-DECIMALS.
    APPEND XFC TO IFC.
  ENDLOOP.

  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
      IT_FIELDCATALOG = IFC
    IMPORTING
      EP_TABLE        = DY_TABLE.
  ASSIGN DY_TABLE->* TO <DYN_TABLE>.

  CREATE DATA DY_LINE LIKE LINE OF <DYN_TABLE>.
  ASSIGN DY_LINE->* TO <DYN_WA>.
ENDFORM.                    " PRM_GET_TAB_STRC
*&---------------------------------------------------------------------*
*&      Form  PRM_GET_EXCEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM PRM_GET_EXCEL .
  IF P_FILE IS NOT INITIAL.
* excel 
    CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        FILENAME    = P_FILE
        I_BEGIN_COL = '1'
        I_BEGIN_ROW = '2'
        I_END_COL   = '300'
        I_END_ROW   = '50000'
      TABLES
        INTERN      = GT_EXCEL_T.
  ENDIF.

  IF GT_EXCEL_T[] IS NOT INITIAL.
    LOOP AT GT_EXCEL_T INTO GS_EXCEL_T.
      AT NEW ROW.
        CLEAR <DYN_WA>.
      ENDAT.
      IF <DYN_WA> IS INITIAL.
        ASSIGN COMPONENT GS_EXCEL_T-COL OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
        IF SY-SUBRC = 0.
          <DYN_FIELD> = SY-MANDT.
        ENDIF.
      ENDIF.
      GS_EXCEL_T-COL = GS_EXCEL_T-COL + 1.
      GC_FIELDVALUE = GS_EXCEL_T-VALUE.
      CONDENSE GC_FIELDVALUE.
      ASSIGN COMPONENT GS_EXCEL_T-COL OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
      IF SY-SUBRC = 0.
        <DYN_FIELD> = GC_FIELDVALUE.
      ENDIF.
      AT END OF ROW.
        APPEND <DYN_WA> TO <DYN_TABLE>.
      ENDAT.
    ENDLOOP.
  ENDIF.
ENDFORM.                    " PRM_GET_EXCEL
*&---------------------------------------------------------------------*
*&      Form  PRM_UPDATA_TAB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM PRM_UPDATA_TAB .
  MODIFY (P_TBMA) FROM TABLE <DYN_TABLE>.
  IF SY-SUBRC = 0.
    MESSAGE ' ' TYPE 'S'.
    ELSE.
      MESSAGE ' ' TYPE 'S' DISPLAY LIKE 'E'.
      EXIT.
  ENDIF.
ENDFORM.                    " PRM_UPDATA_TAB

 
가장 간단한 방법은 매개 변수를 입력하는 것이다. 테이블 이름, EXXCEL(EXCEL에서 데이터는 테이블의 데이터 요소 순서에 따라 클라이언트를 추가하지 않아도 된다)

좋은 웹페이지 즐겨찾기