EXCEL BDS模板上传及赋值
上传模板事务代码:OAER
l 功能代码:向EXCEL模板中写入数据示例代码如下
REPORT ZEXCEL_DOI.
“doi type pools
TYPE-POOLS: soi.
*SAP Desktop Office Integration Interfaces
DATA: container TYPE REF TO cl_gui_custom_container,control TYPE REF TO i_oi_container_control,document TYPE REF TO i_oi_document_proxy,spreadsheet TYPE REF TO i_oi_spreadsheet,error TYPE REF TO i_oi_error,errors TYPE REF TO i_oi_error OCCURS 0 WITH HEADER LINE.
*dynpro fields
CONTROLS: exceldata TYPE TABLEVIEW USING SCREEN 0100.
DATA: tablename(10),ok_code(15),save_ok(15),row(4),column(4),data(39).
*spreadsheet interface structures of the selected R/3 table
DATA: cellitem TYPE soi_generic_item,rangeitem TYPE soi_range_item,ranges TYPE soi_range_list,excel_input TYPE soi_generic_table,excel_input_wa TYPE soi_generic_item,initialized(1),retcode TYPE soi_ret_string.
*screen fields
SET SCREEN 100.
INCLUDE ZEXCEL_DOI_STATUS_0100O01.
*&———————————————————————*
*& Module USER_COMMAND_0100 INPUT
*&———————————————————————*
* text
*———————————————————————-*
MODULE USER_COMMAND_0100 INPUT.save_ok = ok_code.CLEAR ok_code.CASE save_ok.WHEN ‘BACK’.SET SCREEN 0.WHEN ‘OUTPUT’.DATA: fields_table TYPE TABLE OF rfc_fields,tabname TYPE x030l-tabname,ret TYPE i VALUE 0.CHECK NOT tablename IS INITIAL.tabname = tablename.DATA: dref TYPE REF TO data,dref_it TYPE REF TO data,len_table TYPE i,len_excel_len TYPE i.FIELD-SYMBOLS: <tablewa> TYPE any,<it_table> TYPE any table.CATCH SYSTEM-EXCEPTIONS create_data_unknown_type = 1OTHERS = 2.CREATE DATA dref TYPE (tabname).CREATE DATA dref_it TYPE TABLE OF (tabname).ENDCATCH.IF sy-subrc <> 0.EXIT.ENDIF.ASSIGN: dref->* TO <tablewa>,dref_it->* TO <it_table>.DATA: count TYPE i.CLEAR count.DATA: BEGIN OF la_row,row TYPE c LENGTH 500,END OF la_row,excel_table LIKE TABLE OF la_row,myrow TYPE c LENGTH 500.REFRESH excel_table.SELECT * INTO TABLE <it_table>FROM (tabname).“create an excel range for dataDATA: rows_number TYPE i,columns_number TYPE i.CALL FUNCTION ‘RFC_GET_STRUCTURE_DEFINITION’EXPORTINGtabname = tabnameTABLESfields = fields_tableEXCEPTIONStable_not_active = 1OTHERS = 2.DESCRIBE TABLE fields_table LINES columns_number.DESCRIBE TABLE <it_table> LINES rows_number.CALL METHOD spreadsheet->insert_range_dimEXPORTING name = ‘SAP_Table’top = ‘2′left = ‘2′rows = rows_numberno_flush = ‘X’columns = columns_numberIMPORTING error = errors.APPEND errors.CALL METHOD spreadsheet->set_colorEXPORTING rangename = ‘SAP_Table’back = ‘50′front = ‘2′no_flush = ‘X’IMPORTING error = errors.APPEND errors.“Create range for commentsCALL METHOD spreadsheet->insert_range_dimEXPORTING name = ‘SAP_Comments’top = ‘2′left = ‘1′rows = rows_numberno_flush = ‘X’columns = 1IMPORTING error = errors.APPEND errors.CALL METHOD spreadsheet->set_colorEXPORTING rangename = ‘SAP_Comments’back = ‘25′front = ‘2′no_flush = ‘X’IMPORTING error = errors.APPEND errors.DATA: title TYPE c LENGTH 30.REFRESH:ranges,excel_input.rangeitem-name = ‘SAP_Comments’.rangeitem-columns = 1.rangeitem-rows = 1.APPEND rangeitem TO ranges.excel_input_wa-column = 1.excel_input_wa-row = 1.CONCATENATE ‘R/3 table’ tabname ‘- comments’ INTO titleSEPARATED BY space.excel_input_wa-value = title.APPEND excel_input_wa TO excel_input.CALL METHOD spreadsheet->set_ranges_dataEXPORTING ranges = rangescontents = excel_inputIMPORTING error = errors.APPEND errors.“transfer R/3 table data into excel rangeCALL METHOD spreadsheet->insert_one_tableEXPORTING data_table = <it_table>ddic_name = tabnamerangename = ‘SAP_Table’no_flush = ‘X’wholetable = ‘X’IMPORTING error = errors.APPEND errors.“protect the R/3 data against inputCALL METHOD spreadsheet->fit_widestEXPORTING name = spaceno_flush = ‘X’.CALL METHOD spreadsheet->protect_rangeEXPORTING name = ‘SAP_Table’protect = ‘X’no_flush = ‘X’IMPORTING error = errors.APPEND errors.“no flush automation queueCALL METHOD control->set_focusEXPORTING no_flush = ‘ ’IMPORTING error = errors.APPEND errors.LOOP AT errors.CALL METHOD errors->raise_messageEXPORTING type = ‘E’.ENDLOOP.FREE errors.REFRESH:excel_input,ranges.CLEAR :excel_input_wa,rangeitem.ENDCASE.
ENDMODULE. “ USER_COMMAND_0100 INPUT
*&———————————————————————*
*& Module CREATE_BASIC_OBJECTS OUTPUT
*&———————————————————————*
* text
*———————————————————————-*
MODULE CREATE_BASIC_OBJECTS OUTPUT.CHECK initialized IS INITIAL.“first get doi i_oi_container_control interfaceCALL METHOD c_oi_container_control_creator=>get_container_controlIMPORTING control = controlerror = error.“check no errors occuredCALL METHOD error->raise_message EXPORTING type = ‘E’.“create a control container as defined in dynpro 0100CREATE OBJECT containerEXPORTING container_name = ‘CONTAINER’.“initialize the SAP DOI container,tell it to run in the container“specified above and tell it to run excel inplaceCALL METHOD control->init_controlEXPORTING r3_application_name = ‘R/3 Basis’inplace_enabled = ”inplace_scroll_documents = ”parent = containerregister_on_close_event = ‘X’register_on_custom_event = ‘X’no_flush = ‘X’IMPORTING error = errors.
* save error object in collectionAPPEND errors.
* ask the SAP DOI container for a i_oi_document_proxy for ExelCALL METHOD control->get_document_proxyEXPORTING document_type = ‘Excel.Sheet’no_flush = ‘X’IMPORTING document_proxy = documenterror = errors.
*and then create a new Excel SheetCALL METHOD document->create_documentEXPORTING open_inplace = ”document_title = ‘R/3 table contents in Excel’no_flush = ‘X’IMPORTING error = errors.APPEND errors.
* check if our document proxy can serve a spread sheet interfaceDATA: has TYPE i.CALL METHOD document->has_spreadsheet_interfaceEXPORTING no_flush = ‘X’IMPORTING is_available = haserror = errors.APPEND errors.CALL METHOD document->get_spreadsheet_interfaceEXPORTING no_flush = ‘ ’IMPORTING sheet_interface = spreadsheeterror = errors.APPEND errors.
* now loop through error collection because
* Get_spreadsheet_interface flushed and synchronized
* the automation queue !LOOP AT errors.CALL METHOD errors->raise_messageEXPORTING type = ‘E’.ENDLOOP.initialized = ‘X’.
ENDMODULE. “ CREATE_BASIC_OBJECTS OUTPUT
l 打开excel模板示例代码如下:METHODS: open_excel_template IMPORTING im_clsnam TYPE sbdst_classname im_clstyp TYPE sbdst_classtype im_objkey TYPE sbdst_object_key im_desc TYPE char255, “打开报表模板
*———————————————-*
* METHOD open_excel_template *
* 打开excel模板
*———————————————-*METHOD open_excel_template.DATA: locint_signature TYPE sbdst_signature,locint_uris TYPE sbdst_uri,locwa_signature LIKE LINE OF locint_signature,locwa_uris LIKE LINE OF locint_uris.
* Create object for cl_bds_document_setCREATE OBJECT r_document.
* Get Document with URLlocwa_signature-prop_name = ‘DESCRIPTION’.
* Description of the table template in OAORlocwa_signature-prop_value = im_desc.APPEND locwa_signature TO locint_signature.CALL METHOD r_document->get_with_urlEXPORTINGclassname = im_clsnamclasstype = im_clstypobject_key = im_objkeyCHANGINGuris = locint_urissignature = locint_signatureEXCEPTIONSnothing_found = 1error_kpro = 2internal_error = 3parameter_error = 4not_authorized = 5not_allowed = 6.IF sy-subrc NE 0.MESSAGE ‘Error Retrieving Document’ TYPE ‘E’.ENDIF.
* Create container controlCALL METHOD c_oi_container_control_creator=>get_container_controlIMPORTINGcontrol = r_controlerror = r_error.IF r_error->has_failed = ‘X’.CALL METHOD c_oi_errors=>raise_messageEXPORTINGtype = ‘E’.ENDIF.
* Initialize Custom ControlCREATE OBJECT r_containerEXPORTINGcontainer_name = ‘TAB_EXCEL’. “Custom Control NameCALL METHOD r_control->init_controlEXPORTINGr3_application_name = ‘EXCEL INPLACE BDS’inplace_enabled = abap_falseinplace_scroll_documents = abap_trueparent = r_containerIMPORTINGerror = r_error.IF r_error->has_failed = ‘X’.CALL METHOD c_oi_errors=>raise_messageEXPORTINGtype = ‘E’.ENDIF.
* Create object for cl_bds_document_setCREATE OBJECT r_document.READ TABLE locint_uris INTO locwa_uris INDEX 1.CALL METHOD r_control->get_document_proxyEXPORTINGdocument_type = ‘Excel.Sheet’IMPORTINGdocument_proxy = r_proxyerror = r_error.IF r_error->has_failed = ‘X’.CALL METHOD c_oi_errors=>raise_messageEXPORTINGtype = ‘E’.ENDIF.
* Open DocumentCALL METHOD r_proxy->open_documentEXPORTINGdocument_url = locwa_uris-uriopen_inplace = abap_falseprotect_document = abap_false “Protect Document initiallyIMPORTINGerror = r_error.IF r_error->has_failed = ‘X’.CALL METHOD c_oi_errors=>raise_messageEXPORTINGtype = ‘E’.ENDIF.
* Get Excel InterfaceCALL METHOD r_proxy->get_spreadsheet_interfaceIMPORTINGsheet_interface = r_excelerror = r_error.IF r_error->has_failed = ‘X’.CALL METHOD c_oi_errors=>raise_messageEXPORTINGtype = ‘E’.ENDIF.CALL METHOD r_proxy->get_spreadsheet_interfaceIMPORTINGsheet_interface = r_handle.ENDMETHOD.
ENDCLASS.
l 逐个CELL输入数据的示例代码如下:fill_cell IMPORTING im_x TYPE iim_y TYPE iim_value TYPE char255, “填充单元格
———————————————-*
* METHOD fill_cell *
* 向excel的单元格中插入数据
*———————————————-*METHOD fill_cell.DATA: lv_columns TYPE i,lv_rows TYPE i.lv_columns = 1.lv_rows = 1.CALL METHOD r_handle->insert_range_dimEXPORTINGNAME = ‘cell’NO_FLUSH = ‘X’TOP = im_xLEFT = im_yROWS = lv_rowsCOLUMNS = lv_columnsIMPORTINGerror = r_error.DATA: lt_ran TYPE soi_range_list,lt_cont TYPE soi_generic_table,la_ran LIKE LINE OF lt_ran,la_cont LIKE LINE OF lt_cont.la_ran-name = ‘cell’.la_ran-columns = lv_columns.la_ran-rows = lv_rows.APPEND la_ran TO lt_ran.la_cont-column = 1.la_cont-row = 1.la_cont-value = im_value.APPEND la_cont TO lt_cont.CALL METHOD r_handle->set_ranges_dataEXPORTING ranges = lt_rancontents = lt_contIMPORTINGerror = r_error.CALL METHOD r_handle->fit_widestEXPORTINGname = spaceno_flush = ‘X’.ENDMETHOD.