Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Jigang_Zhang张吉刚
Active Contributor
It's a very common requirement to remove the leading zero at ALV list output or send out an excel file as an email attachment. There're many documents that describe how to achieve that. Recently, I get the requirement is to keep the leading zero for an order number, I thought it'll be quite straight forward then it cost me several hours to achieve that~

The easiest way could be:

  1. using ALV OO with method cl_salv_column_table~set_leading_zero to set column which needs

  2. convert the internal table into XML format, then using cl_document_bcs=>xstring_to_solix   convert into excel file

  3. using cl_document_bcs=>create_document  and method ADD_ATTACHMENT to send out.


 
*Convert ALV list to excel format
lv_xml_type = if_salv_bs_xml=>c_type_mhtml.
lv_xml = gr_table->to_xml( xml_type = lv_xml_type ).
TRY.
size = xstrlen( lv_xml ).
*Send ALV converted excel file attachment
binary_content = cl_document_bcs=>xstring_to_solix(
ip_xstring = lv_xml ).
* -------- create persistent send request ------------------------
send_request = cl_bcs=>create_persistent( ).

* -------- create and set document with attachment ---------------
* create document object from the internal table with text

document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = main_text
i_subject = 'ZBKLG_Report' ). "#EC NOTEXT

* add the spreadsheet as an attachment to document object

document->add_attachment(
i_attachment_type = 'XLS' "#EC NOTEXT
i_attachment_subject = lv_sub "'ExampleSpreadSheet'
i_attachment_size = size
i_att_content_hex = binary_content ).

* add document object to send request
send_request->set_document( document ).

* --------- add recipient (e-mail address) -----------------------
* create recipient object

LOOP AT s_email.
recipient =
cl_cam_address_bcs=>create_internet_address( s_email-low ).
ld_email = s_email-low.

* add recipient object to send request
send_request->add_recipient( recipient ).
ENDLOOP.

* ---------- send document ---------------------------------------
sent_to_all = send_request->send( i_with_error_screen = 'X' ).

COMMIT WORK.

IF sy-subrc <> 0.
MOVE text-e05 TO l_msg.
REPLACE '&1' WITH p_email INTO l_msg.
MESSAGE l_msg TYPE 'E' DISPLAY LIKE 'E'.

ELSE.
MOVE text-s02 TO l_msg.
REPLACE '&1' WITH ld_email INTO l_msg.
MESSAGE l_msg TYPE 'I' DISPLAY LIKE 'I'.
ENDIF.

* ------------ exception handling ----------------------------------
* replace this rudimentary exception handling with your own one !!!
CATCH cx_bcs INTO bcs_exception.
MESSAGE i865(so) WITH bcs_exception->error_type.
ENDTRY.

But it'll not show the leading zero for order number if you define refer to system field VBELN/EBELN etc. In an excel file, the leading zero will hide automatically although we send it out with leading zeros.



 

The quick way is to choose this column and set the custom format as 0000000000, then Excel will follow this format and display correctly.


Of course, the user will not accept that as they need extra work even if it cost only 10 seconds: P The final approach is very simple, DO NOT USE SYSTEM DATA ELEMENT, using CHAR10 will do!


 

5 Comments
Labels in this area