Skip to content

Instantly share code, notes, and snippets.

@fleischr
Created October 13, 2022 16:38
Show Gist options
  • Save fleischr/d601da0852aa97f87f231888842ec8ba to your computer and use it in GitHub Desktop.
Save fleischr/d601da0852aa97f87f231888842ec8ba to your computer and use it in GitHub Desktop.
report zreadfromappserver.
*originally posted on https://blogs.sap.com/2020/03/19/read-an-excel-file-from-al11-directory/
*Introduction :
*There are scenarios where the user will upload a file in the Application Server which needs to be consumed to manipulate that data (such as updating in a table or sending it as a mail). In that case, we can use the DATASET to read the file in the Application Server.
*Problem Statement:
*The user uploads an Excel File in the Application Server. That Excel file is picked from the list of files in the Application server and stored in an internal table and then the data is uploaded to a custom table in SAP.
*Solution:
*Step 1: Know the directory in which the file is stored.
DATA : lv_dir TYPE eps2filnam.
lv_dir = '/VTS/DEV/'.
*Step 2: Get the list of files in that Directory path using the Function Module EPS2_GET_DIRECTORY_LISTING
DATA it_files TYPE TABLE OF eps2fili.
CALL FUNCTION 'EPS2_GET_DIRECTORY_LISTING'
EXPORTING
iv_dir_name = lv_dir
tables
dir_list = it_files
EXCEPTIONS
INVALID_EPS_SUBDIR = 1
SAPGPARAM_FAILED = 2
BUILD_DIRECTORY_FAILED = 3
NO_AUTHORIZATION = 4
READ_DIRECTORY_FAILED = 5
TOO_MANY_READ_ERRORS = 6
EMPTY_DIRECTORY_LIST = 7
OTHERS = 8.
*Step 3: Find the file in that list using its name or date(or any indicator to identify the file given by the user)
DATA : p_file_n TYPE localfile .
READ TABLE it_files INTO DATA(wa_files) INDEX 1. "reading recent file
IF sy-subrc = 0.
p_file_n = wa_files-name.
ENDIF.
*Step 4: Use OPEN DATASET keyword to open the file. Use READ DATASET to read each record in the file. CLOSE DATASET to close the file. Store each line item in an internal table.
DATA: BEGIN OF it_tab OCCURS 0,
rec(1000) TYPE c,
END OF it_tab.
DATA: wa_tab(1000) TYPE c.
OPEN DATASET p_file_n FOR INPUT IN TEXT MODE ENCODING NON-UNICODE.
IF sy-subrc = 0.
DO.
READ DATASET p_file_n INTO wa_tab.
IF sy-subrc <> 0.
EXIT.
ENDIF.
it_tab-rec = wa_tab.
APPEND it_tab.
ENDDO.
ENDIF.
CLOSE DATASET p_file_n.
*Step 5: Now the excel data is stored in an internal table which can be uploaded into any table of the same structure.
MODIFY ztest FROM it_Tab.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment