ABAP DOI详解(3)
這一篇解決上一篇的幾個(gè)遺留問(wèn)題:
- 如何根據(jù)屏幕大小讓Excel自適應(yīng)
- Excel單個(gè)單元格寫(xiě)入
- 設(shè)置Excel屬性
- 錯(cuò)誤處理
如何根據(jù)屏幕大小讓Excel自適應(yīng)
Dialog screen中的custom control大小是固定的,比較難看。如果想讓Excel自適應(yīng)變更大小,要用cl_gui_container類(lèi)。
增加一個(gè)新的子例程:
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初始化的時(shí)候,設(shè)定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就能根據(jù)屏幕變更大小,是不是漂亮多了呢?
Excel單個(gè)單元格寫(xiě)入
單個(gè)單元格寫(xiě)入的方法,同批量寫(xiě)入一樣,使用i_oi_spreadsheet接口的set_range_dim方法和set_range_data方法。區(qū)別在于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.* 每次只寫(xiě)一行一列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的數(shù)據(jù)寫(xiě)入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
設(shè)置Excel屬性
我們剛剛把spfli的數(shù)據(jù)寫(xiě)入到Excel,假設(shè)我們想把工作表的名稱(chēng)改為spfli,并且將數(shù)據(jù)(注意我們寫(xiě)入的時(shí)候定義了range name為cell)區(qū)設(shè)置邊框,列大小根據(jù)數(shù)據(jù)自適應(yīng)我們可以這樣:
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接口的方法和如何使用這些方法,請(qǐng)參考幫助:Using the spreadsheet interface。
錯(cuò)誤處理
與Office集成的錯(cuò)誤是非常復(fù)雜的,因?yàn)樯婕巴獠抠Y源的通訊和處理。調(diào)用i_oi_spreadsheet接口的方法后,必須弄清楚調(diào)用是否成功。前面幾篇的演示代碼故意沒(méi)有涉及錯(cuò)誤的處理,是為了讓代碼更容易理解。每一個(gè)方法都有一個(gè)ret_code參數(shù),如果有錯(cuò)誤,返回錯(cuò)誤碼,沒(méi)有錯(cuò)誤則返回OK。在程序中,開(kāi)發(fā)人員可以用c_oi_errors類(lèi)中定義的常量來(lái)代表這些錯(cuò)誤碼或者沒(méi)有錯(cuò)誤的OK。請(qǐng)移步至SAP help了解這些常量:Error Messages and Their Meanings。
在程序中,通常有兩種方法來(lái)處理錯(cuò)誤,第一種方法:使用c_oi_errors的靜態(tài)方法raise_message簡(jiǎn)單地顯示相關(guān)的錯(cuò)誤:
CALL METHOD C_OI_ERRORS=>RAISE_MESSAGE EXPORTING TYPE = type- 1
- 2
- 1
- 2
type可以是A/E/W/I/S。
第二種方法是區(qū)分不同的錯(cuò)誤,給用戶(hù)一個(gè)更明確的提示:
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
因?yàn)镋xcel操作多個(gè)步驟,為了在過(guò)程中間減少對(duì)用戶(hù)的干擾,也可以把ret_code返回的錯(cuò)誤碼先儲(chǔ)存在內(nèi)表中,集中處理:
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.總結(jié)
以上是生活随笔為你收集整理的ABAP DOI详解(3)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ABAP DOI详解(2)
- 下一篇: MSEG和EKBE的区别在哪里