Hello,

Sign up to join our community!

Welcome Back,

Please sign in to your account!

Forgot Password,

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

SAP EWM Help Latest Questions

  • 0
  • 0
DPM125
Beginner

xco_cp_xlsx package – Read Excel Sheet

SAP’s XCO Library provides APIs for working with Office documents, including Excel (XLSX) — via the xco_cp_xlsx package

 

CLASS zcl_read_employee_excel DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.

PUBLIC SECTION.
METHODS read_employee_excel.
ENDCLASS.

CLASS zcl_read_employee_excel IMPLEMENTATION.

METHOD read_employee_excel.

“—————————————————
” Step 1: Define employee structure and table
“—————————————————
TYPES: BEGIN OF ty_employee,
empid TYPE string,
name TYPE string,
department TYPE string,
salary TYPE string,
END OF ty_employee.

DATA lt_employee TYPE STANDARD TABLE OF ty_employee.
DATA ls_employee TYPE ty_employee.

“—————————————————
” Step 2: Load Excel file (binary content)
” Replace file path with your actual file location
“—————————————————
DATA(lv_file_path) = ‘C:\Temp\employees.xlsx’.
DATA(lv_xstring) TYPE xstring.

TRY.
cl_gui_frontend_services=>gui_upload_xstring(
EXPORTING
filename = lv_file_path
IMPORTING
data = lv_xstring
).
CATCH cx_root INTO DATA(lx_err).
WRITE: / ‘Error reading file:’, lx_err->get_text( ).
RETURN.
ENDTRY.

“—————————————————
” Step 3: Parse Excel using XCO Library
“—————————————————
DATA(lo_excel_doc) = xco_cp_xlsx=>document->from_binary( lv_xstring ).

” Get first sheet
DATA(lo_sheet) = lo_excel_doc->sheets->get_by_index( 1 ).

“—————————————————
” Step 4: Read data row by row (skip header)
“—————————————————
DATA(lt_rows) = lo_sheet->rows->all( ).
LOOP AT lt_rows INTO DATA(lo_row) INDEX DATA(lv_index).

IF lv_index = 1.
CONTINUE. ” Skip header row
ENDIF.

DATA(lt_cells) = lo_row->cells->all( ).

CLEAR ls_employee.
LOOP AT lt_cells INTO DATA(lo_cell) INDEX DATA(lv_col_index).
CASE lv_col_index.
WHEN 1. ls_employee-empid = lo_cell->value->to_string( ).
WHEN 2. ls_employee-name = lo_cell->value->to_string( ).
WHEN 3. ls_employee-department = lo_cell->value->to_string( ).
WHEN 4. ls_employee-salary = lo_cell->value->to_string( ).
ENDCASE.
ENDLOOP.

APPEND ls_employee TO lt_employee.

ENDLOOP.

“—————————————————
” Step 5: Display results
“—————————————————
ULINE.
WRITE: / ‘EMPID’, 10 ‘NAME’, 30 ‘DEPARTMENT’, 50 ‘SALARY’.
ULINE.
LOOP AT lt_employee INTO ls_employee.
WRITE: / ls_employee-empid UNDER ‘EMPID’,
ls_employee-name UNDER ‘NAME’,
ls_employee-department UNDER ‘DEPARTMENT’,
ls_employee-salary UNDER ‘SALARY’.
ENDLOOP.
ULINE.

ENDMETHOD.

ENDCLASS.

Related Questions

Leave an answer

Leave an answer