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

How to create API to download an Excel sheet

create a CDS view

custom Entity:

@EndUserText.label: ‘template Download’

@ObjectModel: {

query: {

implementedBy: ‘ABAP: ZCL_DOWN_TEMPLATE’

}

}

define root custom entity ZCHM__TMP_DWN

{

key docnum : abap.char(10);

comp_code : abap.char(4);

sales_org : abap.char(4);

distr_chn : abap.char(2);

division : abap.char(2);

ref_doc : abap.char(20);

currency : abap.char(8);

ref_item : abap.char(10);

material : abap.char(40);

matl_group : abap.char(9);

net_amount : abap.char(40);

gross : abap.char(15);

quantity : abap.char(13);

price_unit : abap.char(5);

bill_unit : abap.char(3);

file_content : abap.string( 999999999 );

}

 

then Create a Class where you can write you logic to create the template and pass the file content to the front end.

 

CLASS zcl_down_template DEFINITION

PUBLIC FINAL

CREATE PUBLIC.

PUBLIC SECTION.

INTERFACES if_rap_query_provider.

PRIVATE SECTION.

TYPES: BEGIN OF lty_template,

docnum TYPE c LENGTH 40,

Process_type TYPE c LENGTH 40,

comp_code TYPE c LENGTH 40,

sales_org TYPE c LENGTH 40,

distr_chn TYPE c LENGTH 40,

division TYPE c LENGTH 40,

ref_doc TYPE c LENGTH 40,

currency TYPE c LENGTH 40,

material TYPE c LENGTH 40,

matl_group TYPE c LENGTH 40,

net_amount TYPE c LENGTH 40,

gross TYPE c LENGTH 40,

quantity TYPE c LENGTH 40,

price_unit TYPE c LENGTH 40,

bill_unit TYPE c LENGTH 40,

END OF lty_template.

DATA et_result TYPE TABLE OF lty_template.

DATA es_result TYPE lty_template.

DATA et_res TYPE TABLE OF ztmp_dwn.

DATA es_res TYPE ztmp_dwn.

ENDCLASS.

CLASS zcl_down_template IMPLEMENTATION.

METHOD if_rap_query_provider~select.

” TODO: variable is assigned but never used (ABAP cleaner)

DATA(lr_skip) = io_request->get_paging( )->get_offset( ).

” TODO: variable is assigned but never used (ABAP cleaner)

DATA(lr_top) = io_request->get_paging( )->get_page_size( ).

DATA lv_index_root TYPE i VALUE 1.

DATA lo_document TYPE REF TO if_xco_xlsx_wa_document.

DATA lo_sheet TYPE REF TO if_xco_xlsx_wa_worksheet.

DATA lv_sheet_name TYPE string.

lv_sheet_name = |Download_Template_{ cl_abap_context_info=>get_system_date( ) }|.

lo_document = xco_cp_xlsx=>document->empty( )->write_access( ).

lo_sheet = lo_document->get_workbook( )->worksheet->at_position( lv_index_root ).

lo_sheet->set_name( iv_name = lv_sheet_name ).

et_result = VALUE #( ( docnum = ‘ID’ Process_type = ‘Template’ )

( docnum = ‘DOCNUM’

Process_type = ‘PAYTYPE’

comp_code = ‘COMP_CODE’

sales_org = ‘SALES_ORG’

distr_chn = ‘DISTR_CHAN’

division = ‘DIVISION’

ref_doc = ‘REF_DOC’

currency = ‘CURRENCY’

material = ‘MATERIAL’

matl_group = ‘MATL_GROUP’

net_amount = ‘NET_AMOUNT’

gross = ‘GROSS’

quantity = ‘QUANTITY’

price_unit = ‘PRICE_UNIT’

bill_unit = ‘BILL_UNIT’)

( docnum = ‘*Document Number(10)’

Process_type = ‘*Process Type(4)’

comp_code = ‘*Company Code(4)’

sales_org = ‘*Sales Organization(4)’

distr_chn = ‘*Distribution Channel(2)’

division = ‘*Division’

ref_doc = ‘*Reference Document’

currency = ‘*Currency’

ref_item = ‘*Item No(10)’

material = ‘*Material(40)’

matl_group = ‘Material Group(9)’

net_amount = ‘NetAmount(15)’

gross = ‘Gross(15)’

quantity = ‘Quantuity(13)’

price_unit = ‘PriceUnit(5)’

bill_unit = ‘BillUnit(3)’ ) ).

 

DATA(lo_pattern) = xco_cp_xlsx_selection=>pattern_builder->simple_from_to( )->get_pattern( ).

lo_sheet->select( lo_pattern

)->row_stream(

)->operation->write_from( REF #( et_result )

)->execute( ).

DATA(ld_excel) = lo_document->get_file_content( ).

DATA(lv_base64_encoding) = xco_cp=>xstring( ld_excel )->as_string( xco_cp_binary=>text_encoding->base64 )->value.

es_resfile_content = lv_base64_encoding.

APPEND es_res TO et_res.

IF io_request->is_total_numb_of_rec_requested( ).

io_response->set_total_number_of_records( lines( et_res ) ).

ENDIF.

IF io_request->is_data_requested( ).

io_response->set_data( et_res ).

ENDIF.

ENDMETHOD.

ENDCLASS.

 

Now activate the class and expose it the service definition and at last create a web API v4 and pass the URL to the front person to fetch and download the excel sheet.

Related Questions

Leave an answer

Leave an answer