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: 

Upload a .CSV File into an Internal table

Former Member
0 Kudos

Hi,

What are the parameters to be filled into the Function Modules "GUI_UPLOAD" and "ALSM_EXCEL_TO_INTERNAL_TABLE" to Upload a .CSV File into an internal table.

Please send a sample code to support this....

Regards,

Aadhi.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Just create a internal table with one field let say of length 512 and use GUI_UPLOAD to upload the data. Then you can use SPLIT to split the data .

Regards,

Atish

4 REPLIES 4

Former Member
0 Kudos

Hi,

Just create a internal table with one field let say of length 512 and use GUI_UPLOAD to upload the data. Then you can use SPLIT to split the data .

Regards,

Atish

former_member200338
Active Contributor

Hi,

Check the below code.


TYPE-POOLS: truxs.
TYPES:
  BEGIN OF ty_line,
    vbeln LIKE vbap-vbeln,
    posnr LIKE vbap-posnr,
  END OF ty_line.
*data:  ty_Lines TYPE STANDARD TABLE of ty_Line WITH DEFAULT KEY.
DATA: itab   TYPE STANDARD TABLE OF ty_line WITH DEFAULT KEY.
DATA: itab1  TYPE truxs_t_text_data.

SELECT
  vbeln
  posnr
  UP TO 10 ROWS
  FROM vbap
  INTO TABLE itab.

CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
  EXPORTING
    i_field_seperator    = ';'
  TABLES
    i_tab_sap_data       = itab
  CHANGING
    i_tab_converted_data = itab1
  EXCEPTIONS
    conversion_failed    = 1
    OTHERS               = 2.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    filename = 'd:\TEMP\test1.txt'
  TABLES
    data_tab = itab1
  EXCEPTIONS
    OTHERS   = 1.

IF sy-subrc eq 0.
  WRITE: 'Data downloaded successfully'.
ENDIF.



DATA: BEGIN OF IEXCEL OCCURS 0.
      INCLUDE STRUCTURE ALSMEX_TABLINE.
DATA: END OF IEXCEL.

PARAMETERS: FILENM   LIKE rlgrap-filename MEMORY ID M01,
            NOHEADER AS CHECKBOX.

call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
  exporting
    filename                      = FILENM
    i_begin_col                   = 1
    i_begin_row                  = 1
    i_end_col                     = 100
    i_end_row                     = 30000
  tables
    intern                        = IEXCEL
  EXCEPTIONS
    INCONSISTENT_PARAMETERS       = 1
    UPLOAD_OLE                    = 2
    OTHERS                        = 3.

if sy-subrc <> 0.
   WRITE: / 'EXCEL UPLOAD FAILED ', FILENM, SY-SUBRC.
endif.

Former Member
0 Kudos

Hi,

ALSM_EXCEL_TO_INTERNAL_TABLE FM will used to move the excel data to internal table from Presentation server. Not for .csv files.

CALL FUNCTION 'UPLOAD'

EXPORTING

FILENAME = 'D:\file.csv'

FILETYPE = 'DAT'

HAS_FIELD_SEPARATOR = ','

TABLES

DATA_TAB = I_FILE1

EXCEPTIONS

CONVERSION_ERROR = 1

FILE_OPEN_ERROR = 2

FILE_READ_ERROR = 3

INVALID_TABLE_WIDTH = 4

INVALID_TYPE = 5

NO_BATCH = 6

UNKNOWN_ERROR = 7

OTHERS = 8.

IF SY-SUBRC NE 0.

CASE SY-SUBRC.

WHEN 1.

WRITE: / 'Conversion error. Data not read ....'.

WHEN 2.

WRITE: / 'file_open_error. Data not read ....'.

WHEN 3.

WRITE: / 'file_read_error. Data not read ....'.

WHEN 4.

WRITE: / 'invalid_table_width. Data not read ....'.

WHEN 5.

WRITE: / 'invalid_type. data not read ....'.

WHEN 6.

WRITE: / ' no_batch '.

WHEN 7.

WRITE: / 'unknown_error'.

WHEN OTHERS.

WRITE: / 'Error reading data. read data may not be good ...'

Former Member
0 Kudos

Hi,

Use following code to upload the flatfile into internal table.

Have a look on the following code,you can get some idea.

TABLES: kna1.

DATA: BEGIN OF itab1 OCCURS 0,

str(255),

END OF itab1.

DATA: itab2 TYPE kna1 OCCURS 0 WITH HEADER LINE.

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

filename = 'D:\ABAP EVE\ffile1.txt'

filetype = 'ASC'

TABLES

data_tab = itab1

EXCEPTIONS

conversion_error = 1

file_open_error = 2

file_read_error = 3

invalid_type = 4

no_batch = 5

unknown_error = 6

invalid_table_width = 7

gui_refuse_filetransfer = 8

customer_error = 9

no_authority = 10

OTHERS = 11.

IF sy-subrc <> 0.

WRITE:/ 'sorry'.

ELSE.

LOOP AT itab1.

SPLIT itab1-str AT ',' INTO itab2-kunnr itab2-name1.

APPEND itab2.

ENDLOOP.

IF sy-subrc = 0.

LOOP AT itab2.

WRITE:/ itab2-kunnr,itab2-name1.

INSERT INTO kna1 VALUES itab2.

ENDLOOP.

IF sy-subrc = 0.

WRITE:/ 'inserted'.

ELSE.

WRITE:/ 'not inserted'.

ENDIF.

ELSE.

WRITE:/ 'fail'.

ENDIF.

ENDIF.

Reward,if it is useful.

Thanks,

Chandu.