SAP ABAP OLE 操作上傳的EXCEL模板(轉)
DOI操作EXCEL等方法。以後用到再做介紹。此處不是直接生成EXCEL檔案,而是從
伺服器已經上傳的EXCEL模板中下載模板然後開啟修改實現資料儲存。也可以直接建立
新的EXCEL檔案往裡面傳遞資料並設定格式。這裡不做介紹。
1.採用模板檔案實現。上傳EXCEL模板
T-code:SMW0 WebRFC 的二進位制資料 包:MI
點選按鈕,上傳excel模板。
模板資料儲存在系統表wwwdata中。
2.在程式中下載模板
*下載EXCEL模板FORM
FORM temp_excel_get.
DATA: lo_objdata LIKE wwwdatatab,
lo_mime LIKE w3mime,
lc_filename TYPE string VALUE'test01' ,
lc_fullpath TYPE string VALUE'D:\test\' ,
lc_path TYPE string VALUE'D:\test\' ,
ls_destination LIKE rlgrap-filename,
ls_objnam TYPE string,
li_rc LIKE sy-subrc,
ls_errtxt TYPE string.
DATA:p_objid TYPE wwwdatatab-objid,
p_dest LIKE sapb-sappfad.
p_objid = 'ZTEST.XLS'. “此處為EXCEL模板名稱
CALL METHOD cl_gui_frontend_services=>file_save_dialog ”呼叫儲存對話方塊
EXPORTING
default_extension = 'XLS'
default_file_name = lc_filename
CHANGING
filename = lc_filename
path = lc_path
fullpath = lc_fullpath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF lc_fullpath = ''.
MESSAGE '不能開啟excel' TYPE 'E'.
ENDIF.
IF sy-subrc = 0.
p_dest = lc_fullpath.
* concatenate p_objid '.XLS' into ls_objnam.
CONDENSE ls_objnam NO-GAPS.
SELECT SINGLE relid objid FROM wwwdata INTO CORRESPONDING FIELDS OF lo_objdata
WHERE srtf2 = 0 AND relid = 'MI' AND objid = p_objid.
*檢查表wwwdata中是否存在所指定的模板檔案
IF sy-subrc NE 0 OR lo_objdata-objid EQ space.“如果不存在,則給出錯誤提示
CONCATENATE '模板檔案' ls_objnam '不存在' INTO ls_errtxt.
MESSAGE ls_errtxt TYPE 'I'.
ENDIF.
ls_destination = p_dest. ”儲存路徑
*如果存在,呼叫DOWNLOAD_WEB_OBJECT 函式下載模板到路徑下
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = lo_objdata
destination = ls_destination
IMPORTING
rc = li_rc.
IF li_rc NE 0.
CONCATENATE '模板檔案:' ls_objnam '下載失敗' INTO ls_errtxt.
MESSAGE ls_errtxt TYPE 'E'.
ENDIF.
fname = ls_destination.
ENDIF.
ENDFORM. "fm_excel
3.下載完模板後,開啟模板檔案,填入資料
CREATE OBJECT excel 'EXCEL.APPLICATION'. "Create EXCEL OBJECT
IF sy-subrc NE 0.
EXIT.
ENDIF.
SET PROPERTY OF excel 'Visible' = 0. "1/0 是否顯示EXCEL
CALL METHOD OF excel 'Workbooks' = workbook.
CALL METHOD OF workbook 'Open'
EXPORTING
#1 = fname. ."開啟上面下載路徑下的excel檔案
CALL METHOD OF excel 'Sheets' = sheet
EXPORTING
#1 = 1.
CALL METHOD OF sheet 'Select' .
CALL METHOD OF sheet 'ACTIVATE'. “sheet 啟用
SET PROPERTY OF sheet 'NAME' = sheetname. “設定sheet名稱
*此處假設內表itab 中已經存在需要寫入excel中的資料
*並且假如從模板的第7行開始插入資料
LOOP AT itab INTO wa.
tab = wa-tabix + 6.
*在excel中插入一行
PERFORM excel_row_insert USING sheet tab 1.
*填充所插入行的每個單元格的資料
PERFORM fill_range USING tab 1 wa-t01.
PERFORM fill_range USING tab 2 wa-t02.
PERFORM fill_range USING tab 3 wa-t03.
PERFORM fill_range USING tab 4 wa-t04.
PERFORM fill_range USING tab 5 wa-t05.
PERFORM fill_range USING tab 6 wa-t06.
ENDLOOP.
*設定EXCEL中所插入的資料行邊框線格式為黑色有邊框
bod = tab.
CONDENSE bod NO-GAPS.
CONCATENATE 'A6:F' bod INTO bod.
PERFORM borderrange USING excel bod.
PERFORM sub_excel_save."儲存excel資料
*&---------------------------------------------------------------------*
*& Form sub_excel_save
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM sub_excel_save.
GET PROPERTY OF excel 'ActiveSheet' = sheet. “獲取活動SHEET
FREE OBJECT sheet.
FREE OBJECT workbook.
GET PROPERTY OF excel 'ActiveWorkbook' = workbook.
CALL METHOD OF workbook 'SAVE'.
SET PROPERTY OF excel 'Visible' = 1. "是否顯示EXCEL 此處顯示不退出
* SET PROPERTY OF excel 'Visible' = 1.
* CALL METHOD OF workbook 'CLOSE'.
* CALL METHOD OF excel 'QUIT'. 註釋部分為不顯示直接退出
FREE OBJECT sheet.
FREE OBJECT workbook.
FREE OBJECT excel.
ENDFORM. "save_book
*&---------------------------------------------------------------------*
*& 向excel中的指定行插入N行
*&---------------------------------------------------------------------*
FORM excel_row_insert USING lcobj_sheet
lc_row
lc_count.
DATA lc_range TYPE ole2_object.
DATA h_borders TYPE ole2_object.
DO lc_count TIMES.
CALL METHOD OF lcobj_sheet 'Rows' = lc_range
EXPORTING #1 = 6.
CALL METHOD OF lc_range 'Copy'. “COPY第6行插入一個新行
CALL METHOD OF lcobj_sheet 'Rows' = lc_range
EXPORTING #1 = lc_row.
CALL METHOD OF lc_range 'Insert'.
CALL METHOD OF lc_range 'ClearContents'. "是否需要清空Cell
ENDDO.
ENDFORM. "excel_row_insert
*&---------------------------------------------------------------------*
*& Form fill_range
*&---------------------------------------------------------------------*
* text 填充EXCEL 單元格
*----------------------------------------------------------------------*
* -->VALUE(F_ROW) text
* -->VALUE(F_COL) text
* -->VALUE(F_VALUE) text
*----------------------------------------------------------------------*
FORM fill_range USING value(f_row)
value(f_col)
value(f_value).
DATA:
row TYPE i,
col TYPE i.
row = f_row.
col = f_col.
CALL METHOD OF excel 'CELLS' = cell
EXPORTING
#1 = row
#2 = col.
SET PROPERTY OF cell 'VALUE' = f_value.
ENDFORM. "fill_range
*&---------------------------------------------------------------------*
*& Form borderrange
*&---------------------------------------------------------------------*
* text:設定EXCEL中所插入的資料行邊框線格式
*----------------------------------------------------------------------*
* -->LCOBJ_EXCEL text
* -->RANGE text
*----------------------------------------------------------------------*
FORM borderrange USING lcobj_excel
range .
DATA: lc_cell TYPE ole2_object ,
lc_borders TYPE ole2_object .
CALL METHOD OF lcobj_excel 'RANGE' = lc_cell
EXPORTING
#1 = range.
DO 4 TIMES .
CALL METHOD OF lc_cell 'BORDERS' = lc_borders
EXPORTING #1 = sy-index.
SET PROPERTY OF lc_borders 'LineStyle' = '1'.
SET PROPERTY OF lc_borders 'WEIGHT' = 2. "4=max
SET PROPERTY OF lc_borders 'ColorIndex' = '1'.
ENDDO.
FREE OBJECT lc_borders.
FREE OBJECT lc_cell.
ENDFORM. "borderrange