Friday, April 19, 2019

Excel to Internal Table


In the following program I have entered an excel with data. Now I need to pass this excel into SAP system. The data will be converted into internal format of SAP and an internal table will be populated. Finally when we have the data in our internal table then we can use those data as required. In this example I have used Field-Symbol. We can use normal internal table & work area in the same fashion.

REPORT zsr_bdc
       NO STANDARD PAGE HEADING LINE-SIZE 255.

TYPE-POOLS truxs.
DATA dref TYPE REF TO data.
TYPESBEGIN OF ts_excl,
         ccode  TYPE string,
         porg   TYPE string,
         accgrp TYPE string,
       END OF ts_excl.
FIELD-SYMBOLS<wa_excl> TYPE ts_excl,
               <it_excl> TYPE STANDARD TABLE,
               <it_data> TYPE STANDARD TABLE.

INITIALIZATION.
  PARAMETERS p_file TYPE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM f4_filename.

START-OF-SELECTION.
  PERFORM convert_excel_to_table.
  PERFORM display_data.

*&---------------------------------------------------------------------*
*& Form F4_FILENAME
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM f4_filename .

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name 'P_FILE'
    IMPORTING
      file_name  p_file.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form CONVERT_EXCEL_TO_TABLE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM convert_excel_to_table .

  CREATE DATA dref TYPE truxs_t_text_data.
  ASSIGN dref->TO <it_data>.
  CREATE DATA dref TYPE TABLE OF ts_excl.
  ASSIGN dref->TO <it_excl>.

  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_line_header        'X'
      i_tab_raw_data       <it_data>
      i_filename           p_file
    TABLES
      i_tab_converted_data <it_excl>
    EXCEPTIONS
      conversion_failed    1
      OTHERS               2.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form DISPLAY_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM display_data .

  WRITE'Company',
        12 'Purchase Org.',
        42 'Account Group'.
  ULINE.
  IF <it_excl> IS ASSIGNED.
    LOOP AT <it_excl> ASSIGNING <wa_excl>.
      WRITE/4 <wa_excl>-ccode,
             12 <wa_excl>-porg,
             42 <wa_excl>-accgrp.
    ENDLOOP.
  ENDIF.

ENDFORM.

The excel file is as follows.

Execute the program and then browse the excel file. 

The output is as follows.


1 comment:

Anonymous said...

Hi...I was going through your blog and it seems so helpful ..Thank you