Upload Excel document into ABAP internal table

ABAP code for uploading an Excel document into an internal table using function module ALSM_EXCEL_TO_INTERNAL_TABLE. See code below for structures. The code is base on uploading a simple Excel spreadsheet. This code also works with the new MS excel format .xlsx. Be carefull though for some reason SAP have decided to use a table structure which only allows a length of 50 characters per cell of your spreadsheet.

*&---------------------------------------------------------------------*
*& Report  UPLOAD_EXCEL                                                *
*&---------------------------------------------------------------------*
*& Upload and excel file into an internal table using the following    *
*& function module: ALSM_EXCEL_TO_INTERNAL_TABLE                       *
*&---------------------------------------------------------------------*
REPORT  UPLOAD_EXCEL no standard page heading.
*Data Declaration
*----------------
data: itab like alsmex_tabline occurs 0 with header line.
* Has the following format:
*             Row number   | Colum Number   |   Value
*             ---------------------------------------
*      i.e.     1                 1             Name1
*               2                 1             Joe
TYPES: Begin of t_record,
    name1 like itab-value,
    name2 like itab-value,
    age   like itab-value,
    End of t_record.
DATA: it_record type standard table of t_record initial size 0,
      wa_record type t_record.
DATA: gd_currentrow type i.
*Selection Screen Declaration
*----------------------------
PARAMETER p_infile like rlgrap-filename.
************************************************************************
*START OF SELECTION
 call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
       exporting
            filename                = p_infile
            i_begin_col             = '1'
            i_begin_row             = '2'  "Do not require headings
            i_end_col               = '14'
            i_end_row               = '31'
       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.
* Sort table by rows and colums
  sort itab by row col.
* Get first row retrieved
  read table itab index 1.
* Set first row retrieved to current row
  gd_currentrow = itab-row.
  loop at itab.
*   Reset values for next row
    if itab-row ne gd_currentrow.
      append wa_record to it_record.
      clear wa_record.
      gd_currentrow = itab-row.
    endif.
    case itab-col.
      when '0001'.                              "First name
        wa_record-name1 = itab-value.
      when '0002'.                              "Surname
        wa_record-name2 = itab-value.
      when '0003'.                              "Age
        wa_record-age   = itab-value.
    endcase.
  endloop.
  append wa_record to it_record.
*!! Excel data is now contained within the internal table IT_RECORD
* Display report data for illustration purposes
  loop at it_record into wa_record.
    write:/     sy-vline,
           (10) wa_record-name1, sy-vline,
           (10) wa_record-name2, sy-vline,
           (10) wa_record-age, sy-vline.
  endloop.

See here for links to other methods of uploading excel files to SAP


Related Articles

SAP applications and ABAP reports that process data files stored on your PC or within SAP
HEX codes and there associated ABAP declaration within SAP
Display files on SAP Application Server(UNIX)
Browse files on SAP Application Server(UNIX)
Downloading files to PC(Presentation Server)
Downloading files to SAP Application Server
Popup window to select a File using function module WS_FILENAME_GET
Get list of files within specific directory or SAP Application server
Directory selection for ABAP report using SAP method DIRECTORY_BROWSE
File Selection for ABAP report using SAP object method FILE_OPEN_DIALOG
Save file location popup on ABAP report selection screen using FILE_SAVE_DIALOG
ABAP Upload and download Function Modules in SAP
SAP File Selection Window - various methods of adding a file selection popup to your ABAP report
Upload Tab delimited file from PC into ABAP internal table
Upload and download files into and out of SAP or your PC
ABAP to check if file exists before downloading from SAP
SAP Upload Excel document into internal table
ABAP to Upload Excel document into internal table on SAP system including .xlsx
ABAP Uploading files from PC to SAP R/3 system
ABAP to Upload files from SAP Application Server
Upload Tab delimited file from PC into ABAP internal table
Upload Tab delimited file from application server into ABAP internal table
Example Excel Spreadsheet used to demo SAP upload and download using ABAP
Download file from SAP unix system to your desktop using transaction SXDA