Skip to content

CL_FDT_XL_SPREADSHEET

Parse an uploaded Excel (.xlsx) file in ABAP — read cell values by sheet name and cell coordinate.

Purpose

CL_FDT_XL_SPREADSHEET reads an Excel file that has been uploaded as xstring. It does not generate Excel files — for generating Excel see the Excel from ABAP tutorial.

Package dependency

This class requires the SAP_FDT_BASIS package. Verify it is installed: open SE24 and search for CL_FDT_XL_SPREADSHEET. If the class is not found, an alternative for reading Excel is to unzip the .xlsx with CL_ABAP_ZIP and parse the XML directly.

Key methods

Method Description
Constructor ( document_name xdocument ) Pass a name string and the Excel file as xstring
GET_CELL_VALUE_FROM_WORKSHEET( sheet_name cell_coordinate ) Read a single cell value by sheet and coordinate (e.g. 'A1')

The class also implements IF_FDT_DOC_SPREADSHEET which provides additional worksheet introspection methods.

Example — upload and read an Excel file

REPORT z_fdt_xl_read.

START-OF-SELECTION.

  " Step 1 — upload the Excel file from the user's PC in binary mode
  DATA lt_rawdata TYPE STANDARD TABLE OF x255.
  DATA lv_filename TYPE string VALUE 'C:\temp\myfile.xlsx'.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename = lv_filename
      filetype = 'BIN'
    TABLES
      data_tab = lt_rawdata
    EXCEPTIONS
      OTHERS   = 1.

  IF sy-subrc <> 0.
    MESSAGE 'Upload failed' TYPE 'E'.
    RETURN.
  ENDIF.

  " Step 2 — convert binary table to xstring
  DATA lx_excel TYPE xstring.
  LOOP AT lt_rawdata INTO DATA(ls_raw).
    CONCATENATE lx_excel ls_raw INTO lx_excel IN BYTE MODE.
  ENDLOOP.

  " Step 3 — create the spreadsheet reader
  TRY.
      DATA(lo_xl) = NEW cl_fdt_xl_spreadsheet(
                        document_name = lv_filename
                        xdocument     = lx_excel ).
    CATCH cx_fdt_excel_core INTO DATA(lx).
      MESSAGE lx->get_text( ) TYPE 'E'.
      RETURN.
  ENDTRY.

  " Step 4 — read individual cells by sheet name and coordinate
  DATA(lv_a1) = lo_xl->get_cell_value_from_worksheet(
                  sheet_name      = 'Sheet1'
                  cell_coordinate = 'A1' ).

  DATA(lv_b2) = lo_xl->get_cell_value_from_worksheet(
                  sheet_name      = 'Sheet1'
                  cell_coordinate = 'B2' ).

  WRITE: / 'A1:', lv_a1.
  WRITE: / 'B2:', lv_b2.

Sheet name is case-sensitive

'Sheet1' and 'sheet1' are treated as different sheets. Use the exact name as it appears in the Excel tab.

All cell values are returned as STRING

GET_CELL_VALUE_FROM_WORKSHEET always returns TYPE STRING regardless of the cell's Excel format. Convert to numeric or date types explicitly if needed after reading.

See also

Comments