Read a File into a Variable in ABAP

DevWorkbench Saturday August 23rd, 2014

Uploading files to the SAP ERP application server and reading data from them is a function that is very often requested by users. Today, you can learn how to implement this very easily using the SAP Control Framework.

Uploading files is quite simple if you know how to do it. Once again (and not for the last time), we encounter the class CL_GUI_FRONTEND_SERVICES. With two simple method calls, it allows us to select a file from the current desktop and upload it to the SAP application server, reading it into a variable.

Let’s consider the setup first. I’ve developed a small demo report that has exactly one parameter: the name of a file, or rather, a path to a file on the current user’s machine. Using this path, I will then read the file into a variable. Let’s look at how to retrieve that path first.

You can see that there is nothing special about the parameter – it’s a simple character variable. The knack is the usage of AT SELECTION-SCREEN ON VALUE-REQUEST. Once the user presses F4, he will be shown a file selection dialog. After he has picked a file, the according path is moved from the class method return value to the parameter so the user gets some feedback.

File selection dialog sent from CL_GUI_FRONTEND_SERVICES

The next step is to actually read the file. This should happen once the report is executed, so it is done at the START-OF-SELECTION event.

Before the file is actually uploaded, the file path needs to be moved into a string variable. After that, we can simply call the GUI_UPLOAD method of class CL_GUI_FRONTEND_SERVICES to read the data into an internal table. You will now have all the data from the file available in the variable gt_line. You could now loop over the table as usual and process the data line by line. Neat!

The class CL_GUI_FRONTEND_SERVICES is part of the SAP Control Framework, a library that allows ABAP developers to interact with desktop applications and use native desktop functionality. I will write a few more posts about this in the next few days to explore some more possibilities. As usual, you can find today’s demo program on the next page.