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 file using a .txt or .xls

Former Member
0 Kudos

Hello all,

Can anyone tell me how to upload a file which can be either a txt file or an excel file using gui_upload function module .

Currently I am using

CALL METHOD r_obj->gui_upload

EXPORTING

filename = filename

filetype = 'ASC'

has_field_separator = 'X'

IGNORE_CERR = ABAP_TRUE

CHANGING

data_tab = t_filedata

This is working fine for uploading a txt file but not for excel file.

8 REPLIES 8

I355602
Advisor
Advisor
0 Kudos

Hi,

Search on SCN, you will get many threads.

Refer:- FM ALSM_EXCEL_TO_INTERNAL_TABLE

Regards,

Tarun

0 Kudos

Hello Guys,

Just a word of caution: ALSM_EXCEL_TO_INTERNAL_TABLE is no longer available in the recent ABAP release.

May be save the excel as CSV or tab-delimited text & proceed with GUI_UPLOAD. You can also refer to this [WIKI |http://wiki.sdn.sap.com/wiki/display/Snippets/Read%20multiple%20sheets%20of%20an%20Excel%20file%20into%20SAP%20through%20ABAP]for details.

Cheers,

Suhas

0 Kudos

for the Excel you can use this code


DATA : lt_intern TYPE kcde_cells OCCURS 0 WITH HEADER LINE.
  DATA : ld_index TYPE i.
  FIELD-SYMBOLS: <fs>.
  CALL FUNCTION 'KCD_EXCEL_OLE_TO_INT_CONVERT'
    EXPORTING
      filename                = p_file
      i_begin_col             = 1
      i_begin_row             = 1
      i_end_col               = 256
      i_end_row               = 65000
    TABLES
      intern                  = lt_intern
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.
  LOOP AT lt_intern.
* P_HEAD is a check box in input that identify if the file has header line. 
*So you can skip the first line if is checked.
    IF p_head EQ 'X' AND lt_intern-row EQ 1. 
      CONTINUE.
    ENDIF.
    MOVE lt_intern-col TO ld_index.
    ASSIGN COMPONENT ld_index OF STRUCTURE
    tb_file TO <fs>.
    MOVE : lt_intern-value TO <fs>.
* TB_FILE is your internal table that stores information from file.
    AT END OF row.
      APPEND tb_file. 
      CLEAR tb_file.
    ENDAT.
  ENDLOOP.

Former Member
0 Kudos

Hi Sinthu,

Do as below:-

CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = 'C:\temp\text.xls'
filetype = 'ASC'
has_field_separator = 'X'
CHANGING
data_tab = itab
IF sy-subrc = 0.
WRITE:/ 'success'.
ENDIF.
.

CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = 'C:\temp\text.txt'
filetype = 'ASC'
has_field_separator = 'X'
CHANGING
data_tab = itab
IF sy-subrc = 0.
WRITE:/ 'success'.
ENDIF.
.

Regards

Abhii

Former Member
0 Kudos

Hi Dinesh,

You can do it directly by using the Function Module 'GUI_UPLOAD'. It will work both for text and excel.

example:

for Excel file.

DATA: v_file TYPE string VALUE 'C:\Documents and Settings\Desktop\Tps.xls'.

for text file.

DATA: v_file TYPE string VALUE 'C:\Documents and Settings\Desktop\Tps.txt'.

and then the below code.

DATA: BEGIN OF destfile OCCURS 0,
      text(250) TYPE c,
      END OF destfile.

CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename                      = v_file
   filetype                      = 'ASC'
*   HAS_FIELD_SEPARATOR           = ' '
*   HEADER_LENGTH                 = 0
*   READ_BY_LINE                  = 'X'
*   DAT_MODE                      = ' '
*   CODEPAGE                      = ' '
*   IGNORE_CERR                   = ABAP_TRUE
*   REPLACEMENT                   = '#'
*   CHECK_BOM                     = ' '
*   VIRUS_SCAN_PROFILE            =
*   NO_AUTH_CHECK                 = ' '
* IMPORTING
*   FILELENGTH                    =
*   HEADER                        =
  TABLES
    data_tab                      = destfile.

Try using this way. It should work.

Regards,

Bhanu.

Former Member
0 Kudos

Hi dinesh,

try this FM CALL FUNCTION 'GUI_FILE_LOAD_DIALOG' & get the path

and thwn use FM CALL FUNCTION 'GUI_UPLOAD'

Former Member
0 Kudos

Hi ,

Try the solution given by Abhii but salve the excel file as tab delimited and use that file for upload. It will work.

Former Member
0 Kudos

Hi,

You may use FM TEXT_CONVERT_XLS_TO_SAP