-1

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.

Marrome
  • 1
  • 1
  • 4
  • Yes, the task is to build my own function using SE37 which creates the txt or csv file. I already tried using OPEN DATASET and TRANSFER but I can't seem to get it right. Shouldn't it be possible to import the table into the function, and do the TRANSFER inside the functions source code ? – Marrome Mar 29 '22 at 13:37
  • The code you posted looks fine to me at first glance. Can you tell us how it fails to fulfill your requirements? Don't you get an output file? Doesn't the content look the way you expected? – Philipp Mar 29 '22 at 14:34
  • Well theres no output file. No error message either. The table is filled (I can see that in the GUI) but the txt file doesn't appear. – Marrome Mar 29 '22 at 15:28
  • You might want to check `sy-subrc` after `OPEN DATASET` and `TRANSFER` to ensure that they are actually successful. – Philipp Mar 29 '22 at 16:02
  • I'm not sure if that comes from implementing sy-subrc but now when I run the program I get the Runtime error CALL_FUNCTION_PARM_UNKNOWN and it says that I sent the parameter LV_MYTAB but it wasn't defined in the function. – Marrome Mar 29 '22 at 18:38

2 Answers2

0

Something like this:

OPEN DATASET lv_p_app FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

LOOP AT gt_error INTO gs_final.

TRANSFER gs_final TO lv_p_app.

ENDLOOP.

CLOSE DATASET lv_p_app.

ENDIF.

More samples here https://answers.sap.com/questions/3704234/download-internal-table-onto-the-application-serve.html

alezhu
  • 475
  • 3
  • 6
  • I tried closely exactly that. But somehow the file just doesn't appear to be created. Here's what I tried: 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: `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.` I also tried it without the loop, didn't work either. What am I doing wrong? – Marrome Mar 29 '22 at 14:18
  • 2
    what `sy-subrc` do you get after `OPEN DATASET`? you may not have WRITE permissions for that folder – Suncatcher Mar 29 '22 at 15:19
  • See my comment above. – Marrome Mar 29 '22 at 18:39
0

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! :)

Marrome
  • 1
  • 1
  • 4
  • Can you point out what exactly your mistake was? – Philipp Mar 30 '22 at 15:05
  • I think the main thing was that I put the itab as an IMPORT Parameter instead of a CHANGING Parameter. Also I didn't do the LOOP that you see in this solution. So in general my solution idea was good but not executed right. – Marrome Mar 30 '22 at 20:00