ABAP DOI详解(3)
這一篇解決上一篇的幾個遺留問題:
- 如何根據屏幕大小讓Excel自適應
- Excel單個單元格寫入
- 設置Excel屬性
- 錯誤處理
如何根據屏幕大小讓Excel自適應
Dialog screen中的custom control大小是固定的,比較難看。如果想讓Excel自適應變更大小,要用cl_gui_container類。
增加一個新的子例程:
data: gr_container type ref to cl_gui_container,gr_splitter type ref to cl_gui_splitter_container,...form get_dynamic_container.create object gr_splitterexportingparent = cl_gui_container=>screen0rows = 1columns = 1 .call method gr_splitter->set_borderexportingborder = cl_gui_cfw=>false.gr_container = gr_splitter->get_container( row = 1 column = 1 ). endform.- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
同樣地,Container?control初始化的時候,設定parent為gr_container :
form create_container_control. * create container controlcall method c_oi_container_control_creator=>get_container_controlimportingcontrol = gr_control.* initialize controlcall method gr_control->init_controlexportinginplace_enabled = 'X 'inplace_scroll_documents = 'X'register_on_close_event = 'X'register_on_custom_event = 'X'r3_application_name = 'DOI demo by Stone Wang'parent = gr_container. endform.- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
這樣,Excel就能根據屏幕變更大小,是不是漂亮多了呢?
Excel單個單元格寫入
單個單元格寫入的方法,同批量寫入一樣,使用i_oi_spreadsheet接口的set_range_dim方法和set_range_data方法。區別在于range只包含一行一列:
form write_single_cell using p_row p_col p_value. * define internal table for ranges and contents parametersdata: lt_ranges type soi_range_list,ls_rangeitem type soi_range_item,lt_contents type soi_generic_table,ls_content type soi_generic_item.* populate rangesclear ls_rangeitem.clear lt_ranges[].ls_rangeitem-name = 'cell' .ls_rangeitem-columns = 1.ls_rangeitem-rows = 1.ls_rangeitem-code = 4.append ls_rangeitem to lt_ranges.* populate contentsclear ls_content.clear lt_contents[].ls_content-column = 1.ls_content-row = 1.ls_content-value = p_value.append ls_content to lt_contents.* 每次只寫一行一列call method gr_spreadsheet->insert_range_dimexportingname = 'cell'no_flush = 'X'top = p_rowleft = p_colrows = 1columns = 1.call method gr_spreadsheet->set_ranges_dataexportingranges = lt_rangescontents = lt_contentsno_flush = 'X'. endform.- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
假如我們要把gt_spfli的數據寫入Excel,可以這樣:
form write_itab_to_excel_singlecell.data: row_index type i.check not gt_spfli is initial.clear gs_spfli.loop at gt_spfli into gs_spfli.row_index = sy-tabix + 1.perform write_single_cell using row_index 1 gs_spfli-carrid.perform write_single_cell using row_index 2 gs_spfli-connid.perform write_single_cell using row_index 3 gs_spfli-cityfrom.perform write_single_cell using row_index 4 gs_spfli-cityto.clear gs_spfli.endloop.row_index = row_index + 1.perform write_single_cell using row_index 1 sy-uname.perform write_single_cell using row_index 2 sy-datum.endform.- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
設置Excel屬性
我們剛剛把spfli的數據寫入到Excel,假設我們想把工作表的名稱改為spfli,并且將數據(注意我們寫入的時候定義了range name為cell)區設置邊框,列大小根據數據自適應我們可以這樣:
form set_excel_attributes. * change name of Sheet1 to 'spfli'call method gr_spreadsheet->set_sheet_nameexportingnewname = 'spfli'oldname = 'Sheet1'.* set border line for rangecall method gr_spreadsheet->set_frameexportingrangename = 'cell'typ = '127'color = '1'no_flush = 'X'.* auto fitcall method gr_spreadsheet->fit_widestexportingname = spaceno_flush = 'X'. endform.- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
i_oi_spreadsheet接口的方法和如何使用這些方法,請參考幫助:Using the spreadsheet interface。
錯誤處理
與Office集成的錯誤是非常復雜的,因為涉及外部資源的通訊和處理。調用i_oi_spreadsheet接口的方法后,必須弄清楚調用是否成功。前面幾篇的演示代碼故意沒有涉及錯誤的處理,是為了讓代碼更容易理解。每一個方法都有一個ret_code參數,如果有錯誤,返回錯誤碼,沒有錯誤則返回OK。在程序中,開發人員可以用c_oi_errors類中定義的常量來代表這些錯誤碼或者沒有錯誤的OK。請移步至SAP help了解這些常量:Error Messages and Their Meanings。
在程序中,通常有兩種方法來處理錯誤,第一種方法:使用c_oi_errors的靜態方法raise_message簡單地顯示相關的錯誤:
CALL METHOD C_OI_ERRORS=>RAISE_MESSAGE EXPORTING TYPE = type- 1
- 2
- 1
- 2
type可以是A/E/W/I/S。
第二種方法是區分不同的錯誤,給用戶一個更明確的提示:
IF ret_code EQ c_oi_errors=>ret_ok." Document opened successfully ELSEIF ret_code EQ c_oi_errors=> ret_document_already_open." Special error handling, e.g. dialog box. ELSE. CALL METHOD c_oi_errors=>raise_message EXPORTING type = 'E'. ENDIF.- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
因為Excel操作多個步驟,為了在過程中間減少對用戶的干擾,也可以把ret_code返回的錯誤碼先儲存在內表中,集中處理:
DATA: errors TYPE REF TO i_oi_error OCCURS 0 WITH HEADER LINE.# DOI processing CALL METHOD control->get_link_serverEXPORTING server_type = server_typeno_flush = 'X'IMPORTING link_server = link_serverretcode = retcodeerror = errors. APPEND errors.LOOP AT errors. CALL METHOD errors->raise_messageEXPORTING type = 'E'EXCEPTIONS message_raised = 1flush_failed = 2. ENDLOOP. FREE errors.總結
以上是生活随笔為你收集整理的ABAP DOI详解(3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ABAP DOI详解(2)
- 下一篇: MSEG和EKBE的区别在哪里