ABAP學習(8):操做EXCEL

ABAP操做EXCEL

一、EXCEL導入內表

方式1:服務器

    調用function,’F4_FILENAME’獲取文件名。經過屏幕元素Prameter的幫助事件,彈出文件選擇框,獲取選擇文件名。app

    調用function,’TEXT_CONVERT_XLS_TO_SAP’,將選擇excel數據放入內表。frontend

示例:ide

TYPE-POOLS truxs. DATA:it_raw TYPE truxs_t_text_data. "定義一個內表來存儲數據,內表的列數和要傳得數據的列數要相同,其按照列來匹配傳值

DATA: BEGIN OF gt_data OCCURS 0, col1 TYPE char10, col2 TYPE char10, END OF gt_data. PARAMETERS:p_file TYPE rlgrap-filename. "調用F4_FILENAME,點擊輸入框後小方塊彈出文件選擇框

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. CLEAR p_file. CALL FUNCTION 'F4_FILENAME'

    IMPORTING file_name = p_file."返回文件名

 

*Excel傳值

START-OF-SELECTION. PERFORM exceltotab. FORM exceltotab . CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

  EXPORTING

* I_FIELD_SEPERATOR = ' ' "分隔符?
 i_line_header = 'X'         "是否去掉首行
 i_tab_raw_data = it_raw     "WORK TABLE
 i_filename = p_file TABLES i_tab_converted_data = gt_data[] "ACTUAL DATA

  EXCEPTIONS conversion_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. ENDFORM.                    "exceltotab
View Code

 

方式2測試

直接彈出文件選擇彈窗,獲取文件路徑:字體

data: ts_files type filetable, rc type i. refresh ts_files. DATA:filename(1024) type C. "調用方法,彈出文件選擇框,獲取選中文件路徑
 call method cl_gui_frontend_services=>file_open_dialog exporting window_title = 'Select File' default_extension = '*.xls' multiselection = ' '    " Only one file
 changing file_table = ts_files rc = rc exceptions file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_supported_by_gui = 4 others = 5. read table ts_files into filename index 1.

調用function ‘ALSM_EXCEL_TO_INTERNAL_TABLE’,將excel表內容讀入ABAP內表。ui

"將excel讀取到內表'ALSM_EXCEL_TO_INTERNAL_TABLE'

DATA :excelTab like alsmex_tabline OCCURS 10 WITH HEADER LINE. Data :fileTest like rlgrap-filename VALUE 'D:\myexcel.xlsx'. call function 'ALSM_EXCEL_TO_INTERNAL_TABLE' exporting filename = fileTest i_begin_col = 1 i_begin_row = 1 i_end_col = 20 i_end_row = 20000 tables intern = excelTab exceptions inconsistent_parameters = 1 upload_ole = 2 others = 3.

 

自定義function:‘ALSM_EXCEL_TO_INTERNAL_TABLE’url

 

Structure:ALSMEX_TABLINE,其中VALUE字段類型爲CHAR50,若是上傳字段長度超過CHAR50,那麼上傳數據就會不全,因此咱們須要自定義Function,修改ALSMEX_TABLINE的VALUE字段。spa

自定義步驟:excel

1.複製Sturcture:ALSMEX_TABLINE,自定義名字TEST_ALSMEX_TABLINE修改VALUE的數據類型,定義爲需求長度;

2.複製function:ALSM_EXCEL_TO_INTERNAL_TABLE,複製以前先建立目標Function group,否則會複製失敗;

 

 

3.在新建Function Group建立include: LZTEST_EXCELF01,將ALSMEX下,includes:LALSMEXF01內容複製到新建include。

 

 

 4.將ALSMEX下,includes:LALSMEXTOP內容複製到新建Function Group的includes:LZTEST_EXCELTOP中,而後檢查,激活。

示例:

* value of excel-cell
TYPES: ty_d_itabvalue             TYPE alsmex_tabline-value, * internal table containing the excel data
       ty_t_itab                  TYPE alsmex_tabline   OCCURS 0,

5.修改TEST_EXCEL_TO_INTERNAL_TABLE,將INTERN參數對應數據類型修改成自定義的TEST_ALSMEX_TABLINE,將LZTEST_EXCELTOP中的ty_d_itabvalue和ty_t_itab對應的數據類型也修改成自定義類型。

 

6.程序中調用function便可。

示例:

"將excel讀取到內表'ALSM_EXCEL_TO_INTERNAL_TABLE'
  DATA :exceltab like alsmex_tabline OCCURS 10 WITH HEADER LINE. Data :filetest like rlgrap-filename VALUE 'D:test.xlsx'. * CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
  CALL FUNCTION 'TEST_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING filename = fileTest i_begin_col = 1 i_begin_row = 1 i_end_col = 20 i_end_row = 20000
    TABLES intern = exceltab EXCEPTIONS inconsistent_parameters = 1 upload_ole = 2 others = 3.

 

二、OLE2操做EXCEL

使用ABAP程序操做EXCEL,將內表數據寫出到EXCEL

OLE2方式操做EXCEL,OLE是將全部EXCEL中組成結構封裝成一個ole2的類。經過ole2對象方法來操做Excel。

一、類對象:lo_application:一個Excel;

經過create object語句建立一個excel對象,

CREATE OBJECT lo_application 'Excel.Application'.  "建立excel對象

設置EXCEL的屬性,是否可見:

SET PROPERTY OF lo_application 'Visible' = 1.  "設置excel可見

SET PROPERTY OF lo_application 'SheetsInNewWorkbook' = 1.

 

二、類對象:lo_workbooks :工做簿集合;

經過建立的excel對象,調用Workbooks方法,建立他的工做薄集,

CALL METHOD OF lo_application 'Workbooks'  = lo_workbooks. "調用lo_application的'Workbooks'建立工做簿集

 

三、類對象:lo_workbook:工做簿;

經過建立的工做薄集對象,調用add方法,建立他的工做薄,

CALL METHOD OF lo_workbooks   'Add'        = lo_workbook.  "調用lo_workbooks 的方法'Add' ,建立工做薄

 

四、類對象:lo_worksheets:工做表,sheet集合;

    經過EXCEL對象的Sheets方法,建立sheet集合對象

"建立一個sheet集合

CALL METHOD OF lo_application 'Sheets' = lo_worksheets . 經過sheet集合對象,調用add方法,能夠建立多個sheet對象。建立出的sheet對象調用Activate方法,能夠激活。 "建立新的work sheet. "默認建立的最新的sheet,處於active狀態

CALL METHOD OF lo_worksheets 'Add' = new_worksheet. *CALL METHOD OF new_worksheet 'Activate'. "激活當前新的sheet

 

五、類對象:lo_worksheet:工做表,sheet;

    經過EXCEL對象的Activesheet屬性,能夠獲取到當前活動的sheet,這個sheet就是建立初始時,默認存在的sheet。

GET PROPERTY OF lo_application 'ACTIVESHEET' = lo_worksheet.  "獲取當前活動sheet,初始建立時,只有一個sheet,且active

     設置sheet的名稱,設置sheet的屬性name:

SET PROPERTY OF lo_worksheet 'Name' = 'first sheet'. "設置sheet名

 

六、類對象:lo_cell:excel單元格;

    經過sheet對象,調用方法Cells方法,在當前sheet中建立單元格對象。

"建立單元個

CALL METHOD OF lo_worksheet 'Cells' = lo_cell EXPORTING # = 1  "Row
 # = 1. "Column

    設置單元格對象屬性value,爲單元格賦值:

SET PROPERTY OF lo_cell 'value' = ''.   "設置單元個值

   設置單元格對象屬性NumberFormat,設置單元格數據格式:

"設置單元格值格式,支持excel中的格式

*SET PROPERTY OF lo_cell 'NumberFormat' = '0.00'.

SET PROPERTY OF lo_cell 'NumberFormat' = 'm/d/yy'.

  設置單元格自動換列

"特別長文字,自動換列,看到所有文字

 SET PROPERTY OF lo_cell 'WrapText' = 1.

  設置水平對齊方式

"設置水平對齊方式

"center = -4108

"left = -4131

"right = -4152

"top = -4160

SET PROPERTY OF lo_cell 'HorizontalAlignment' = -4108.

類對象:lo_font:字體設置對象,調用單元格對象的font方法,獲取font對象,經過font對象設置單元格字體。

"設置單元格字體

CALL METHOD OF lo_cell 'font' = lo_font. "EXCEL支持的字體名

*SET PROPERTY OF lo_font 'Name' = 'Agency FB'. "字體名

SET PROPERTY OF lo_font 'Name' = '微軟正黑體'.   "字體名

SET PROPERTY OF lo_font 'Size' = 15.        "大小

"顏色數字規則?

SET PROPERTY OF lo_font 'Color' = -12123441. "顏色

SET PROPERTY OF lo_font 'TintAndShade' = 0. "明亮度-1~1,1徹底透明

SET PROPERTY OF lo_font 'Bold' = 0.  "是否粗體字 0 :false 1:true

SET PROPERTY OF lo_font 'Italic' = 1. "是否斜體 0:false 1:true

SET PROPERTY OF lo_font 'Underline' = 2. "xlUnderlineStyleSingle = 2,下劃線

類對象lo_interior,單元格背景;

示例:

經過單元格對象獲取lo_interior對象,設置Color或者ColorIndex屬性,設置背景顏色,兩種方式均可。對應顏色值不清楚?

"設置單元格背景

DATA:lo_interior TYPE ole2_object. CALL METHOD OF lo_cell 'interior' = lo_interior. SET PROPERTY OF lo_interior 'Color' = 12123441. SET PROPERTY OF lo_interior 'ColorIndex' = 43.

 

八、類對象:lo_range:多個單元格集合;

選擇多個單元格集合。

示例:

選擇單元格(1,1)到(3,3)這部分區域:

"選擇Ranges of cells

CALL METHOD OF lo_worksheet 'Cells' = lo_cellstart EXPORTING # = 1 "row
 # = 1. "column

CALL METHOD OF lo_worksheet 'Cells' = lo_cellend EXPORTING # = 3 "row
 # = 3. "column

"選中一個區域Cell

CALL METHOD OF lo_worksheet 'RANGE' = lo_range EXPORTING # = lo_cellstart # = lo_cellend.

也能夠選中一行或者一列單元格:

類對象:lo_column:excel行

"選擇列

CALL METHOD OF lo_worksheet 'Columns' = lo_column EXPORTING # = 1.

類對象:lo_row:excel列

"選擇行

CALL METHOD OF lo_worksheet 'Row' = lo_row EXPORTING # = 2.

若是要操做選擇部分區域須要lo_selection對象

類對象:lo_selection,選中操做對象

"獲取選中區域selection

CALL METHOD OF lo_row 'Select'. CALL METHOD OF lo_application 'selection' = lo_selection.

 

九、類對象:lo_validation:驗證操做validation對象;

示例:測試失敗?

"添加驗證

"選中(4,4)

CALL METHOD OF lo_worksheet 'Cells' = lo_cell EXPORTING # = 4 # = 4. "獲取選中區域selection

CALL METHOD OF lo_cell 'Select'. CALL METHOD OF lo_application 'selection' = lo_selection. "獲取驗證對象lo_validation

CALL METHOD OF lo_selection 'Validation' = lo_validation. "添加驗證規則

CALL METHOD OF lo_validation 'Add'

  EXPORTING #1 = 4 "驗證類型Type 4 = xlValidateDate,驗證日期
 #2 = 1 "驗證彈窗類型AlertStype 1 = xlValidAlertStop
 #3 = 1 "操做類型,Operator 1 = xlBetween,在之間
 #4 = '1/1/2000'   "Formula1
 #5 = '1/1/2010'.  "Formula2

"設置錯誤信息

SET PROPERTY OF lo_validation 'ErrorMessage' = 'Enter a valid date'.

 

十、使用複製粘貼方式將內表數數據寫入EXCEL

示例:

"複製粘貼方式將數據寫入EXCEL

CALL METHOD OF lo_worksheet 'Activate'.   "激活當前新的sheet

 

DATA:test_spfli LIKE TABLE OF spfli WITH HEADER LINE. TYPES: ty_data(1500) TYPE c. DATA: lt_data TYPE ty_data OCCURS 0 WITH HEADER LINE. DATA:lv_cont TYPE I. FIELD-SYMBOLS: <field>  TYPE ANY. SELECT * FROM spfli INTO TABLE test_spfli. * Prepare the data before copy to clipboard;

LOOP AT test_spfli. lv_cont = 1. * Write for example 5 columns per row.

  DO 5 TIMES. ASSIGN COMPONENT lv_cont OF STRUCTURE test_spfli TO <field>. "使用#將數據連接起來,數據格式#xxx#xxxx#xxxx

    CONCATENATE lt_data <field> INTO lt_data SEPARATED BY cl_abap_char_utilities=>horizontal_tab. ADD 1 TO lv_cont. ENDDO. "去掉開始#號

  SHIFT lt_data BY 1 PLACES LEFT. APPEND lt_data. CLEAR lt_data. ENDLOOP. * Copy to clipboard into ABAP

CALL FUNCTION 'CONTROL_FLUSH'

  EXCEPTIONS OTHERS = 3. CALL FUNCTION 'CLPB_EXPORT'

  TABLES data_tab = lt_data EXCEPTIONS clpb_error = 1 OTHERS = 2. * Select the cell A1

CALL METHOD OF lo_worksheet 'Cells'      = lo_cell EXPORTING #1           = 1  "Row
 #2           = 1. "Column

 

* Paste clipboard from cell A1

CALL METHOD OF lo_cell 'SELECT'. CALL METHOD OF lo_worksheet 'PASTE'. "調用lo_workbook 'SaveAs' 方法保存excel

CALL METHOD OF lo_workbook 'SaveAs'

  EXPORTING # = lv_complete_path. if sy-subrc = 0. MESSAGE 'File downloaded successfully' TYPE 'S'. ELSE. MESSAGE 'Error downloading the file' TYPE 'E'. ENDIF.

完整示例:

TYPE-POOLS: soi,ole2. DATA: lo_application TYPE  ole2_object,  "表明excel對象
 lo_workbook TYPE  ole2_object,  "表明excel工做薄
 lo_workbooks TYPE  ole2_object,  "表明excel工做薄集合
 lo_range TYPE  ole2_object,  "表明多個單元格集合
 lo_worksheet TYPE ole2_object,  "表明excel工做表sheet
 lo_worksheets TYPE ole2_object,  "表明excel工做表sheet集合
 lo_column TYPE  ole2_object,  "表明excel列
 lo_row TYPE  ole2_object,  "表明excel行
 lo_cell TYPE  ole2_object,  "表明excel單元格
 lo_font TYPE  ole2_object.  "表明字體

 

DATA: lo_cellstart      TYPE ole2_object, lo_cellend TYPE ole2_object, lo_selection TYPE ole2_object, lo_validation TYPE ole2_object. DATA: lv_selected_folder TYPE string, lv_complete_path TYPE char256, lv_titulo TYPE string .  "彈窗描述

 

DATA: new_worksheet TYPE ole2_object. "新sheet

 

"調用文件路徑選擇框,默認路徑C:\

CALL METHOD cl_gui_frontend_services=>directory_browse EXPORTING window_title = lv_titulo   "彈窗最上方,能夠添加描述信息,不是彈窗標題
 initial_folder = 'C:\' "初始化選擇路徑 CHANGING selected_folder = lv_selected_folder "返回選擇路徑 EXCEPTIONS cntl_error = 0 error_no_gui = 1 OTHERS = 2. "檢查是否獲取到路徑 CHECK NOT lv_selected_folder IS INITIAL. DATA:str_len TYPE I. DATA:temp_sign(1) TYPE C. str_len = STRLEN( lv_selected_folder ) - 1. temp_sign = lv_selected_folder+str_len(1). if temp_sign = '\'. "肯定保存路徑 CONCATENATE lv_selected_folder 'Test' INTO lv_complete_path. else. "肯定保存路徑 CONCATENATE lv_selected_folder '\Test' INTO lv_complete_path. ENDIF. "建立excel對象 CREATE OBJECT lo_application 'Excel.Application'. "建立excel對象 SET PROPERTY OF lo_application 'Visible' = 1. "設置excel可見 SET PROPERTY OF lo_application 'SheetsInNewWorkbook' = 1. "建立workbook CALL METHOD OF lo_application 'Workbooks' = lo_workbooks. "調用lo_application的'Workbooks'建立工做簿集 CALL METHOD OF lo_workbooks 'Add' = lo_workbook. "調用lo_workbooks 的方法'Add' ,建立工做薄 GET PROPERTY OF lo_application 'ACTIVESHEET' = lo_worksheet. "獲取當前活動sheet,初始建立時,只有一個sheet,且active SET PROPERTY OF lo_worksheet 'Name' = 'first sheet'. "設置sheet名 "建立一個sheet集合 CALL METHOD OF lo_application 'Sheets' = lo_worksheets . "建立新的work sheet. "默認建立的最新的sheet,處於active狀態 CALL METHOD OF lo_worksheets 'Add' = new_worksheet. *CALL METHOD OF new_worksheet 'Activate'. "激活當前新的sheet SET PROPERTY OF new_worksheet 'Name' = 'second sheet'. "設置sheet名 "lo_application默認sheet,設置值 "建立單元個 CALL METHOD OF lo_worksheet 'Cells' = lo_cell EXPORTING # = 1 "Row # = 1. "Column SET PROPERTY OF lo_cell 'value' = ''. "設置單元個值 "設置單元格字體 CALL METHOD OF lo_cell 'font' = lo_font. "EXCEL支持的字體名 *SET PROPERTY OF lo_font 'Name' = 'Agency FB'. "字體名 SET PROPERTY OF lo_font 'Name' = '微軟正黑體'. "字體名 SET PROPERTY OF lo_font 'Size' = 15. "大小 "建立單元個 CALL METHOD OF lo_worksheet 'Cells' = lo_cell EXPORTING # = 1 "Row # = 2. "Column SET PROPERTY OF lo_cell 'value' = '12/03/2010'. "設置單元個值 "設置單元格值格式,支持excel中的格式 *SET PROPERTY OF lo_cell 'NumberFormat' = '0.00'. SET PROPERTY OF lo_cell 'NumberFormat' = 'm/d/yy'. "設置單元格字體 CALL METHOD OF lo_cell 'font' = lo_font. "顏色數字規則? SET PROPERTY OF lo_font 'Color' = -12123441. "顏色 SET PROPERTY OF lo_font 'TintAndShade' = 0. "明亮度-1~1,1徹底透明 SET PROPERTY OF lo_font 'Bold' = 0. "是否粗體字 0 :false 1:true SET PROPERTY OF lo_font 'Italic' = 1. "是否斜體 0:false 1:true SET PROPERTY OF lo_font 'Underline' = 2. "xlUnderlineStyleSingle = 2,下劃線 "設置單元格背景 DATA:lo_interior TYPE ole2_object. CALL METHOD OF lo_cell 'interior' = lo_interior. SET PROPERTY OF lo_interior 'Color' = 12123441. "設置單元格邊框 DATA:lo_borders TYPE ole2_object. "獲取邊框對象,輸入參數‘7’:左邊框xlEdgeLeft;‘8’:上邊框xlEdgeTop;‘9’:下邊框xlEdgeBottom;‘10’:右邊框xlEdgeRight CALL METHOD OF lo_cell 'borders' = lo_borders EXPORTING # = 7. SET PROPERTY OF lo_borders 'LineStyle' = 1."xlContinuous='1' SET PROPERTY OF lo_borders 'Weight' = 4."xlThick = 4 "在對應new sheet設置單元格值 "建立單元個 CALL METHOD OF new_worksheet 'Cells' = lo_cell EXPORTING # = 1 "Row # = 1. "Column SET PROPERTY OF lo_cell 'value' = 'hello2'. "設置單元個值 "建立單元個 CALL METHOD OF new_worksheet 'Cells' = lo_cell EXPORTING # = 1 "Row # = 2. "Column SET PROPERTY OF lo_cell 'value' = 'welcome2'. "設置單元個值 *"選擇Ranges of cells *CALL METHOD OF lo_worksheet 'Cells' = lo_cellstart * EXPORTING * # = 1 "row * # = 1. "column * *CALL METHOD OF lo_worksheet 'Cells' = lo_cellend * EXPORTING * # = 3 "row * # = 3. "column * *"選中一個區域Cell *CALL METHOD OF lo_worksheet 'RANGE' = lo_range * EXPORTING * # = lo_cellstart * # = lo_cellend. * "選擇列 *CALL METHOD OF lo_worksheet 'Columns' = lo_column * EXPORTING * # = 1. *"選擇行 *CALL METHOD OF lo_worksheet 'Row' = lo_row * EXPORTING * # = 2. * *"獲取選中區域selection *CALL METHOD OF lo_row 'Select'. *CALL METHOD OF lo_application 'selection' = lo_selection. *"添加驗證 *"選中(4,4) *CALL METHOD OF lo_worksheet 'Cells' = lo_cell * EXPORTING * # = 4 * # = 4. *"獲取選中區域selection *CALL METHOD OF lo_cell 'Select'. *CALL METHOD OF lo_application 'selection' = lo_selection. *"獲取驗證對象lo_validation *CALL METHOD OF lo_selection 'Validation' = lo_validation. *"添加驗證規則 *CALL METHOD OF lo_validation 'Add' * EXPORTING * #1 = 4 "驗證類型Type 4 = xlValidateDate,驗證日期 * #2 = 1 "驗證彈窗類型AlertStype 1 = xlValidAlertStop * #3 = 1 "操做類型,Operator 1 = xlBetween,在之間 * #4 = '1/1/2000' "Formula1 * #5 = '1/1/2010'. "Formula2 *"設置錯誤信息 *SET PROPERTY OF lo_validation 'ErrorMessage' = 'Enter a valid date'. "調用lo_workbook 'SaveAs' 方法保存excel CALL METHOD OF lo_workbook 'SaveAs' EXPORTING # = lv_complete_path. if sy-subrc = 0. MESSAGE 'File downloaded successfully' TYPE 'S'. ELSE. MESSAGE 'Error downloading the file' TYPE 'E'. ENDIF. CALL METHOD OF lo_application 'QUIT'. "退出excel "釋放資源 FREE OBJECT lo_worksheet. FREE OBJECT new_worksheet. FREE OBJECT lo_workbook. FREE OBJECT lo_workbooks. FREE OBJECT lo_application.
View Code

 

三、DOI操做EXCEL

DOI:Desktop Office Integretion

四個對象:

container: 存放excel電子表格(spreadsheet)的容器。

展現spreadsheet確定須要一個容器來存放。這個容器通常在dialog screen中定義,也能夠直接使用ABAP程序默認的screen(即screen號碼爲1000的屏幕)。

container control: 容器中用於建立和管理其餘Office集成所須要的對象。container control是一個接口,類型是i_oi_container_control。

document proxy: 每個document proxy的實例表明用office application打開的文檔,能夠是excel,也能夠是word。若是想打開多個文檔,須要定義多個實例。document proxy是一個接口,類型爲i_oi_document_proxy。

spreadsheet: spreadsheet接口,表明最終要操做的excel文檔。spreadhseet的類型是i_oi_spreadsheet

 

經過Tcode:OAOR,能夠將本地文檔模板上傳到服務器行,經過對應類讀取到ABAP程序中進行操做。

若是讀取服務器上的文檔模板,須要cl_bds_document_set類:

business document set: bds是business document set的縮寫。

business document set用於管理後續要操做的文檔,能夠包含一個或多個文檔。

示例:

"容器對象

DATA:gr_container type ref to cl_gui_container. "管理Excel或其餘文檔對象

DATA:gr_control type ref to i_oi_container_control. "文檔對象,打開保存文檔操做

DATA:gr_document type ref to i_oi_document_proxy. "sheet操做接口對象

DATA:gr_spreadsheet type ref to i_oi_spreadsheet. "sheet中選擇操做區域range

DATA:gr_ranges type SOI_RANGE_LIST. DATA:gr_ranges_item TYPE SOI_RANGE_ITEM. "對應range的填充內容

DATA:gr_contents TYPE SOI_GENERIC_TABLE. DATA:gr_contents_item TYPE SOI_GENERIC_ITEM. start-of-selection. perform main. *get container
 form get_container. "獲取容器對象,實例化
 gr_container = cl_gui_container=>screen0."screen0 表明當前1000默認屏幕
 endform. "get_container

 

* create container control
 form create_container_control. "調用方法,建立管理對象control

  CALL METHOD c_oi_container_control_creator=>get_container_control IMPORTING control = gr_control. "初始化control對象

* initialize control

  CALL METHOD gr_control->init_control EXPORTING inplace_enabled = 'X'  "嵌入屏幕顯示‘x’
 inplace_scroll_documents = 'X'  "可滾動
 register_on_close_event = 'X' register_on_custom_event = 'X' r3_application_name = 'DOI demo' parent = gr_container. endform. "create excel document
 form create_excel_document. CALL METHOD gr_control->get_document_proxy EXPORTING document_type = 'Excel.Sheet' no_flush = 'X'

    IMPORTING document_proxy = gr_document. "open_inplace參數控制excel文檔是獨立顯示仍是在SAP GUI中嵌入顯示。

  "若是嵌入顯示,gr_control的init_control方法中,inplace_enabled參數要設爲X

  CALL METHOD gr_document->create_document EXPORTING document_title = 'DOI test' no_flush = '' open_inplace = ''. endform. "create_excel_document

 

"使用spreedsheet填充數據
 form fill_data. "sheet的名字

  DATA:sheetname(20) TYPE C . DATA:error TYPE REF TO I_OI_ERROR. DATA:retcode TYPE SOI_RET_STRING. "調用document的方法,獲取sheet對象

  CALL METHOD gr_document->get_spreadsheet_interface EXPORTING no_flush = ''

    IMPORTING sheet_interface = gr_spreadsheet error = error. "spreadsheet能夠獲取當前活動sheetname

  CALL METHOD gr_spreadsheet->get_active_sheet EXPORTING no_flush = ''

    IMPORTING sheetname = sheetname error = error retcode = retcode. "獲取當前活動sheet指定ranges區域

  CALL METHOD gr_spreadsheet->insert_range_dim EXPORTING no_flush = '' name = 'range1' left = 1 top = 1 rows = 1 columns = 2

* updating = 1

* sheetname = 'sheet1'

    IMPORTING error = error retcode = retcode. * "修改range的名字,範圍

* CALL METHOD gr_spreadsheet->change_range

* EXPORTING

* rangename = 'range1'

* newname = 'range1'

* rows = 1

* columns = 1

** updating = 1 "默認-1

* IMPORTING

* error = error

* retcode = retcode.

 

  "設置ranges區域字體

  CALL METHOD gr_spreadsheet->set_font EXPORTING rangename = 'range1'  "range的名字
 family = 'Times New Roman' size = 9 bold = 0    "加粗
 italic = 0    "斜體
 align = 0    "水平居中

    IMPORTING error = error retcode = retcode. "設置ranges區域格式

  CALL METHOD gr_spreadsheet->set_format EXPORTING rangename = 'range1' typ = 0 currency = 'RMB' decimals = 1

    IMPORTING error = error retcode = retcode. "設置ranges區域
 gr_ranges_item-name = 'range1'. gr_ranges_item-rows = 1. gr_ranges_item-columns = 2. APPEND gr_ranges_item TO gr_ranges. "設置內容,value 256長度
 gr_contents_item-row = 1. gr_contents_item-column = 1. gr_contents_item-value = '112'. APPEND gr_contents_item TO gr_contents. gr_contents_item-row = 1. gr_contents_item-column = 2. gr_contents_item-value = '113'. APPEND gr_contents_item TO gr_contents. "設置sheet中的內容

  CALL METHOD gr_spreadsheet->set_ranges_data EXPORTING no_flush = '' ranges = gr_ranges   "選中區域
 contents = gr_contents "填充內容

* updating = ''

* rangesdef = ''

    IMPORTING retcode = retcode error = error. * "建立一個新的sheet

* call method gr_spreadsheet->add_sheet

* exporting

* name = 'sheet1'

* no_flush = ''

* importing

* error = error

* retcode = retcode.

* "刪除一個sheet

* call method gr_spreadsheet->delete_sheet

* exporting

* name = 'sheet1'

* no_flush = ''

* importing

* error = error

* retcode = retcode.

  "active指定name的sheet

* CALL METHOD gr_spreadsheet->select_sheet

* EXPORTING

* name = 'sheet1'

* no_flush = ''

* IMPORTING

* error = error

* retcode = retcode.

* "修改sheet名字

* CALL METHOD gr_spreadsheet->set_sheet_name

* EXPORTING

* newname = 'sheet1'

* oldname = sheetname

* no_flush = ''

* IMPORTING

* error = error

* retcode = retcode.
 endform. "保存文檔
 form save_doc. DATA:error TYPE REF TO I_OI_ERROR. DATA:retcode TYPE SOI_RET_STRING. "保存的文件路徑

  DATA:file_name(20) TYPE C VALUE 'D:\test.xls'. "只傳文件名,不設置路徑,該方法會自動默認保存到c盤用戶文件下的文檔文件夾中)

  CALL METHOD gr_document->save_as EXPORTING file_name = file_name prompt_user = ''

    IMPORTING error = error retcode = retcode. endform. "釋放資源
 form free_source. "關閉文檔

  CALL METHOD gr_document->close_document. "釋放容器

  CALL METHOD gr_container->free. "關閉文檔管理

  CALL METHOD gr_control->destroy_control. FREE gr_control. LEAVE PROGRAM. endform. form main. * skip 1.
 perform get_container. perform create_container_control. perform create_excel_document. Perform fill_data. perform save_doc. perform free_source. endform.
View Code

 

帶模板的DOI操做:

一、使用Tcode:OAOR,上傳指定格式的文檔;、

二、經過調用cl_bds_document_set=>get_with_url方法,獲取咱們上傳文檔的URL;

三、而後經過gr_document->open_document,將獲取的URL傳入,打開的就是咱們上傳的模板文檔;

 

使用OAOR,上傳模板。Class name:HRFPM_EXCEL_STANDARD;Class Type:OT;Object Key:能夠本身定義,訪問時也是經過這個找到咱們文件,示例:DOI_EXCEL_TEXT。

 

    能夠選擇咱們要上傳的文檔類型。這裏咱們選中excel,雙擊會彈出一個文件選擇框,選擇咱們要上傳的模板文件就能夠了。

 

    修改一下描述,或者默認,點擊確認就能夠了。

 

    咱們能夠看到已經上傳成功。

使用代碼訪問到咱們上傳的文檔,獲取文檔的URL

示例:

*business document system
 data: gr_bds_documents type ref to cl_bds_document_set, g_classname type sbdst_classname, g_classtype type sbdst_classtype, g_objectkey type sbdst_object_key, g_doc_components type sbdst_components, g_doc_signature type sbdst_signature. * template url
 data: gt_bds_uris type sbdst_uri, gs_bds_url like line of gt_bds_uris, g_template_url(256) type c. g_classname = 'HRFPM_EXCEL_STANDARD'. g_classtype = 'OT'. g_objectkey = 'DOI_EXCEL_TEST'. form get_template_url. CREATE OBJECT gr_bds_documents. CALL METHOD cl_bds_document_set=>get_info EXPORTING classname = g_classname classtype = g_classtype object_key = g_objectkey CHANGING components = g_doc_components signature = g_doc_signature. CALL METHOD cl_bds_document_set=>get_with_url EXPORTING classname = g_classname classtype = g_classtype object_key = g_objectkey CHANGING uris = gt_bds_uris signature = g_doc_signature. free gr_bds_documents. read table gt_bds_uris into gs_bds_url index 1. g_template_url = gs_bds_url-uri. endform. "get_template_url

 

"經過URL獲取模板文檔
 call method gr_document->open_document exporting open_inplace = 'X' document_url = g_template_url.
相關文章
相關標籤/搜索