*&---------------------------------------------------------------------*
*& Report ZDEMO_EXCEL16
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zdemo_excel16.
DATA: lo_excel TYPE REF TO zcl_excel,
lo_excel_writer TYPE REF TO zif_excel_writer,
lo_worksheet TYPE REF TO zcl_excel_worksheet,
lo_drawing TYPE REF TO zcl_excel_drawing.
DATA: lv_file TYPE xstring,
lv_bytecount TYPE i,
lt_file_tab TYPE solix_tab.
DATA: lv_full_path TYPE string,
lv_workdir TYPE string,
lv_file_separator TYPE c.
DATA: ls_io TYPE skwf_io.
CONSTANTS: lv_default_file_name TYPE string VALUE '16_Drawings.xlsx'.
PARAMETERS: p_path TYPE zexcel_export_dir,
p_objid TYPE sdok_docid DEFAULT '456694429165174BE10000000A1550C0', " Question mark in standard Web Dynpro WDT_QUIZ
p_class TYPE sdok_class DEFAULT 'M_IMAGE_P',
pobjtype TYPE skwf_ioty DEFAULT 'P'.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
lv_workdir = p_path.
cl_gui_frontend_services=>directory_browse( EXPORTING initial_folder = lv_workdir
CHANGING selected_folder = lv_workdir ).
p_path = lv_workdir.
INITIALIZATION.
cl_gui_frontend_services=>get_sapgui_workdir( CHANGING sapworkdir = lv_workdir ).
cl_gui_cfw=>flush( ).
p_path = lv_workdir.
START-OF-SELECTION.
IF p_path IS INITIAL.
p_path = lv_workdir.
ENDIF.
cl_gui_frontend_services=>get_file_separator( CHANGING file_separator = lv_file_separator ).
CONCATENATE p_path lv_file_separator lv_default_file_name INTO lv_full_path.
" Creates active sheet
CREATE OBJECT lo_excel.
"Load samle image
DATA: lt_bin TYPE solix_tab,
lv_len TYPE i,
lv_content TYPE xstring,
ls_key TYPE wwwdatatab.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = 'c:\Program Files\SAP\FrontEnd\SAPgui\wwi\graphics\W_bio.bmp'
filetype = 'BIN'
IMPORTING
filelength = lv_len
CHANGING
data_tab = lt_bin
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18
OTHERS = 19.
IF sy-subrc <> 0.
* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_len
IMPORTING
buffer = lv_content
TABLES
binary_tab = lt_bin
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
" Get active sheet
lo_worksheet = lo_excel->get_active_worksheet( ).
lo_worksheet->set_title( 'Sheet1' ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = 2 ip_value = 'Image from web repository (SMW0)' ).
" create global drawing, set position and media from web repository
lo_drawing = lo_excel->add_new_drawing( ).
lo_drawing->set_position( ip_from_row = 3
ip_from_col = 'B' ).
ls_key-relid = 'MI'.
ls_key-objid = 'SAPLOGO.GIF'.
lo_drawing->set_media_www( ip_key = ls_key
ip_width = 166
ip_height = 75 ).
" assign drawing to the worksheet
lo_worksheet->add_drawing( lo_drawing ).
" another drawing from a XSTRING read from a file
lo_worksheet->set_cell( ip_column = 'B' ip_row = 8 ip_value = 'Image from a file (c:\Program Files\SAP\FrontEnd\SAPgui\wwi\graphics\W_bio.bmp)' ).
lo_drawing = lo_excel->add_new_drawing( ).
lo_drawing->set_position( ip_from_row = 9
ip_from_col = 'B' ).
lo_drawing->set_media( ip_media = lv_content
ip_media_type = 'bmp'
ip_width = 83
ip_height = 160 ).
lo_worksheet->add_drawing( lo_drawing ).
ls_io-objid = p_objid.
ls_io-class = p_class.
ls_io-objtype = pobjtype.
IF ls_io IS NOT INITIAL.
" another drawing from a XSTRING read from a file
lo_worksheet->set_cell( ip_column = 'B' ip_row = 18 ip_value = 'Mime repository (by default Question mark in standard Web Dynpro WDT_QUIZ)' ).
lo_drawing = lo_excel->add_new_drawing( ).
lo_drawing->set_position( ip_from_row = 19
ip_from_col = 'B' ).
lo_drawing->set_media_mime( ip_io = ls_io
ip_width = 126
ip_height = 145 ).
lo_worksheet->add_drawing( lo_drawing ).
ENDIF.
CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_2007.
lv_file = lo_excel_writer->write_file( lo_excel ).
" Convert to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_file
IMPORTING
output_length = lv_bytecount
TABLES
binary_tab = lt_file_tab.
* " This method is only available on AS ABAP > 6.40
* lt_file_tab = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_file ).
* lv_bytecount = xstrlen( lv_file ).
" Save the file
cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = lv_bytecount
filename = lv_full_path
filetype = 'BIN'
CHANGING data_tab = lt_file_tab ).