Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Function module to read Comma Seperated File (CSV)

Former Member
0 Likes
6,303

Hi,

Is there any Function module to read CSV file from presentation server or Application server?

Regards,

Madhu

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,904

Hi madhukeshwar,

1. Exactly for this purpose,

i have developed an independent FORM

where we give inputs

a) file name (eg. abcd.txt)

b) separator (eg , COMMA in your case)

c) internal table (eg. t001)

2. It will provide the data

in proper format

(no matter what the separator)

(it can work with any kind of separator)

3. just copy paste in new program.

REPORT abc.

*----


*----


change your table declaration and file name

*----


DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.

PERFORM myupload TABLES t001 USING 'd:\t001.txt' ','.

BREAK-POINT.

*----


in debug see t001

*----


  • INDEPENDENT FORM

*----


FORM myupload TABLES orgtab

USING filename separator.

*----


Data

DATA : BEGIN OF itab OCCURS 0,

myline(1000) TYPE c,

END OF itab.

DATA : extension(5) TYPE c.

DATA : name(100) TYPE c.

DATA : newfilename TYPE string.

*----


Step 1

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = filename

TABLES

data_tab = itab.

*----


Step 2

LOOP AT itab.

REPLACE ALL OCCURRENCES OF separator IN itab-myline WITH

cl_abap_char_utilities=>horizontal_tab.

MODIFY itab.

ENDLOOP.

*----


Step 3

DATA : path LIKE pcfile-path.

path = filename.

CALL FUNCTION 'PC_SPLIT_COMPLETE_FILENAME'

EXPORTING

complete_filename = path

  • CHECK_DOS_FORMAT =

IMPORTING

  • DRIVE =

extension = extension

name = name

  • NAME_WITH_EXT =

  • PATH =

EXCEPTIONS

invalid_drive = 1

invalid_extension = 2

invalid_name = 3

invalid_path = 4

OTHERS = 5

.

*----


Step 4

newfilename = filename.

REPLACE name IN newfilename WITH 'temp'.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

  • BIN_FILESIZE =

filename = newfilename

TABLES

data_tab = itab

  • FIELDNAMES =

.

*----


Step 5

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = newfilename

has_field_separator = 'X'

TABLES

data_tab = orgtab.

ENDFORM. "myupload

3.

regards,

amit m.

10 REPLIES 10
Read only

Former Member
0 Likes
1,904

TEXT_CONVERT_CSV_TO_SAP

Read only

0 Likes
1,904

Thanks.

My requirement is->

Read file (Presentation server) which is in CSV format.

put it into itab.

How can I use this FM?

Regards,

Madhu

Read only

0 Likes
1,904

hi Madhukar,

Use FM <b>GUI_UPLOAD</b> with filetype as 'CSV' this should work...

filetype = 'CSV'.

Regards,

Santosh

Read only

Former Member
0 Likes
1,905

Hi madhukeshwar,

1. Exactly for this purpose,

i have developed an independent FORM

where we give inputs

a) file name (eg. abcd.txt)

b) separator (eg , COMMA in your case)

c) internal table (eg. t001)

2. It will provide the data

in proper format

(no matter what the separator)

(it can work with any kind of separator)

3. just copy paste in new program.

REPORT abc.

*----


*----


change your table declaration and file name

*----


DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.

PERFORM myupload TABLES t001 USING 'd:\t001.txt' ','.

BREAK-POINT.

*----


in debug see t001

*----


  • INDEPENDENT FORM

*----


FORM myupload TABLES orgtab

USING filename separator.

*----


Data

DATA : BEGIN OF itab OCCURS 0,

myline(1000) TYPE c,

END OF itab.

DATA : extension(5) TYPE c.

DATA : name(100) TYPE c.

DATA : newfilename TYPE string.

*----


Step 1

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = filename

TABLES

data_tab = itab.

*----


Step 2

LOOP AT itab.

REPLACE ALL OCCURRENCES OF separator IN itab-myline WITH

cl_abap_char_utilities=>horizontal_tab.

MODIFY itab.

ENDLOOP.

*----


Step 3

DATA : path LIKE pcfile-path.

path = filename.

CALL FUNCTION 'PC_SPLIT_COMPLETE_FILENAME'

EXPORTING

complete_filename = path

  • CHECK_DOS_FORMAT =

IMPORTING

  • DRIVE =

extension = extension

name = name

  • NAME_WITH_EXT =

  • PATH =

EXCEPTIONS

invalid_drive = 1

invalid_extension = 2

invalid_name = 3

invalid_path = 4

OTHERS = 5

.

*----


Step 4

newfilename = filename.

REPLACE name IN newfilename WITH 'temp'.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

  • BIN_FILESIZE =

filename = newfilename

TABLES

data_tab = itab

  • FIELDNAMES =

.

*----


Step 5

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = newfilename

has_field_separator = 'X'

TABLES

data_tab = orgtab.

ENDFORM. "myupload

3.

regards,

amit m.

Read only

0 Likes
1,904

If its a one time activity you can use the transactions CG3Z transactions.

Read only

Former Member
0 Likes
1,904

Hi Madhu,

Use this function module

(Used to split the filename and the extension)

CV120_SPLIT_FILE

Please reward points if you find this as a right solution

Regards,

Harini

Read only

Former Member
0 Likes
1,904

Please use the following logic.

This should upload the file from presentation server

TYPE-POOLS: TRUXS.

wa_rawdata type TRUXS_T_TEXT_DATA.

filename type RLGRAP-FILENAME

itab type (file structure )

CALL FUNCTION 'TEXT_CONVERT_CSV_TO_SAP'

EXPORTING

I_FIELD_SEPERATOR = ';'

i_tab_raw_data = wa_rawdata

I_FILENAME = filename

tables

i_tab_converted_data = itab

  • EXCEPTIONS

  • CONVERSION_FAILED = 1

  • OTHERS = 2

Read only

0 Likes
1,904

Thank you.

Can we read Excel file (xls format) into internal table?

I tried using FM "ALSM_EXCEL_TO_INTERNAL_TABLE". But I need to pass start collumn , end collumn, start row and End row, in this case. I won't be having information about end row. How can I use it? Is there any other FM which I could use?

Regrads,

Madhu

Read only

0 Likes
1,904

Hi For Excel Sheet you can use

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

EXPORTING

i_field_seperator = c_x

i_tab_raw_data = wa_rawdata

i_filename = p_f1

TABLES

i_tab_converted_data = i_excel

EXCEPTIONS

conversion_failed = 1

OTHERS = 2.

You dont have to give start and end column but you do have to define the internal table as in the same format as the sheet.

<b>Note : Please award points for all helpful answers</b>

Close the thread if solved

Read only

Former Member
0 Likes
1,904

Use the FM 'KCD_CSV_FILE_TO_INTERN_CONVERT'

Regards,

Srikanth