Showing posts with label Data Transfer/Data Conversion. Show all posts
Showing posts with label Data Transfer/Data Conversion. Show all posts

Jul 14, 2026

Upload Internal Table to Application Server (AL11) in CSV format Using Class CL_RSDA_CSV_CONVERTER, Method STRUCTURE_TO_CSV

Here is another way to upload internal table to Application Server into CSV format.  
And upload using statement OPEN DATASET.

  DATA: lt_csv_format TYPE truxs_t_text_data, " Standard table type for text
        lv_filepath   TYPE string,
        lv_csv_line   TYPE string.    

  " .... Assuming GT_CSV already populated.

  " Convert internal table to CSV structure
  DATA: lv_c4096 TYPE c LENGTH 4096.
  DATA(lo_csv) = cl_rsda_csv_converter=>create( ).

  " Covert line by line item
  LOOP AT gt_csv ASSIGNING FIELD-SYMBOL(<ls_csv>).
    CLEAR lv_c4096.
    lo_csv->structure_to_csv( EXPORTING i_s_data = <ls_csv>
                              IMPORTING e_data   = lv_c4096 ).
    APPEND INITIAL LINE TO lt_csv_format ASSIGNING FIELD-SYMBOL(<ls_csv_format>).
    IF sy-subrc EQ 0.
      <ls_csv_format> = lv_c4096.
    ENDIF.
  ENDLOOP.

  IF sy-subrc = 0.

    " Write to Application Server
    OPEN DATASET lv_filepath FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

    IF sy-subrc = 0.
      LOOP AT lt_csv_format INTO lv_csv_line.
        TRANSFER lv_csv_line TO lv_filepath.
      ENDLOOP.
      CLOSE DATASET lv_filepath.
    ENDIF.

  ENDIF.

Jun 17, 2026

Upload Internal Table to AL11 in Sap Abap

To upload internal table data to the SAP Application Server (Transaction AL11), you must use standard ABAP Dataset statements: OPEN DATASET, TRANSFER, and CLOSE DATASET. 

Below is a direct answer and complete guide on how to implement this programmatically. 
1. Simple Text Mode Upload
Use TEXT MODE if you want to upload data as a standard text file or CSV. 
abap
DATA: lv_filepath TYPE string VALUE '/usr/sap/interfaces/test_file.txt',
      ls_data     TYPE ty_your_structure. " Replace with your structure type

" Step 1: Open the application server file path
OPEN DATASET lv_filepath FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc = 0.
  " Step 2: Loop through the internal table and transfer records
  LOOP AT lt_internal_table INTO ls_data.
    TRANSFER ls_data TO lv_filepath.
  --ENDLOOP.

  " Step 3: Close the dataset (Mandatory)
  CLOSE DATASET lv_filepath.
  MESSAGE 'File uploaded successfully to AL11' TYPE 'S'.
ELSE.
  MESSAGE 'Error opening file path on AL11' TYPE 'E'.
ENDIF.
2. Comma-Separated Values (CSV) Upload 
If you want to view the fields properly structured in a spreadsheet layout later, convert the table to a string/CSV format using SAP_CONVERT_TO_CSV_FORMAT before sending it to the server.
abap
DATA: lv_filepath   TYPE string VALUE '/tmp/sales_data.csv',
      lt_csv_format TYPE truxs_t_text_data, " Standard table type for text
      lv_csv_line   TYPE string.

" Step 1: Convert internal table to CSV structure
CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
  EXPORTING
    i_field_seperator    = ','
  TABLES
    i_tab_sap_data       = lt_internal_table
  CHANGING
    i_tab_converted_data = lt_csv_format
  EXCEPTIONS
    conversion_failed    = 1
    OTHERS               = 2.

IF sy-subrc = 0.
  " Step 2: Write to Application Server
  OPEN DATASET lv_filepath FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
  
  IF sy-subrc = 0.
    LOOP AT lt_csv_format INTO lv_csv_line.
      TRANSFER lv_csv_line TO lv_filepath.
    ENDLOOP.
    CLOSE DATASET lv_filepath.
  ENDIF.
ENDIF.
3. Binary Mode Upload (For Excel / Zip / Images)
Use BINARY MODE if your data is an XSTRING or raw binary data stream (e.g., generating an actual .xlsx file using cl_salv_bs_lex). 
abap
DATA: lv_filepath TYPE string VALUE '/usr/sap/interfaces/report.xlsx',
      lv_xstring  TYPE xstring. " Contains your binary file data

OPEN DATASET lv_filepath FOR OUTPUT IN BINARY MODE.
IF sy-subrc = 0.
  TRANSFER lv_xstring TO lv_filepath.
  CLOSE DATASET lv_filepath.
ENDIF.
Key Syntax Reminders
  • FOR OUTPUT: Opens a file for writing. If the file already exists, this completely overwrites its contents.
  • FOR APPENDING: Opens a file and places the cursor at the end of the existing text. Use this if you want to append new rows without losing old logs.
  • ENCODING DEFAULT: Mandatory constraint for text mode file handling in modern Unicode SAP environments. 
If you would like, tell me:
  • What file type do you want to create in AL11 (TXT, CSV, XLSX)?
  • Are you handling large datasets that require specific block sizing?
I can provide the exact target layout adjustments or helper class transformations you need!