Application Development 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: 

How to dynamically get flat file csv structure?

Former Member
0 Kudos

Hi Expert,

I need to build a program to upload csv file data using Function Module 'GUI_UPLOAD'.

The issue is I have alot of csv file with different structure and different fields.

My question is how to develop a program that can dynamically get the structure (field name, length, data types) of the csv files in order to pass the data into internal table and hence write into transparent table?

Thank you.

Regards,

Honda

11 REPLIES 11

ChristianGünter
Contributor
0 Kudos

Hi Kevin,

Did you check FM TEXT_CONVERT_CSV_TO_SAP?

Regards

0 Kudos

Hi Christian,

I am only able to look for FM TEXT_CONVERT_XLS_TO_SAP.

Do you have any example on how the TEXT_CONVERT_CSV_TO_SAP working?

Thanks.

0 Kudos

Hi Kelvin,

use this Link http://scn.sap.com/thread/1463777

I think the post of Venkat O should lead you on the right way.

FM TEXT_CONVERT_CSV_TO_SAP is just a wrapper for FM TEXT_CONVERT_TEX_TO_SAP, do you have access to this FM?

Regards, Chris

0 Kudos

Hi Chris,

I do not have the access to FM TEXT_CONVERT_TEX_TO_SAP,  TEXT_CONVERT_XLS_TO_SAP and TEXT_CONVERT_CSV_TO_SAP.

Thanks.

former_member189779
Active Contributor
0 Kudos

Hi,

If you know the table you want update, then check follwing link to create dynamic table.

You can upload the data with GUI_UPLOAD, Now in intrenal table you will get ; separated records. Now you want to fill these records to repective colums of your table

Refer.

http://scn.sap.com/thread/1845232

0 Kudos

Hi Vinit,

What i want to do is about data conversion from legacy system to sap system.

I might have different csv files with different structure.

So, I may want to build a program that can get different csv file structure during runtime and based on the structure itself create internal table to store the csv file data.

I can upload csv file using FM GUI_UPLOAD, but issue now is on how to get dynamic csv files structure to create dynamic internal table.

Thanks.

Regards,

Honda

0 Kudos

Hi Kelvin,

does your CSV file has header line. If yes then based on the header line create dynamic internal table  and read file data into that internal table.

Regards,

Vikash Singh

Phillip_Morgan
Contributor
0 Kudos

Hi Kelvin,

What I understand is that you want a program that receives a file in input, analyzes its contents, and determines the appropriate structure to use for the internal table that will accept the data.

This sounds a little complicated but not impossible.

Is that correct?

P.S. One way to simplify things would be that user selects file (open dialog) and structure (from a list). Then you can work on a generalized routine to transfer from file to internal table.

0 Kudos

Hi Phillip,

Do you found any example of how that working?

Thanks.

0 Kudos

Unfortunately, It is more of an idea - It is how I would do it.

But here is a bit of code of something very close (and maybe could work as is). This puts a line of data separated by a separator in a structure. You need to then append it to a table.

*The call

    PERFORM DATA_CSV_TO_STRUCTURE USING ';'     "the separator

                                        DATA_IN

                               CHANGING RECORD.

* The form

FORM DATA_CSV_TO_STRUCTURE USING SEPARATOR

                                 CSV_DATA        STRUCTURE DATA_IN

                        CHANGING STRUCTURE_DATA.

  FIELD-SYMBOLS: <REC>.

  DATA:

       BEGIN OF RECORD_SPLIT OCCURS 0,

         VALUE(20),

       END OF RECORD_SPLIT.

  CLEAR STRUCTURE_DATA.

  SPLIT CSV_DATA-RECORD AT SEPARATOR INTO TABLE RECORD_SPLIT .

  DO.

    ASSIGN COMPONENT SY-INDEX OF STRUCTURE STRUCTURE_DATA TO <REC>.

    IF SY-SUBRC NE 0.

      EXIT.

    ENDIF.

    READ TABLE RECORD_SPLIT INDEX SY-INDEX.

    <REC> = RECORD_SPLIT-VALUE.

  ENDDO.

ENDFORM.                               " DATA_CSV_TO_STRUCTURE

Former Member
0 Kudos

Hi Kelvin,

for CSV structure.

CALL FUNCTION 'GUI_UPLOAD'
   
EXPORTING
      filename                = lv_file(flatfile)
      filetype                =
'DAT'
      has_field_separator     = c_x(X)
   
TABLES
      data_tab                = li_tab(internal table contain a filed(str)
   
EXCEPTIONS
      file_open_error         =
1

if  your flat file contains coloumn header

loop at li_tab.

if sy-tabix = 1.

continue.

split into columns.....

fill work area

append to internal table.

endloop.

hope this will help you.

Regards,

Chaitanya