메뉴 건너뛰기

SAP 한국 커뮤니티

Convert any internal table to a comma separated file

sapjoy 2006.12.20 23:02 조회 수 : 4474 추천:34

FORM CONVERT_TABLE_TO_DIF TABLES SOURCE_TABLE
                                 TARGET_TABLE
                          USING  TABNAME.

DATA:  BEGIN OF TABLE_DEF OCCURS 50.     "Table with dadadictionary
         INCLUDE STRUCTURE DFIES.        "information on source table
DATA:  END OF TABLE_DEF.

FIELD-SYMBOLS: <SOURCE>                  "Source input
              ,<TARGET>                  "Target result
              ,<FIELD>.                  "Field contents

DATA: L           TYPE I                 "Field with condensed length
     ,TPTR        LIKE SY-FDPOS          "Target pointer
     ,NUMOFFIELDS TYPE I                 "No of fields in structure
     ,VALUE(255)  TYPE C.                "Max field length

* Get information about table structure for the table to download
  CALL FUNCTION 'GET_FIELDTAB'
    EXPORTING  TABNAME        = TABNAME
               LANGU          = SY-LANGU
    TABLES     FIELDTAB       = TABLE_DEF
    EXCEPTIONS NO_TEXTS_FOUND = 1
               OTHERS         = 2.

  DESCRIBE TABLE TABLE_DEF LINES NUMOFFIELDS.
  IF SY-SUBRC NE OK.                     "check that the definition
    ERRORMESSAGE = TEXT-012.             "table is not empty
    PERFORM ERROR_HANDLING.              "General error handling
  ENDIF.

* Build download table
  LOOP AT SOURCE_TABLE.
    TPTR = 0.
    CLEAR TARGET_TABLE-BUFFER.
    LOOP AT TABLE_DEF.
      CLEAR: <FIELD>.
*     Positioning in sourcebuffer = character view
      ASSIGN SOURCE_TABLE+TABLE_DEF-OFFSET(TABLE_DEF-INTLEN) TO <FIELD>
             TYPE TABLE_DEF-INTTYPE .

*     Depending on source field datatype
      CASE TABLE_DEF-INTTYPE.
        WHEN 'C'.
           WRITE <FIELD> TO VALUE.
           TRANSLATE VALUE USING '" ' .        
        WHEN 'P'.
           DO TABLE_DEF-DECIMALS TIMES.
             COMPUTE <FIELD> = <FIELD> / 10.
           ENDDO.
           WRITE <FIELD> DECIMALS TABLE_DEF-DECIMALS TO VALUE.
        WHEN 'D'.
           WRITE <FIELD> TO VALUE.
        WHEN 'N'.
           WRITE <FIELD> TO VALUE NO-ZERO.
        WHEN OTHERS.
           WRITE <FIELD> TO VALUE.
      ENDCASE.

*     Trim outputfield for blanks
      CONDENSE VALUE.
      L = STRLEN( VALUE ).

      ASSIGN TARGET_TABLE-BUFFER+TPTR(TABLE_DEF-OUTPUTLEN) TO
        <TARGET> TYPE 'C'.
      MOVE '"' to <TARGET>.
      ADD 1 TO TPTR.

      ASSIGN TARGET_TABLE-BUFFER+TPTR(TABLE_DEF-OUTPUTLEN) TO
        <TARGET> TYPE 'C'.
      MOVE VALUE TO <TARGET>.
      ADD L TO TPTR.

      ASSIGN TARGET_TABLE-BUFFER+TPTR(TABLE_DEF-OUTPUTLEN) TO
        <TARGET> TYPE 'C'.
      MOVE '"' to <TARGET> .
      ADD 1 TO TPTR.

*     If not the last field, we insert the delimiter comma
      IF NUMOFFIELDS > SY-TABIX.
        ASSIGN TARGET_TABLE-BUFFER+TPTR(TABLE_DEF-OUTPUTLEN) TO
          <TARGET> TYPE 'C'.
        MOVE ',' TO <TARGET> .
        ADD 1 TO TPTR.
      ENDIF.
    ENDLOOP.
    APPEND TARGET_TABLE.
  ENDLOOP.
ENDFORM.