abapsap-guiinternal-tables

How to create a txt-file on the application server filled with an internal table?


I'm learning ABAP at the moment and got the task to build a function that creates a .txt file or a .csv file from an internal table and save it on the application server. I know you can use, for example, GUI_DOWNLOAD for that, but the task is to build my own function to do that.

So I got an internal table filled and I want that to be saved as .txt file on the AS. I'm coding in Eclipse, BTW.

Anybody has an idea how to do that, using a function?

EDIT:

Here's what I tried already:

I created a function in the function builder. On the import parameters I put a parameter with the name "lv_mytab" with type table.

The source code of my function looks like this:

*"-------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     REFERENCE(LV_MYTAB) TYPE  TABLE
*"-------------------------------------------------------------------

DATA(myfile) = '/usr/sap/S42/data/textfile.txt'.
 
OPEN DATASET myfile FOR OUTPUT IN TEXT MODE ENCODING DEFAULT WITH SMART LINEFEED.
  
LOOP AT lv_mytab into lt_table.
  
TRANSFER lt_table to myfile.
  
CLOSE DATASET myfile.  

In my program I tried calling the function like this:

CALL FUNCTION 'EXPORT_TXT_CSV_MR'
        EXPORTING
            lv_mytab            = lt_summe.

lt_summe is the internal table I want to be exported as txt or csv.


Solution

  • Okay with the help of a fellow student I got it working. Heres the solution I found:

    Code source of the function:

    FUNCTION EXPORT_TXT_CSV_MR.
    *"----------------------------------------------------------------------
    *"*"Lokale Schnittstelle:
    *"  IMPORTING
    *"     REFERENCE(S_FILENAME) TYPE  STRING
    *"  CHANGING
    *"     REFERENCE(GT_MYTAB) TYPE  STANDARD TABLE
    *"     REFERENCE(GD_VERARBEITUNG) TYPE  INT4
    *"----------------------------------------------------------------------
    
    DATA:
    ld_filename TYPE string,
    ld_datei type standard table of sbook,
    ld_Zeile like line of ld_datei,
    gd_useraction TYPE i,
    lt_Ausgabe TYPE STANDARD TABLE OF sbook.
    
    
    
    CONCATENATE '/usr/sap/S42/data/' s_filename into ld_filename.
    
    IF gd_Verarbeitung = 1.
    
    ld_datei = gt_mytab.
    
    OPEN DATASET ld_filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
    
    LOOP AT ld_datei into ld_Zeile.
    
    Data(ld_string) = | Partnernummer: { ld_Zeile-customid } Betrag: { ld_Zeile-forcuram } Waehrung: { ld_Zeile-forcurkey } Name: { ld_Zeile-passname }|.
    
    Transfer ld_string to ld_filename.
    
    ENDLOOP.
    
    Close dataset ld_filename.
    
    ElseIF gd_Verarbeitung = 2.
    
    ld_datei = gt_mytab.
    *ld_filename = '/usr/sap/S42/data/CSVFile_MR.csv'.
    
    Open Dataset ld_filename for output in text mode encoding default with SMART LINEFEED.
    
    LOOP AT ld_datei into ld_Zeile.
    
    ld_string = | Partnernummer: { ld_Zeile-customid } Betrag: { ld_Zeile-forcuram } Waehrung: { ld_Zeile-forcurkey } Name: { ld_Zeile-passname }|.
    
    Transfer ld_string to ld_filename.
    
    ENDLOOP.
    
    Close dataset ld_filename.
    
    ENDIF.
    ENDFUNCTION.
    

    And in my program I call it like this:

    if p_txt = 'X'.
    
    ld_txtcsv = 1.
    
        CALL FUNCTION 'EXPORT_TXT_CSV_MR'
            EXPORTING
                s_filename          = 'TXTFile_MR.txt'
            CHANGING
                gt_mytab            = lt_summe
                gd_verarbeitung     = ld_txtcsv.
    
    clear ld_txtcsv.
    
    endif.
    
    if p_csv = 'X'.
    
    ld_txtcsv = 2.
    
     CALL FUNCTION 'EXPORT_TXT_CSV_MR'
            EXPORTING
                s_filename          = 'CSVFile_MR.csv'
            CHANGING
                gt_mytab            = lt_summe
                gd_verarbeitung     = ld_txtcsv.
    
    clear ld_txtcsv.
    
    ENDIF.
    

    Thanks for all of your help! I got it now! :)