Dear SCN,
My name is
greltel15
and I decided to write a blog about handling of TSV and CSV files. First we will see what TSV and CSV files really are and then we will develop code methods to convert these files. I would like to thank my colleague and dear friend
Dimitris Valouxis
for his support and his contribution to the current article. Without him this blog would not exist.
In this blog, we will share code snippets to handle CSV and TSV files. It's a daily task to convert these files to Internal Tables (and vice versa) and it comes very handy and time saving when we have a ready-to-go code available.
If you don't know what CSV file is you can have a look in the following
link
. In a nutshell, CSV is a simple text file in which data and information are separated using a separator (in most cases the separator is a comma). There are two main points to keep in mind. Separator is used to separate two fields (values) while delimiter is used to set the limits. Often double quote is used as a delimiter.
A brief explanation about TSV files follows. So, tab delimited format stores information
from a database or spreadsheet in the format of a tabular structure.
Each entry takes one line in the text file and the various fields are separated by tabs. It is widely used, as data between different systems can be easily transferred via this format. In order to create a TSV.txt file, you can simply save an excel worksheet as text tab delimited. Follow
this
link for more information.
We use to code in methods of global class in order to be available anytime we need to
use them. So all the codes provided are in form of method but it will be an easy task if you want to
convert them to a local report or function module. Check the importing and exporting parameters to
understand the types used. We tried to keep codes as generic as possible in order to be sure that they will work in every case and that's why we pass 'ANY TABLE' as a table type.
1)CSV to Internal Table
Lets start with the conversion of CSV file to Internal Table. Using cl_rsda_csv_converted to separate the comma value to columns our work is so much easier. First, we need to create an instance of the class and pass the parameters of separator and delimiter to 'CREATE' method. Then, simply upload the CSV file to raw data and then separate the line values into columns by looping into the table and using our class.
...
2)Internal Table to CSV
Download Internal Table as CSV to Presentation Server. First, we have to create the target table based on the value of the main table. Loop at the main table and for each row, separate the values using commas and then append lines to target string table. Finally, download the table using GUI_DOWNLOAD. It is exact the opposite procedure of uploading CSV file.
...
3)Tab Delimited to Internal Table
This one is simple. Just use GUI_UPLOAD with filetype 'ASC' and the table will be ready. With 'ASC' the table is transferred as text. The conversion exits are also carried out.
...
4)Internal Table to Tab Delimited
Now for Tab Delimited Τable download we use the exact same process with CSV, but instead of comma we use the horizontal tab in order to split the fields into columns. Here is the tricky part.
You must NOT use # symbol hardcoded but instead you must use the attribute cl_abap_char_utilities=>horizontal_tab. In short, there is a difference in hexadecimal value between # as a normal character and # as a tab character and so ABAP treats them differently in each case. You can read an awesome article
here
which clarifies the case and explains why you should be very cautious when dealing with the horizontal tab. We strongly suggest you to take a look.
Finally, after forming the table, we use GUI_DOWNLOAD with filetype 'DAT' to save our file. DAT is used for Column-by-column transfer. With this format, the data is transferred as with ASC text. However, no conversion exists are carried out and the columns are separated by tab characters
...
Wrap up
So, here is all the