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

create an Excel file with styling (colors, fonts, etc.) using the XCO Library in ABAP.

Create an Excel file called employees.xlsx containing employee data, where:

  • Header row has bold white text on a blue background

  • Data rows have standard black font

  • Salary column is formatted as currency

CLASS zcl_create_excel_xco DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.

PUBLIC SECTION.
METHODS create_employee_excel.
ENDCLASS.

CLASS zcl_create_excel_xco IMPLEMENTATION.

METHOD create_employee_excel.

“————————————————-
” Step 1: Define data
“————————————————-
TYPES: BEGIN OF ty_employee,
empid TYPE string,
name TYPE string,
department TYPE string,
salary TYPE p DECIMALS 2,
END OF ty_employee.

DATA(lt_employees) = VALUE STANDARD TABLE OF ty_employee(
( empid = ‘E001’ name = ‘Alice’ department = ‘IT’ salary = ‘65000’ )
( empid = ‘E002’ name = ‘Bob’ department = ‘HR’ salary = ‘55000’ )
( empid = ‘E003’ name = ‘Carol’ department = ‘Finance’ salary = ‘72000’ )
).

“————————————————-
” Step 2: Create Excel document
“————————————————-
DATA(lo_doc) = xco_cp_xlsx=>document->create( ).

” Add sheet
DATA(lo_sheet) = lo_doc->sheets->add( ‘Employees’ ).

“————————————————-
” Step 3: Define styles
“————————————————-
DATA(lo_styles) = lo_doc->styles.

” Header style: bold + white text + blue background
DATA(lo_style_header) = lo_styles->add( ).
lo_style_header->font->name->set( ‘Calibri’ ).
lo_style_header->font->bold->set( abap_true ).
lo_style_header->font->color->set_rgb( ‘FFFFFF’ ).
lo_style_header->fill->color->set_rgb( ‘4472C4’ ). ” Blue background
lo_style_header->alignment->horizontal->set( xco_cp_xlsx_alignment=>horizontal->center ).

” Normal cell style
DATA(lo_style_normal) = lo_styles->add( ).
lo_style_normal->font->name->set( ‘Calibri’ ).
lo_style_normal->font->size->set( 11 ).

” Salary style (number format)
DATA(lo_style_salary) = lo_styles->add( ).
lo_style_salary->font->name->set( ‘Calibri’ ).
lo_style_salary->number_format->set( xco_cp_xlsx_number_format=>builtin->currency_2 ).

“————————————————-
” Step 4: Write header row
“————————————————-
DATA(lo_header_row) = lo_sheet->rows->add( 1 ).

lo_header_row->cells->add( 1 )->value->set( ‘EmpID’ )->style->set( lo_style_header ).
lo_header_row->cells->add( 2 )->value->set( ‘Name’ )->style->set( lo_style_header ).
lo_header_row->cells->add( 3 )->value->set( ‘Department’ )->style->set( lo_style_header ).
lo_header_row->cells->add( 4 )->value->set( ‘Salary’ )->style->set( lo_style_header ).

“————————————————-
” Step 5: Fill data rows
“————————————————-
DATA(lv_row_index) = 2.

LOOP AT lt_employees INTO DATA(ls_emp).
DATA(lo_row) = lo_sheet->rows->add( lv_row_index ).

lo_row->cells->add( 1 )->value->set( ls_emp-empid )->style->set( lo_style_normal ).
lo_row->cells->add( 2 )->value->set( ls_emp-name )->style->set( lo_style_normal ).
lo_row->cells->add( 3 )->value->set( ls_emp-department )->style->set( lo_style_normal ).
lo_row->cells->add( 4 )->value->set( ls_emp-salary )->style->set( lo_style_salary ).

lv_row_index += 1.
ENDLOOP.

” Auto-fit columns
lo_sheet->columns->auto_fit( ).

“————————————————-
” Step 6: Export as XSTRING and save to frontend
“————————————————-
DATA(lv_xstring) = lo_doc->to_binary( ).

” Save file to frontend
DATA(lv_file_name) = ‘C:\Temp\employees.xlsx’.

TRY.
cl_gui_frontend_services=>gui_download(
EXPORTING
bin_filesize = xstrlen( lv_xstring )
filename = lv_file_name
filetype = ‘BIN’
CHANGING
data_tab = cl_bcs_convert=>xstring_to_solix( lv_xstring )
).
WRITE: / ‘Excel file created successfully:’, lv_file_name.
CATCH cx_root INTO DATA(lx_err).
WRITE: / ‘Error saving file:’, lx_err->get_text( ).
ENDTRY.

ENDMETHOD.

ENDCLASS.

Related Questions

Leave an answer

Leave an answer