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

Reading a CSV file

Former Member
0 Likes
1,009

hi guys,

can some one help how to read a CSV file especially reading columns in CSV file.

If possible please help me by sending some sample code.

thank you and waiting for your reply

Pavan

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
862

Hi Pavan,

One method is to use the fm GUI_UPLOAD to upload the data along with the commas and then use split statement to get the actual columns.

data: begin of it_data occurs 0,

data(1000),

end of it_data.

data: begin of itab occurs 0,

field1....

field2.......

field3........

end of itab.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = l_filename

TABLES

data_tab = it_data

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

OTHERS = 17.

loop at it_data.

split it_data-data at ',' into itab-field1

itab-field2

itab-field3

.

.

.

append itab.

clear itab.

endloop.

Regards,

Ravi

6 REPLIES 6
Read only

Former Member
0 Likes
863

Hi Pavan,

One method is to use the fm GUI_UPLOAD to upload the data along with the commas and then use split statement to get the actual columns.

data: begin of it_data occurs 0,

data(1000),

end of it_data.

data: begin of itab occurs 0,

field1....

field2.......

field3........

end of itab.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = l_filename

TABLES

data_tab = it_data

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

OTHERS = 17.

loop at it_data.

split it_data-data at ',' into itab-field1

itab-field2

itab-field3

.

.

.

append itab.

clear itab.

endloop.

Regards,

Ravi

Read only

Former Member
0 Likes
862
type-pools: kcde.
parameters: p_file like rlgrap-filename default 'C:STOCK.csv'.
data: it_data type kcde_intern ,
       wa_data type line of kcde_intern.
data : begin of it_data1 occurs 0,
          field1(10),
          field2(10),
          field3(10),
          field4(10),
       end of it_data1.

call function 'KCD_CSV_FILE_TO_INTERN_CONVERT'
exporting
i_filename = p_file
i_separator = ','
tables
e_intern = it_data
exceptions
upload_csv = 1
upload_filetype = 2
others = 3
.
if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.

loop at it_data into wa_data.

  case wa_data-col.
    when '0001'.
      it_data1-field1 = wa_data-value.
    when '0002'.
      it_data1-field2 = wa_data-value.
    when '0003'.
     it_data1-field3 = wa_data-value.
    when '0004'.
     it_data1-field4 = wa_data-value.
  endcase.
at end of row.
  append it_data1  .
  clear it_data1.
endat.
endloop.

loop at it_data1.
  write :/ it_data1-field1, it_data1-field2,it_data1-field3,it_data1-field4.
endloop.
Read only

Former Member
0 Likes
862

Hi

If the file is CSV file then declare a internal table like this:

DATA: begin of itab occurs 0,

field(255) TYPE c,

end if itab.

Upload the data into internal table using GUI_UPLOAD if file is on Presentation server or Read it using Open Dataset. And then loop the table & split the data into other work area & append it to ohter table in which you have specified particular fields.

E.g.:

DATA: v_string type string.

LOOP AT it_itab INTO v_string.

SPLIT v_string

AT ','

INTO x_struct-v2 x_struct-v3 x_struct-v4

x_struct-v5 x_struct-v6 x_struct-v7

x_struct-v8 x_struct-v9 x_struct-v10

x_struct-v11 x_struct-v12 x_struct-v13

x_struct-v14 x_struct-v15.

Append x_struct to it_struct.

Reward points if helpful answer.

Ashvender

Read only

Former Member
0 Likes
862

Here is a sample code:

call function 'GUI_UPLOAD'

exporting

filename = chafile

filetype = 'ASC'

has_field_separator = 'X'

tables

data_tab = it_data

exceptions

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

others = 17.

Read only

Former Member
0 Likes
862

HI,

Write the code as normal as a upload program do for a text file, calli the GUI_UPLOAD function module like below.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

FILENAME = 'C:\test.csv'

FILETYPE = 'ASC'

TABLES

DATA_TAB = itab

EXCEPTIONS

FILE_OPEN_ERROR = 1

FILE_READ_ERROR = 2

NO_BATCH = 3

GUI_REFUSE_FILETRANSFER = 4

INVALID_TYPE = 5

NO_AUTHORITY = 6

UNKNOWN_ERROR = 7

BAD_DATA_FORMAT = 8

HEADER_NOT_ALLOWED = 9

SEPARATOR_NOT_ALLOWED = 10

HEADER_TOO_LONG = 11

UNKNOWN_DP_ERROR = 12

ACCESS_DENIED = 13

DP_OUT_OF_MEMORY = 14

DISK_FULL = 15

DP_TIMEOUT = 16

OTHERS = 17.

Regards

Sudheer

Read only

Former Member
0 Likes
862

thanks very much chandrasekhar, but i still got some doubts regarding this -

what is the significance of funcion module

call function 'KCD_CSV_FILE_TO_INTERN_CONVERT'.

and i tried using gui_upload but problem is i am using split command since values in CSV are separated by comma but since numeric fields are this way 42,000 its splitting 42 and 000 and putting them in separate fields.

but i think this funcion module will resolve my problem can you please give bit more detail on this.

thank you very much and waiting for your reply.

pavan