This Program will take the data from selection screen and put into text file and ZIP that file and download into your desktop.
REPORT ztest_program.
DATA:g_val TYPE c,
file_tab TYPE STANDARD TABLE OF solisti1,
bin_file TYPE xstring,
bytecount TYPE i,
path_table TYPE TABLE OF char1024.
TYPES:
BEGIN OF ty_bin_file,
name TYPE string,
size TYPE i,
data TYPE solix_tab,
END OF ty_bin_file.
TYPES :BEGIN OF ty_data,
data TYPE c LENGTH 255,
END OF ty_data.
DATA: gt_bindata TYPE STANDARD TABLE OF ty_bin_file,
gt_data TYPE TABLE OF ty_data,
gs_data1 TYPE ty_data.
DATA:gs_data TYPE kna1.
DATA:gt_kna1 TYPE TABLE OF kna1.
DATA:gcl_zip TYPE REF TO cl_abap_zip.
"Select options
SELECT-OPTIONS:s_kunnr FOR gs_data-kunnr.
START-OF-SELECTION.
"Get the data from Kna1 table
PERFORM get_data.
*&---------------------------------------------------------------------*
*&
Form GET_DATA
*&---------------------------------------------------------------------*
*
FORM get_data .
DATA:ls_knumv TYPE konv.
DATA:lt_content TYPE soli_tab.
DATA: lv_string TYPE string,
lv_data TYPE string.
FIELD-SYMBOLS:<fs_data> TYPE ANY,
<fs_field> TYPE ANY.
DATA:lv_separator TYPE c LENGTH 1 VALUE '|'.
SELECT * FROM kna1 INTO TABLE gt_kna1
WHERE kunnr IN s_kunnr.
IF sy-subrc = 0.
LOOP AT gt_kna1 ASSIGNING <fs_data>.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_data>
TO <fs_field>.
IF sy-subrc = 0.
lv_data = <fs_field>.
CONCATENATE lv_string
lv_data lv_separator
INTO lv_string.
CLEAR:lv_data.
ELSE.
gs_data1-data = lv_string.
APPEND gs_data1 TO gt_data.
EXIT.
ENDIF.
ENDDO.
ENDLOOP.
ENDIF.
PERFORM zip_file TABLES lt_content.
ENDFORM.
" GET_DATA
*&---------------------------------------------------------------------*
*&
Form zipping_pdf_file
*&---------------------------------------------------------------------*
FORM zip_file TABLES lt_content.
DATA:zip TYPE xstring.
DATA:ls_content TYPE soli,
lv_len TYPE i.
DATA:lv_xstring TYPE xstring,
lv_name TYPE string.
* create our zipper object
CREATE OBJECT gcl_zip.
CALL FUNCTION 'SCMS_TEXT_TO_BINARY'
IMPORTING
output_length = lv_ len
TABLES
text_tab = gt_data
binary_tab = lt_content[]
EXCEPTIONS
FAILED = 1
OTHERS = 2
.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_len
IMPORTING
buffer = lv_xstring
TABLES
binary_tab = lt_content[]
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
RAISE ex_bin_conv_error.
ENDIF.
* add file to zip
lv_name = 'knumv.txt'.
CALL METHOD gcl_zip->add
EXPORTING
name = lv_name
content = lv_xstring.
* "save zip
CALL METHOD gcl_zip->save
RECEIVING
zip = zip.
PERFORM download_zip_file TABLES lt_content
USING zip.
ENDFORM.
" zipping_pdf_file
*&---------------------------------------------------------------------*
*&
Form download_zip_file
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->ZIP
text
*----------------------------------------------------------------------*
FORM download_zip_file TABLES lt_contents
USING zip.
DATA:lv_out TYPE i.
DATA:lv_filename TYPE string,
lv_path TYPE string,
pv_dest_filepath TYPE string.
** Get the binary stream for ZIP file
*
** Convert the XSTRING to Binary table
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = zip
IMPORTING
output_length = lv_out
TABLES
binary_tab = lt_contents[].
cl_gui_frontend_services=>file_save_dialog(
EXPORTING
window_title = 'Select the File Save Location'
file_filter = '(*.zip)|*.zip|'
CHANGING
filename = lv_filename
path = lv_path
fullpath = pv_dest_filepath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
n
ot_supported_by_gui = 3
OTHERS = 4
).
cl_gui_frontend_services=>gui_download(
EXPORTING
bin_filesize = lv_out
filename = pv_dest_filepath
filetype = 'BIN'
CHANGING
data_tab = lt_contents[]
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
).
ENDFORM.
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Team Lead
Contact : serkurumsal@yandex.com
Social Plugin