Writing data to a file in ABAP in File Handling

 

  • In ABAP we can use GUI_DOWNLOAD function module to download the contents of a internal table into file.
  • let’s take a requirement to understand it more clearly.

Requirement :-

  • Use want to download the details of Sales Document for a particular range into his local desktop.



Hints:-

  • We will use F4_FILENAME function module to select the file name from our PC then we will use GUI_DOWNLOAD function module to download the contents into our local system.

Solution :-

  • Step 1 :- Create a executable program in ABAP Editor.



  • Step 2 :- Create a type structure for VBAK table and then create its corresponding internal table.



  • Step 3 :- create a select option for user input.



  • Step 4 :- Create a parameter to take file name from our local system.



  • Step 4 :- Use At selection screen for value request and then call F4_FILENAME function module.



  • Step 5 :- In start of selection we will write select query to fetch data into internal table.



  • Use Function module GUI_DOWNLOAD to download the file into selected file.




Code :-

*&---------------------------------------------------------------------*
*& Report ZAR_WRITE_FILE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zar_write_file.

TYPES : BEGIN OF ty_vbak,
          vbeln TYPE vbeln_va,
          erdat TYPE erdat,
          erzet TYPE erzet,
          ernam TYPE ernam,
          vbtyp TYPE vbtypl,
        END OF ty_vbak.

DATA : lt_vbak type table of ty_vbak.
DATA : lo_file type string.

DATA : lv_vbeln type vbeln_va.
SELECT-OPTIONS : s_vbeln for lv_vbeln.
PARAMETERS : p_file type localfile.

AT SELECTION-SCREEN on VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'
 EXPORTING
   PROGRAM_NAME        = SYST-CPROG
   DYNPRO_NUMBER       = SYST-DYNNR
   FIELD_NAME          = ' '
 IMPORTING
   FILE_NAME           = p_file
          .

lo_file = p_file.

START-OF-SELECTION.
Select vbeln erdat erzet ernam vbtyp
  from vbak into table lt_vbak
  where vbeln in s_vbeln.

  If lt_vbak is not INITIAL.
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename                        = lo_file
*       FILETYPE                        = 'ASC'
*       APPEND                          = ' '
       WRITE_FIELD_SEPARATOR           = 'X'
      tables
        data_tab                        = lt_vbak
     EXCEPTIONS
       FILE_WRITE_ERROR                = 1
       NO_BATCH                        = 2
       GUI_REFUSE_FILETRANSFER         = 3
       INVALID_TYPE                    = 4
       NO_AUTHORITY                    = 5
       UNKNOWN_ERROR                   = 6
       HEADER_NOT_ALLOWED              = 7
       SEPARATOR_NOT_ALLOWED           = 8
       FILESIZE_NOT_ALLOWED            = 9
       HEADER_TOO_LONG                 = 10
       DP_ERROR_CREATE                 = 11
       DP_ERROR_SEND                   = 12
       DP_ERROR_WRITE                  = 13
       UNKNOWN_DP_ERROR                = 14
       ACCESS_DENIED                   = 15
       DP_OUT_OF_MEMORY                = 16
       DISK_FULL                       = 17
       DP_TIMEOUT                      = 18
       FILE_NOT_FOUND                  = 19
       DATAPROVIDER_EXCEPTION          = 20
       CONTROL_FLUSH_ERROR             = 21
       OTHERS                          = 22
              .

  endif.

Execute the Code :-



  • In P_FILE Press F4 to select the file.
  • Give a range of input for Sales Document number.


  • Press F8 → Click allow.
  • Data will be downloaded.


Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Unlocking SAP ABAP Secrets: Interactive Classical Reports with GET CURSOR, At User Command, and a Comparison of HIDE Statements

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming