*---------------------------------------------------------------------*
* Many externa desktop applications expect incomming data in comma    *
* separated file format. But R/3 does not have any standard functions *
* for convertting tables to this format. But with this rutine you are *
* able to create your own function module used for converting to comma*
* separated files.                                                    *
*                                                                     *
* Form rutine used for converting any internal table defined in the   *
* datadictionary as comma separated records in a nother internal table*
* with only one column of any length.                                 *
*                                                                     *
* "<FIELD-1 CONTENTS>","<FIELD-2 CONTENTS>","<FIELD-3 CONTENTS>"      *
*                                                                     *
*---------------------------------------------------------------------*
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.