三步搞定ABAP DOI操作EXCEL
---------------------------------------------------前言---------------------------------------------------------------------------------
ABAP可以使用OLE與DOI兩種方式實現操作EXCEL。使用OLE時,每個單元格的值和樣式都需要寫程式碼實現,特別是對於不規則的格式, 程式碼量巨大. 而DOI是從伺服器已經上傳的EXCEL模板中下載模板然後開啟修改實現資料儲存。當然,也可以直接建立新的EXCEL檔案往裡面傳遞資料並設定格式(這樣好像又走回老路了,不是我們要的, OLE與DOI的區別就好像是面向過程與面向物件的區別).
簡單來說,DOI操作EXCEL主要有以下幾步:
準備工作:上傳Excel模板
第一步:下載Excel到本地
第二步:開啟Excel文件
第三步: 寫入資料
----------------------------------------------------正文-------------------------------------------------------------------------------------
準備工作:上傳Excel模板:
1.輸入Tcode: smw0 如圖所示:
2.選擇二進位制資料選擇,回車後進入查詢介面,包: MI
3.點選執行:進入顯示列表,點選新建圖示:
4.輸入物件名稱與描述後點擊開啟檔案選擇框,,選擇檔案後,會提示選擇包,
好了,可以寫程式碼了(複製可以直接執行,前提是已經上傳模板),
REPORT zr04_ole_templet.DATA:c_export_filename_xls TYPE string VALUE 'ZRRFQ01_1.XLS', "匯出模板預設檔名 '資料匯入模板'
c_objid_xls TYPE wwwdatatab-objid VALUE 'ZRRFQ01_1.XLS'. "存放模板的物件idDATA: lo_objdata
ls_destination LIKE rlgrap-filename ,"下載儲存的目標路徑
lc_path TYPE string, "儲存路徑
lc_fullpath TYPE string, "檔案完整路徑
li_rc LIKE sy-subrc. "返回值DATA: excel_obj TYPE ole2_object,
book_obj TYPE ole2_object,
sheet_obj TYPE ole2_object,
cell_obj TYPE ole2_object.START-OF-SELECTION.
第一步: 下載Excel到本地PERFORM download_xls_template.
第二步:開啟Excel文件PERFORM open_excel.
第三步: 寫入資料PERFORM write_excel.*&---------------------------------------------------------------------**& Form download_xls_template*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*FORM download_xls_template.* 獲取儲存路徑CALL METHOD cl_gui_frontend_services=>get_desktop_directory
CHANGING
desktop_directory = lc_path.IF lc_path IS INITIAL.
lc_path = 'C:\TEMP'.ENDIF.CONCATENATE lc_path '\' c_export_filename_xls INTO lc_fullpath.* 檢查模板是否存在SELECT SINGLE relid objid FROM wwwdata INTO CORRESPONDING FIELDS OF lo_objdata
WHERE srtf2 = 0 AND relid = 'MI' AND objid = c_objid_xls.IF sy-subrc NE 0 OR lo_objdata-objid EQ space.MESSAGE e000(zpp001) WITH c_export_filename_xls.ENDIF.* 下載模板
ls_destination = lc_fullpath.CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'EXPORTINGkey = lo_objdata
destination = ls_destination
IMPORTING
rc = li_rc.IF li_rc NE 0.MESSAGE e001(zpp001) WITH c_export_filename_xls.ENDIF.ENDFORM. "download_xls_template*&---------------------------------------------------------------------**& Form open_excel*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*FORM open_excel.CREATE OBJECT excel_obj 'excel.APPLICATION'.IF sy-subrc NE 0.MESSAGE 'EXCEL建立錯誤' TYPE 'S' DISPLAY LIKE 'E'.STOP.ENDIF.CALL METHOD OF excel_obj 'WORKBOOKS' = book_obj.SET PROPERTY OF excel_obj 'VISIBLE' = 1.SET PROPERTY OF excel_obj 'SheetsInNewWorkbook' = 1.
* 開啟excel檔案 , (新建使用:CALL METHOD OF book_obj 'Add' = sheet_obj)CALL METHOD OF book_obj 'Open' = sheet_obj
EXPORTING #1 = ls_destination.CALL METHOD OF sheet_obj 'ACTIVATE'.FREE OBJECT sheet_obj. "OKENDFORM. "open_excel*&---------------------------------------------------------------------**& Form write_excel*&---------------------------------------------------------------------** text*----------------------------------------------------------------------*FORM write_excel.DATA: it_spfli LIKE spfli OCCURS 10 WITH HEADER LINE.DATA: h TYPE i. "行號*取資料SELECT * FROM spfli INTO TABLE it_spfli UP TO 10 ROWS.* 輸出Excel表頭,自定義格式的列等PERFORM fill_cell USING 1 1 1 'Flug'(001).PERFORM fill_cell USING 1 2 1 'Nr'(002).PERFORM fill_cell USING 1 3 1 'Von'(003).PERFORM fill_cell USING 1 4 1 'Nach'(004).PERFORM fill_cell USING 1 5 1 'Zeit'(005).* 複製資料到Excel,針對固定格式LOOP AT it_spfli.
h = sy-tabix + 1.PERFORM fill_cell USING h 1 0 it_spfli-carrid.PERFORM fill_cell USING h 2 0 it_spfli-connid.PERFORM fill_cell USING h 3 0 it_spfli-cityfrom.PERFORM fill_cell USING h 4 0 it_spfli-cityto.PERFORM fill_cell USING h 5 0 it_spfli-deptime.ENDLOOP.FREE OBJECT cell_obj.ENDFORM. "write_excel*&---------------------------------------------------------------------**& Form FILL_CELL*&---------------------------------------------------------------------** row: 行號,* col: 列號,* bold: 字型是否加粗,0,否,1是.* val: 填充值*----------------------------------------------------------------------*FORM fill_cell USING row col bold val.CALL METHOD OF excel_obj 'CELLS' = cell_obj
EXPORTING #1 = row
#2 = col.SET PROPERTY OF cell_obj 'VALUE' = val.FREE OBJECT cell_obj.ENDFORM. "FILL_CELL
效果圖: