2025 Jun 18 1:21 PM - edited 2025 Jun 19 8:58 AM
Hello everyone, I'm developer but I am having a problem with my code that is I want to export data to excel without using OLE but I want to export data in external format when using CALL FUNCTION CONVERT DATE TO INTERNAL when the data displayed on ALV is DD/MM/YYYY then I want when exporting data to excel it will display as YYYYMMDD. I need help.
ABAP Development SAP BTP ABAP environment
FORM download_data_to_excel USING p_table TYPE tabname.
DATA lt_fields TYPE STANDARD TABLE OF dfies.
DATA lt_data TYPE REF TO data.
DATA lv_localpath TYPE string.
DATA lv_msg TYPE string.
DATA lo_alv TYPE REF TO cl_salv_table.
DATA lv_xstring TYPE xstring.
DATA lv_bytecount TYPE i.
DATA lt_solix TYPE solix_tab.
FIELD-SYMBOLS <lt_table> TYPE STANDARD TABLE.
" TODO: variable is never used (ABAP cleaner)
DATA ls_field TYPE dfies.
" Lấy metadata bảng
CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING tabname = p_table
langu = sy-langu
TABLES dfies_tab = lt_fields
EXCEPTIONS not_found = 1
OTHERS = 2.
IF sy-subrc <> 0 OR lt_fields IS INITIAL.
MESSAGE ':cross_mark: Không thể lấy metadata của bảng.' TYPE 'E'.
ENDIF.
" Tạo bảng động chứa dữ liệu
CREATE DATA lt_data TYPE STANDARD TABLE OF (p_table).
ASSIGN lt_data->* TO <lt_table>.
" Lấy toàn bộ dữ liệu từ bảng
SELECT * FROM (p_table) INTO TABLE @<lt_table>.
IF sy-subrc <> 0 OR lines( <lt_table> ) = 0.
MESSAGE ':information: Bảng không có dữ liệu để xuất.' TYPE 'I'.
RETURN.
ENDIF.
" Hộp thoại chọn nơi lưu file Excel
cl_gui_frontend_services=>file_save_dialog( EXPORTING default_extension = 'xlsx'
default_file_name = |Data_{ p_table }_{ sy-datum }_{ sy-uzeit }.xlsx|
initial_directory = 'C:\TEMP\'
prompt_on_overwrite = 'X'
CHANGING filename = lv_localpath
path = lv_msg
fullpath = lv_localpath
EXCEPTIONS cntl_error = 1
error_no_gui = 2
OTHERS = 3 ).
IF sy-subrc <> 0 OR lv_localpath IS INITIAL.
MESSAGE ':information: Bạn chưa chọn nơi lưu.' TYPE 'I'.
RETURN.
ENDIF.
" Tạo ALV từ dữ liệu
TRY.
cl_salv_table=>factory( IMPORTING r_salv_table = lo_alv
CHANGING t_table = <lt_table> ).
" Đặt tiêu đề cột đơn giản
DATA(lo_columns) = lo_alv->get_columns( ).
lo_columns->set_optimize( abap_true ).
" Chuyển đổi sang XLSX
lv_xstring = lo_alv->to_xml( xml_type = if_salv_bs_xml=>c_type_xlsx ).
" Chuyển XSTRING sang SOLIX
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING buffer = lv_xstring
IMPORTING output_length = lv_bytecount
TABLES binary_tab = lt_solix.
" Tải file xuống
cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = lv_bytecount
filename = lv_localpath
filetype = 'BIN'
CHANGING data_tab = lt_solix
EXCEPTIONS file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24 ).
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
MESSAGE |:white_heavy_check_mark: Xuất dữ liệu thành công. Tổng cộng { lines( <lt_table> ) } dòng.| TYPE 'S'.
ENDIF.
CATCH cx_root INTO DATA(lx_error).
MESSAGE |:cross_mark: Lỗi khi xuất file Excel: { lx_error->get_text( ) }| TYPE 'E'.
ENDTRY.
ENDFORM.
LOOP AT lt_fields INTO ls_field.
ASSIGN COMPONENT ls_field-fieldname OF STRUCTURE <ls_row> TO <lv_value>.
IF sy-subrc <> 0 OR <lv_value> IS INITIAL.
CONTINUE.
ENDIF.
CALL METHOD OF lo_sheet 'Cells' = lo_cell
EXPORTING #1 = lv_row
#2 = lv_col.
IF ls_field-datatype = 'DATS'.
CALL FUNCTION convert DATE TO INTERNAL
EXPORTING date_external = <lv_value>
accept_initial_date = abap_on
importing
date_internal = lv_data
exceptions
date_external_is_invalid = 1
others = 2.
IF sy-subrc <> 0.
" Implement suitable error handling here
ENDIF.
SET PROPERTY OF lo_cell 'Value' = lv_data.
ELSEIF ls_field-datatype = 'TIMS'.
WRITE <lv_value> TO lv_data2.
REPLACE ALL OCCURRENCES OF ':' IN lv_data2 WITH ' '.
SET PROPERTY OF lo_cell 'Value' = lv_data2.
ELSE.
SET PROPERTY OF lo_cell 'Value' = <lv_value>. " 20250610
SET PROPERTY OF lo cell 'NumberFormat' = '@'.
ENDIF.
ENDLOOP.
Request clarification before answering.
| User | Count |
|---|---|
| 12 | |
| 9 | |
| 7 | |
| 5 | |
| 4 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.