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

Function module to pass data from excel file in BDC

Former Member
0 Likes
1,681

Hi guys,

I am using 'TEXT_CONVERT_XLS_TO_SAP' to pass data from excel file. Am using ECC 6.0

My excel file contains 1 header list and multiple line items. For this I think I must use 'ALSM_EXCEL_TO_INTERNAL_TABLE' instead of 'TEXT_CONVERT_XLS_TO_SAP'. Can you please suggest and send me the procedure/code for the above mentioned file to upload data.

Thanks in advance.

Kiran

7 REPLIES 7
Read only

Former Member
0 Likes
1,076

HI,

Check this Code..

DATA l_count TYPE sy-tabix.

   CONSTANTS: lc_begin_col TYPE i VALUE '1',

              lc_begin_row TYPE i VALUE '2',

              lc_end_col   TYPE i VALUE '2',

              lc_end_row   TYPE i VALUE '3000'.

* Begin of CALK912848 - Carlos Werberich - 16Sep08

  CLEAR p_i_excel_data. REFRESH p_i_excel_data.

* End   of CALK912848 - Carlos Werberich - 16Sep08

* Function module to read excel file and convert it into internal table

   CALL FUNCTION 'KCD_EXCEL_OLE_TO_INT_CONVERT'

     EXPORTING

       filename                = p_p_file

       i_begin_col             = lc_begin_col

       i_begin_row             = lc_begin_row

       i_end_col               = lc_end_col

       i_end_row               = lc_end_row

     TABLES

       intern                  = i_data

     EXCEPTIONS

       inconsistent_parameters = 1

       upload_ole              = 2

       OTHERS                  = 3.

* Error in file upload

   IF sy-subrc NE 0 .

     MESSAGE text-006 TYPE 'E'.

     EXIT.

   ENDIF.

   IF i_data[] IS INITIAL .

     MESSAGE text-007 TYPE 'E'.

     EXIT.

   ELSE.

     SORT i_data BY row col .

* Loop to fill data in Internal Table

     LOOP AT i_data .

       MOVE i_data-col TO l_count .

       ASSIGN COMPONENT l_count OF STRUCTURE p_i_excel_data TO  .

       AT END OF row .

* Append data into internal table

         APPEND p_i_excel_data.

         CLEAR p_i_excel_data.

       ENDAT .

     ENDLOOP .

   ENDIF 

Read only

Former Member
0 Likes
1,076

Hi Kiran,

Use it like this

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'

EXPORTING

FILENAME = P_FILE "name of file

I_BEGIN_COL = 1 "Starting column

I_BEGIN_ROW = 2 "Starting row

I_END_COL = 17 "Ending coloumn

I_END_ROW = 3000 "Ending row

TABLES

INTERN = itab "Internal table

EXCEPTIONS

INCONSISTENT_PARAMETERS = 1

UPLOAD_OLE = 2

OTHERS = 3 .

basically what this FM will do is it will read the file starting second row as first name can be the field names. It will take the data from each cell in Excel sheet. then you can use it in programming to put in seprate internal tables for header and Item.

loop at itab into wa.

if wa-col = '0001'.

record-Control = wa-value.

endif.

elseif wa-col = '0002'.

record-COMP_CODE = wa-value.

elseif wa-col = '0003'.

record-DOC_TYPE = wa-value.

elseif wa-col = '0004'.

record-VENDOR = wa-value.

endif.

Like this you can use it.

Regards,

Sachin

Read only

tarangini_katta
Active Contributor
0 Likes
1,076

HI kiran,

Do like this .THis idid for KB31N transaction.

TYPES : BEGIN OF fl_header,

budat(10) TYPE c,

bldat(10) TYPE c,

bltxt TYPE coheader-bltxt,

END OF fl_header.

TYPES : BEGIN OF fl_item,

ekostl TYPE ekostl,

eaufnr TYPE eaufnr,

stagr TYPE stagr,

mbgbtr(10) TYPE c,

sgtxt TYPE sgtxt,

END OF fl_item.

*Selection Screen

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.

PARAMETERS : p_file TYPE rlgrap-filename,

p_kokrs TYPE kokrs DEFAULT 'ZIE1',

p_batch TYPE apqi-groupid.

SELECTION-SCREEN END OF BLOCK b1.

*Data Declarations

DATA : it_intern TYPE TABLE OF alsmex_tabline,

wa_intern TYPE alsmex_tabline,

it_bdcdata TYPE TABLE OF bdcdata,

wa_bdcdata TYPE bdcdata,

wa_header TYPE fl_header,

wa_item TYPE fl_item,

it_item TYPE TABLE OF fl_item,

t_fnam(20) TYPE c,

t_idx TYPE n,

t_file TYPE rlgrap-filename,

t_amt_int(10) TYPE c,

t_amt_int1(10) TYPE c,

t_amt_int2(10) TYPE c.

*F4 Help for the FilePath

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'

IMPORTING

file_name = p_file.

START-OF-SELECTION.

t_file = p_file.

*Upload data from excel sheet to the Internal Table

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'

EXPORTING

filename = t_file

i_begin_col = 1

i_begin_row = 1

i_end_col = 10

i_end_row = 500

TABLES

intern = it_intern

EXCEPTIONS

inconsistent_parameters = 1

upload_ole = 2

OTHERS = 3.

IF sy-subrc = 0.

*Append data internal table to corresponding strctures

LOOP AT it_intern INTO wa_intern.

*Header Data

CASE sy-tabix.

  • CASE wa_intern-row.

WHEN '0002'.

wa_header-budat = wa_intern-value.

WHEN '0004'.

wa_header-bldat = wa_intern-value.

WHEN '0006'.

wa_header-bltxt = wa_intern-value.

ENDCASE.

*Item Data

IF sy-tabix GE '12'.

ON CHANGE OF wa_intern-row.

IF sy-tabix GT '12'.

APPEND wa_item TO it_item.

CLEAR wa_item.

ENDIF.

ENDON.

CASE wa_intern-col.

WHEN '0001'.

wa_item-ekostl = wa_intern-value.

WHEN '0002'.

wa_item-eaufnr = wa_intern-value.

WHEN '0003'.

wa_item-stagr = wa_intern-value.

WHEN '0004'.

wa_item-mbgbtr = wa_intern-value.

WHEN '0005'.

wa_item-sgtxt = wa_intern-value.

ENDCASE.

ENDIF.

AT LAST.

APPEND wa_item TO it_item.

CLEAR wa_item.

ENDAT.

ENDLOOP.

*Open the Session in SM35.

PERFORM open_group.

*BDC Recording

PERFORM bdc_dynpro USING 'SAPLSPO4' '0300'.

PERFORM bdc_field USING 'BDC_CURSOR'

'SVALD-VALUE(01)'.

PERFORM bdc_field USING 'BDC_OKCODE'

'=FURT'.

PERFORM bdc_field USING 'SVALD-VALUE(01)'

p_kokrs.

PERFORM bdc_dynpro USING 'SAPLK23F1' '1200'.

perform bdc_field using 'BDC_CURSOR'

'COHEADER-SEND_REC_REL'.

perform bdc_field using 'BDC_OKCODE'

'=PVAR'.

PERFORM bdc_field USING 'COHEADER-SEND_REC_REL'

'03SAP'.

PERFORM bdc_field USING 'RK23F-STATUS'

'L'.

perform bdc_dynpro using 'SAPLK23F1' '1200'.

perform bdc_field using 'BDC_OKCODE'

'=POST'.

perform bdc_field using 'COHEADER-SEND_REC_REL'

'03SAP'.

perform bdc_field using 'RK23F-STATUS'

'L'.

REPLACE ALL OCCURRENCES OF '/' IN wa_header-budat WITH '.' .

PERFORM bdc_field USING 'COHEADER-BUDAT'

wa_header-budat.

REPLACE ALL OCCURRENCES OF '/' IN wa_header-bldat WITH '.' .

PERFORM bdc_field USING 'COHEADER-BLDAT'

wa_header-bldat.

PERFORM bdc_field USING 'COHEADER-BLTXT'

wa_header-bltxt.

  • MOVE 1 TO t_idx.

t_idx = 1.

LOOP AT it_item INTO wa_item.

CONCATENATE 'EL2(' t_idx ')' INTO t_fnam.

PERFORM bdc_field USING t_fnam

wa_item-ekostl.

CONCATENATE 'EL3(' t_idx ')' INTO t_fnam.

PERFORM bdc_field USING t_fnam

wa_item-eaufnr.

CONCATENATE 'EL4(' t_idx ')' INTO t_fnam.

PERFORM bdc_field USING t_fnam

wa_item-stagr.

CONCATENATE 'EL8(' t_idx ')' INTO t_fnam.

PERFORM bdc_field USING t_fnam

wa_item-sgtxt.

CONCATENATE 'ELR1(' t_idx ')' INTO t_fnam.

SPLIT wa_item-mbgbtr AT '.' INTO t_amt_int t_amt_int1 t_amt_int2.

CONCATENATE t_amt_int t_amt_int1 t_amt_int2 INTO wa_item-mbgbtr.

REPLACE ALL OCCURRENCES OF ',' IN wa_item-mbgbtr WITH '.'.

PERFORM bdc_field USING t_fnam

wa_item-mbgbtr.

*Page break at table control

t_idx = t_idx + 1.

IF t_idx = 9.

PERFORM bdc_field USING 'BDC_CURSOR'

'EL8(08)'.

PERFORM bdc_field USING 'BDC_OKCODE' 'P+'.

PERFORM bdc_dynpro USING 'SAPLK23F1' '1200'.

t_idx = 2.

ENDIF.

  • CLEAR wa_item.

ENDLOOP.

PERFORM bdc_dynpro USING 'SAPLK23F1' '1200'.

PERFORM bdc_field USING 'BDC_OKCODE'

'=POST'.

*insert the data into the Sassion

PERFORM bdc_insert.

*Close the session

PERFORM close_group.

CALL TRANSACTION 'SM35'. "#EC CI_CALLTA

ENDIF.

&----


*& Form OPEN_GROUP

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM open_group .

CALL FUNCTION 'BDC_OPEN_GROUP'

EXPORTING

group = p_batch

keep = 'X'

user = 'XS234MA'

EXCEPTIONS "#EC *

client_invalid = 1

destination_invalid = 2

group_invalid = 3

group_is_locked = 4

holddate_invalid = 5

internal_error = 6

queue_error = 7

running = 8

system_lock_error = 9

user_invalid = 10

OTHERS = 11.

ENDFORM. " OPEN_GROUP

&----


*& Form BDC_INSERT

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM bdc_insert .

CALL FUNCTION 'BDC_INSERT'

EXPORTING

tcode = 'KB31N'

TABLES

dynprotab = it_bdcdata

EXCEPTIONS "#EC *

internal_error = 1

not_open = 2

queue_error = 3

tcode_invalid = 4

printing_invalid = 5

posting_invalid = 6

OTHERS = 7.

ENDFORM. " BDC_INSERT

&----


*& Form CLOSE_GROUP

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM close_group .

CALL FUNCTION 'BDC_CLOSE_GROUP'

EXCEPTIONS

not_open = 1

queue_error = 2

OTHERS = 3.

IF sy-subrc <> 0. "#EC *

ENDIF.

ENDFORM. " CLOSE_GROUP

&----


*& Form BDC_DYNPRO

&----


  • text

----


  • -->P_0269 text

  • -->P_0270 text

----


FORM bdc_dynpro USING value(p_0269) "#EC *

value(p_0270).

CLEAR wa_bdcdata.

wa_bdcdata-program = p_0269.

wa_bdcdata-dynpro = p_0270.

wa_bdcdata-dynbegin = 'X'.

APPEND wa_bdcdata TO it_bdcdata.

ENDFORM. " BDC_DYNPRO

&----


*& Form BDC_FIELD

&----


  • text

----


  • -->P_0274 text

  • -->P_0275 text

----


FORM bdc_field USING value(p_0274) "#EC *

value(p_0275).

CLEAR wa_bdcdata.

wa_bdcdata-fnam = p_0274.

wa_bdcdata-fval = p_0275.

APPEND wa_bdcdata TO it_bdcdata.

ENDFORM. " BDC_FIELD

Read only

Former Member
0 Likes
1,076

Hi you can try using GUI_UPLOAD FM.

Read only

Former Member
0 Likes
1,076

you can try this

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

EXPORTING

  • I_FIELD_SEPERATOR =

i_line_header = 'X'

i_tab_raw_data = it_raw

i_filename = pfile

TABLES

i_tab_converted_data = lt_excel[].

  • 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.

Read only

Former Member
0 Likes
1,076

you can try this

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

EXPORTING

  • I_FIELD_SEPERATOR =

i_line_header = 'X'

i_tab_raw_data = it_raw

i_filename = pfile

TABLES

i_tab_converted_data = lt_excel[].

  • 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.

Read only

Former Member
0 Likes
1,076

Thank You