Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Excel Upload to Internal Table

Hi all,

I want to upload data which is in EXCEL file to an internal table.(BSP)

I have already read the blogs/theards for this topic. Question I have is, Is it possible to Upload an EXCEL file but not CSV to an Internal Table.

I have already implemented the below code for uploading an CSV file to Internal Table:

METHOD do_handle_event .

DATA: conv TYPE REF TO cl_abap_conv_in_ce,

fileupload TYPE REF TO cl_htmlb_fileupload.

DATA: xcontent TYPE xstring,

length TYPE i,

name TYPE string,

type TYPE string,

content TYPE string,

s_table TYPE string_table.

DATA: default_view TYPE REF TO if_bsp_page.

IF NOT htmlb_event IS INITIAL.

CASE htmlb_event->server_event.

WHEN 'Upload'.

fileupload ?= cl_htmlb_manager=>get_data( request = request id = 'myUpload' name = 'fileUpload' ).

IF ( fileupload->file_name NE '' ) AND ( fileupload->file_length GT 0 ).

name = fileupload->file_name.

length = fileupload->file_length.

type = fileupload->file_content_type.

xcontent = fileupload->file_content.

conv = cl_abap_conv_in_ce=>create( input = xcontent ignore_cerr = 'X').

conv->read( IMPORTING data = content len = length ).

SPLIT content AT cl_abap_char_utilities=>cr_lf INTO TABLE s_table.

MOVE name TO filename.

IF NOT s_table IS INITIAL.

process_data( s_table ).

ENDIF.

ENDIF.

ENDCASE.

ENDIF.

ENDMETHOD.

Ariel Ferreiro has helped me with the below code:

*-- VARIABLES: Events

DATA: event TYPE REF TO cl_htmlb_event,

button_event TYPE REF TO cl_htmlb_event_button.

event ?= cl_htmlb_manager=>get_event( runtime->server->request ).

  • Button?

IF event->name = 'button' AND event->event_type = 'click'.

button_event ?= event.

IF button_event->id = 'btnUpload'.

  • -- get the file HTMLB

fileUpload ?= CL_HTMLB_MANAGER=>GET_DATA(

request = request

id = 'txtFile'

name = 'fileUpload' ).

  • Check if the user selected something

IF ( fileUpload->file_name NE '' ) AND

( fileUpload->file_length GT 0 ).

  • Check if it is an excel file.

IF fileUpload->file_content_type EQ 'application/vnd.ms-excel'.

DATA:

input_string TYPE STRING,

fields TYPE string_table,

s_table TYPE string_table,

conv TYPE REF TO CL_ABAP_CONV_IN_CE,

len TYPE I,

iserror TYPE I,

tmp TYPE C.

  • :: READ THE COnteNT OF XSTRING

conv = CL_ABAP_CONV_IN_CE=>CREATE(

input = fileUpload->file_content ).

  • :: READ AND CONVERT

conv->READ(

importing

data = input_string

len = len ).

split input_string at cl_abap_char_utilities=>cr_lf

into table s_table.

field-symbols: <wa_table> like line of s_table.

LOOP AT s_table assigning <wa_table>.

  • :: SKIP THE HEADER OF THE EXCEL (if it has one title header)

IF SY-TABIX NE 1.

split <wa_table> at cl_abap_char_utilities=>HORIZONTAL_TAB

into table fields.

  • :: NOW PARSE THE FIELDS AND WORK WITH THEM (its all yours!

Former Member

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question