{"id":7928,"date":"2025-10-19T02:00:54","date_gmt":"2025-10-18T20:30:54","guid":{"rendered":"https:\/\/www.sapewmhelp.com\/?question=xco_cp_xlsx-package-read-excel-sheet"},"modified":"2025-10-19T02:00:54","modified_gmt":"2025-10-18T20:30:54","slug":"xco_cp_xlsx-package-read-excel-sheet","status":"publish","type":"question","link":"https:\/\/www.sapewmhelp.com\/?question=xco_cp_xlsx-package-read-excel-sheet","title":{"rendered":"xco_cp_xlsx package &#8211; Read Excel Sheet"},"content":{"rendered":"<p>SAP\u2019s <strong>XCO Library<\/strong> provides APIs for working with Office documents, including Excel (XLSX) \u2014 via the <code>xco_cp_xlsx<\/code> package<\/p>\n<p>&nbsp;<\/p>\n<p>CLASS zcl_read_employee_excel DEFINITION<br \/>\nPUBLIC<br \/>\nFINAL<br \/>\nCREATE PUBLIC.<\/p>\n<p>PUBLIC SECTION.<br \/>\nMETHODS read_employee_excel.<br \/>\nENDCLASS.<\/p>\n<p>CLASS zcl_read_employee_excel IMPLEMENTATION.<\/p>\n<p>METHOD read_employee_excel.<\/p>\n<p>&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n&#8221; Step 1: Define employee structure and table<br \/>\n&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nTYPES: BEGIN OF ty_employee,<br \/>\nempid TYPE string,<br \/>\nname TYPE string,<br \/>\ndepartment TYPE string,<br \/>\nsalary TYPE string,<br \/>\nEND OF ty_employee.<\/p>\n<p>DATA lt_employee TYPE STANDARD TABLE OF ty_employee.<br \/>\nDATA ls_employee TYPE ty_employee.<\/p>\n<p>&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n&#8221; Step 2: Load Excel file (binary content)<br \/>\n&#8221; Replace file path with your actual file location<br \/>\n&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nDATA(lv_file_path) = &#8216;C:\\Temp\\employees.xlsx&#8217;.<br \/>\nDATA(lv_xstring) TYPE xstring.<\/p>\n<p>TRY.<br \/>\ncl_gui_frontend_services=&gt;gui_upload_xstring(<br \/>\nEXPORTING<br \/>\nfilename = lv_file_path<br \/>\nIMPORTING<br \/>\ndata = lv_xstring<br \/>\n).<br \/>\nCATCH cx_root INTO DATA(lx_err).<br \/>\nWRITE: \/ &#8216;Error reading file:&#8217;, lx_err-&gt;get_text( ).<br \/>\nRETURN.<br \/>\nENDTRY.<\/p>\n<p>&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n&#8221; Step 3: Parse Excel using XCO Library<br \/>\n&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nDATA(lo_excel_doc) = xco_cp_xlsx=&gt;document-&gt;from_binary( lv_xstring ).<\/p>\n<p>&#8221; Get first sheet<br \/>\nDATA(lo_sheet) = lo_excel_doc-&gt;sheets-&gt;get_by_index( 1 ).<\/p>\n<p>&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n&#8221; Step 4: Read data row by row (skip header)<br \/>\n&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nDATA(lt_rows) = lo_sheet-&gt;rows-&gt;all( ).<br \/>\nLOOP AT lt_rows INTO DATA(lo_row) INDEX DATA(lv_index).<\/p>\n<p>IF lv_index = 1.<br \/>\nCONTINUE. &#8221; Skip header row<br \/>\nENDIF.<\/p>\n<p>DATA(lt_cells) = lo_row-&gt;cells-&gt;all( ).<\/p>\n<p>CLEAR ls_employee.<br \/>\nLOOP AT lt_cells INTO DATA(lo_cell) INDEX DATA(lv_col_index).<br \/>\nCASE lv_col_index.<br \/>\nWHEN 1. ls_employee-empid = lo_cell-&gt;value-&gt;to_string( ).<br \/>\nWHEN 2. ls_employee-name = lo_cell-&gt;value-&gt;to_string( ).<br \/>\nWHEN 3. ls_employee-department = lo_cell-&gt;value-&gt;to_string( ).<br \/>\nWHEN 4. ls_employee-salary = lo_cell-&gt;value-&gt;to_string( ).<br \/>\nENDCASE.<br \/>\nENDLOOP.<\/p>\n<p>APPEND ls_employee TO lt_employee.<\/p>\n<p>ENDLOOP.<\/p>\n<p>&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n&#8221; Step 5: Display results<br \/>\n&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nULINE.<br \/>\nWRITE: \/ &#8216;EMPID&#8217;, 10 &#8216;NAME&#8217;, 30 &#8216;DEPARTMENT&#8217;, 50 &#8216;SALARY&#8217;.<br \/>\nULINE.<br \/>\nLOOP AT lt_employee INTO ls_employee.<br \/>\nWRITE: \/ ls_employee-empid UNDER &#8216;EMPID&#8217;,<br \/>\nls_employee-name UNDER &#8216;NAME&#8217;,<br \/>\nls_employee-department UNDER &#8216;DEPARTMENT&#8217;,<br \/>\nls_employee-salary UNDER &#8216;SALARY&#8217;.<br \/>\nENDLOOP.<br \/>\nULINE.<\/p>\n<p>ENDMETHOD.<\/p>\n<p>ENDCLASS.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","question-category":[158],"question_tags":[178,447,177,446],"class_list":["post-7928","question","type-question","status-publish","hentry","question-category-abap","question_tags-abap","question_tags-excel-sheet","question_tags-sap","question_tags-xco-library"],"_links":{"self":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question\/7928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question"}],"about":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7928"}],"wp:attachment":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7928"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion-category&post=7928"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion_tags&post=7928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}