How to upload Excel File into an internal table in ABAP

Sometimes We need upload an excel and insert the data that this file contains, to do this its too easy, just its necessary made use of one function called  KD_GET_FILENAME_ON_F4 .

We are going to show you one example.

Description of the requirement

We need a prorgam that upload information in and excel file of carrid and Conid fields to then update spfli table

Solution

We will develop program that with and excel file will upload inormation and save in an internal table.

Program name: Z_PRUEBAS

this is the program that upload excel dile and save in itab

As you can see we have a parameter called p_file that this file will recive the path when we have our excel file saved, also you can see we have a top include that cotaints or logic to upload this file

Here its content:

*&---------------------------------------------------------------------*
*&  Include           Z_PRUEBAS_TOP
*&---------------------------------------------------------------------*

PARAMETERS: p_file TYPE rlgrap-filename.

DATA: itab   TYPE STANDARD TABLE OF alsmex_tabline,
      l_itab TYPE  alsmex_tabline.


TYPES: BEGIN OF ty_excel,
         carrid  LIKE spfli-carrid,
         conid   LIKE spfli-connid,
       END OF ty_excel.


DATA: it_excel      TYPE STANDARD TABLE OF ty_excel INITIAL SIZE 0,
      wa_excel      TYPE ty_excel,
      gd_currentrow TYPE i.


CLASS cl_report DEFINITION.
  PUBLIC SECTION.

    METHODS:
      load_excel,
      send_to_itab.


ENDCLASS.


CLASS cl_report IMPLEMENTATION.

  METHOD load_excel.
      CLEAR: it_excel, itab.
    CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename                = p_file
        i_begin_col             = '1'
        i_begin_row             = '2'  "Do not require headings
        i_end_col               = '14'
        i_end_row               = '1048576'
      TABLES
        intern                  = itab
      EXCEPTIONS
        inconsistent_parameters = 1
        upload_ole              = 2
        OTHERS                  = 3.
    IF sy-subrc <> 0.
      MESSAGE e010(zz) WITH TEXT-001. "Problem uploading Excel Spreadsheet
    ENDIF.
   ENDMETHOD.

   METHOD send_to_itab.
     SORT itab BY row col.

* Get first row retrieved
    READ TABLE itab INDEX 1 INTO l_itab.

* Set first row retrieved to current row
    gd_currentrow = l_itab-row.

    LOOP AT itab INTO l_itab.
*   Reset values for next row
      IF l_itab-row NE gd_currentrow.
        APPEND wa_excel TO it_excel.
        CLEAR wa_excel.
        gd_currentrow = l_itab-row.
      ENDIF.

      CASE l_itab-col.
        WHEN '0001'.
          wa_excel-carrid   = l_itab-value.
        WHEN '0002'.
          wa_excel-conid = l_itab-value.
      ENDCASE.
   ENDLOOP.

    APPEND wa_excel TO it_excel.
     ENDMETHOD.

  ENDCLASS.

  DATA: class TYPE REF TO cl_report.

 

As you can see we have our clase with our method that we use on first program.

Runing the prorgam

if you run this program you will see the information saved into it_excel table:

 

What about the file?

if you asked yourself about the excel file, dont worry, its this:

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up