Wednesday, April 3, 2019

Fetch data from excel file into Internal table

 DATA: lt_itab  TYPE STANDARD TABLE OF alsmex_tabline INITIAL SIZE 0.
  DATA: lk_itab  TYPE alsmex_tabline,
              lk_excel TYPE ty_excel.

  DATA  lt_excel TYPE STANDARD TABLE OF ty_excel.
  DATA  lv_ivnum(10) TYPE n.

  FIELD-SYMBOLS: <itab_xls> TYPE any,
                 <fk_excel> TYPE ty_excel.
*read file from presentation Layer for XL file upload
  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_file
      i_begin_col             = '1'
      i_begin_row             = '2'
      i_end_col               = '200'
      i_end_row               = '9999'
    TABLES
      intern                  = lt_itab
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
* Sort table by rows and colums
  SORT lt_itab BY row col ASCENDING.

  IF NOT lt_itab IS INITIAL.
* Populate it_data_read with the data read from excel file
    LOOP AT lt_itab INTO lk_itab.
      ASSIGN COMPONENT lk_itab-col OF STRUCTURE lk_excel
            TO <itab_xls>.
      IF sy-subrc NE 0.
        EXIT.
      ENDIF.

      <itab_xls> = lk_itab-value.

      AT END OF row.
        APPEND lk_excel TO gt_excel.
        CLEAR: lk_itab, lk_excel.
      ENDAT.
    ENDLOOP.
  ENDIF.

No comments:

Post a Comment