*&---------------------------------------------------------------------* *& Report Fill Template *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT zdemo_excel_fill_template. *================= * Start of generated code. * All these types were generated * by ZEXCEL_TEMPLATE_GET_TYPES based * on the Excel file ZDEMO_EXCEL_TEMPLATE * from SMW0. *================= TYPES t_number TYPE p length 16 decimals 4. TYPES: begin of t_TABLE1, PERSON type string, SALARY type t_number, end of t_TABLE1, tt_TABLE1 type standard table of t_TABLE1 with default key, begin of t_LINE1, CARRID type string, CONNID type string, FLDATE type d, PRICE type t_number, end of t_LINE1, tt_LINE1 type standard table of t_LINE1 with default key, begin of t_TABLE2, CARRID type string, PRICE type t_number, LINE1 type tt_LINE1, end of t_TABLE2, tt_TABLE2 type standard table of t_TABLE2 with default key, begin of t_Sheet1, DATE type d, TIME type t, USER type string, TOTAL type t_number, PRICE type t_number, TABLE1 type tt_TABLE1, TABLE2 type tt_TABLE2, end of t_Sheet1, begin of t_TABLE3, PERSON type string, SALARY type t_number, end of t_TABLE3, tt_TABLE3 type standard table of t_TABLE3 with default key, begin of t_Sheet2, DATE type d, TIME type t, USER type string, TOTAL type t_number, TABLE3 type tt_TABLE3, end of t_Sheet2. DATA: lo_data type ref to ZCL_EXCEL_TEMPLATE_DATA. *================= * End of generated code *================= * define variables DATA: gs_sheet1 TYPE t_sheet1, gs_sheet2 TYPE t_sheet2. TABLES: sscrfields. CONSTANTS: gc_save_file_name TYPE string VALUE 'fill_template.xlsx'. INCLUDE zdemo_excel_outputopt_incl. PARAMETERS: p_smw0 RADIOBUTTON GROUP rad1 DEFAULT 'X'. PARAMETERS: p_objid TYPE w3objid OBLIGATORY DEFAULT 'ZDEMO_EXCEL_TEMPLATE'. PARAMETERS: p_file RADIOBUTTON GROUP rad1. PARAMETERS: p_fpath TYPE string OBLIGATORY LOWER CASE DEFAULT 'c:\temp\whatever.xlsx'. SELECTION-SCREEN SKIP 1. SELECTION-SCREEN PUSHBUTTON /1(45) but_txt USER-COMMAND get_types. INITIALIZATION. but_txt = '@BY@ Analyze file to propose TYPES'. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fpath. PERFORM get_file_path CHANGING p_fpath. AT SELECTION-SCREEN. CASE sscrfields-ucomm. WHEN 'GET_TYPES'. SUBMIT zexcel_template_get_types WITH p_smw0 = p_smw0 WITH p_objid = p_objid WITH p_file = p_file WITH p_fpath = p_fpath AND RETURN. ENDCASE. START-OF-SELECTION. PERFORM load_data. PERFORM generate_file. FORM load_data. FIELD-SYMBOLS: TYPE t_table1, TYPE t_line1, TYPE t_table2. gs_sheet1-date = sy-datum. gs_sheet1-time = sy-uzeit. gs_sheet1-user = sy-uname. gs_sheet1-total = 5600. APPEND INITIAL LINE TO gs_sheet1-table1 ASSIGNING . -person = 'Lurch Schpellchek'. -salary = 1200. APPEND INITIAL LINE TO gs_sheet1-table1 ASSIGNING . -person = 'Russell Sprout'. -salary = 1300. APPEND INITIAL LINE TO gs_sheet1-table1 ASSIGNING . -person = 'Fergus Douchebag'. -salary = 3000. APPEND INITIAL LINE TO gs_sheet1-table1 ASSIGNING . -person = 'Bartholomew Shoe'. -salary = 100. gs_sheet1-price = 14003. APPEND INITIAL LINE TO gs_sheet1-table2 ASSIGNING . -carrid = 'AC'. -price = 1222. APPEND INITIAL LINE TO -line1 ASSIGNING . -carrid = 'AC'. -connid = '0820'. -fldate = '20021220'. -price = 1222. APPEND INITIAL LINE TO gs_sheet1-table2 ASSIGNING . -carrid = 'AF'. -price = 2222. APPEND INITIAL LINE TO -line1 ASSIGNING . -carrid = 'AF'. -connid = '0820'. -fldate = '20021223'. -price = 2222. APPEND INITIAL LINE TO gs_sheet1-table2 ASSIGNING . -carrid = 'LH'. -price = 9488. APPEND INITIAL LINE TO -line1 ASSIGNING . -carrid = 'LH'. -connid = '0400'. -fldate = '19950228'. -price = 899. APPEND INITIAL LINE TO -line1 ASSIGNING . -carrid = 'LH'. -connid = '0400'. -fldate = '19951117'. -price = 1499. APPEND INITIAL LINE TO -line1 ASSIGNING . -carrid = 'LH'. -connid = '0400'. -fldate = '19950606'. -price = 1090. APPEND INITIAL LINE TO -line1 ASSIGNING . -carrid = 'LH'. -connid = '0400'. -fldate = '19950428'. -price = 6000. APPEND INITIAL LINE TO -line1 ASSIGNING . -carrid = 'LH'. -connid = '0400'. -fldate = '20021221'. -price = 222. APPEND INITIAL LINE TO gs_sheet1-table2 ASSIGNING . -carrid = 'SQ'. -price = 849. APPEND INITIAL LINE TO -line1 ASSIGNING . -carrid = 'SQ'. -connid = '0026'. -fldate = '19950228'. -price = 849. MOVE-CORRESPONDING gs_sheet1 TO gs_sheet2. gs_sheet2-table3 = gs_sheet1-table1. ENDFORM. FORM generate_file. DATA: lo_excel TYPE REF TO zcl_excel, lo_reader TYPE REF TO zif_excel_reader, lo_root TYPE REF TO cx_root. TRY. * prepare data CREATE OBJECT lo_data. lo_data->add( iv_sheet = 'Sheet1' iv_data = gs_sheet1 ). lo_data->add( iv_sheet = 'Sheet2' iv_data = gs_sheet2 ). * create reader CREATE OBJECT lo_reader TYPE zcl_excel_reader_2007. * load template IF p_file IS NOT INITIAL. lo_excel = lo_reader->load_file( p_fpath ). ELSE. PERFORM load_smw0 USING lo_reader p_objid CHANGING lo_excel. ENDIF. * merge data with template lo_excel->fill_template( lo_data ). *** Create output lcl_output=>output( lo_excel ). CATCH cx_root INTO lo_root. MESSAGE lo_root TYPE 'I' DISPLAY LIKE 'E'. ENDTRY. ENDFORM. *&---------------------------------------------------------------------* *& Form Get_file_path *&---------------------------------------------------------------------* FORM get_file_path CHANGING cv_path TYPE string. DATA: lv_rc TYPE i, lv_user_action TYPE i, lt_file_table TYPE filetable, ls_file_table LIKE LINE OF lt_file_table. CLEAR cv_path. cl_gui_frontend_services=>file_open_dialog( EXPORTING window_title = 'select template xlsx' multiselection = '' default_extension = '*.xlsx' file_filter = 'Text file (*.xlsx)|*.xlsx|All (*.*)|*.*' CHANGING file_table = lt_file_table rc = lv_rc user_action = lv_user_action EXCEPTIONS OTHERS = 1 ). IF sy-subrc = 0. IF lv_user_action = cl_gui_frontend_services=>action_ok. IF lt_file_table IS NOT INITIAL. READ TABLE lt_file_table INTO ls_file_table INDEX 1. IF sy-subrc = 0. cv_path = ls_file_table-filename. ENDIF. ENDIF. ENDIF. ENDIF. ENDFORM. " Get_file_path FORM load_smw0 USING io_reader TYPE REF TO zif_excel_reader iv_w3objid TYPE w3objid CHANGING ro_excel TYPE REF TO zcl_excel RAISING zcx_excel. DATA: lv_excel_data TYPE xstring, lt_mime TYPE TABLE OF w3mime, ls_key TYPE wwwdatatab, lv_errormessage TYPE string, lv_filesize TYPE i, lv_filesizec TYPE c LENGTH 10. *--------------------------------------------------------------------* * Read file into binary string *--------------------------------------------------------------------* ls_key-relid = 'MI'. ls_key-objid = iv_w3objid . CALL FUNCTION 'WWWDATA_IMPORT' EXPORTING key = ls_key TABLES mime = lt_mime EXCEPTIONS OTHERS = 1. IF sy-subrc <> 0. lv_errormessage = 'A problem occured when reading the MIME object'(004). zcx_excel=>raise_text( lv_errormessage ). ENDIF. CALL FUNCTION 'WWWPARAMS_READ' EXPORTING relid = ls_key-relid objid = ls_key-objid name = 'filesize' IMPORTING value = lv_filesizec. lv_filesize = lv_filesizec. CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' EXPORTING input_length = lv_filesize IMPORTING buffer = lv_excel_data TABLES binary_tab = lt_mime EXCEPTIONS failed = 1 OTHERS = 2. *--------------------------------------------------------------------* * Parse Excel data into ZCL_EXCEL object from binary string *--------------------------------------------------------------------* ro_excel = io_reader->load( i_excel2007 = lv_excel_data ). ENDFORM.