SAP OLE完美解決寫入資料慢的問題
阿新 • • 發佈:2018-11-24
將內表儲存到剪貼簿,然後將剪貼簿的資料貼上到EXCEL
步驟和解決的問題:
- 用SWM0上傳文件模版
- 下載模版檔案到本地
DATA: lv_objdata LIKE wwwdatatab, lv_errtxt TYPE string, lv_destination LIKE rlgrap-filename, lv_rc LIKE sy-subrc. lv_destination = gv_fullpath. SELECT SINGLE relid objid FROM wwwdata INTO CORRESPONDING FIELDS OF lv_objdata WHERE srtf2 = 0 AND relid = 'MI' AND objid = 'ZWMP154'. IF sy-subrc NE 0 OR lv_objdata-objid EQ space. lv_errtxt = '模板檔案:ZWMP154不存在,請用TCODE:SMW0進行載入' . MESSAGE e000(su) WITH lv_errtxt. ENDIF. CALL FUNCTION 'DOWNLOAD_WEB_OBJECT' EXPORTING key = lv_objdata destination = lv_destination IMPORTING rc = lv_rc. IF lv_rc NE 0. CONCATENATE '模板檔案' '下載失敗' INTO lv_errtxt. MESSAGE e000(su) WITH lv_errtxt. ENDIF.
3. 通過OLE開啟模版並寫入資料
CREATE OBJECT excel 'EXCEL.APPLICATION'. IF sy-subrc <> 0. WRITE: 'error'. STOP. ENDIF. * 設定excel是否顯示,0 不顯示,1顯示 SET PROPERTY OF excel 'VISIBLE' = 1. CALL METHOD OF excel 'Workbooks' = workbook. CALL METHOD OF workbook 'Open' EXPORTING #1 = lv_destination. GET PROPERTY OF excel 'ACTIVECELL' = sheet. CALL METHOD OF excel 'Worksheets' = sheet EXPORTING #1 = 'Sheet1'. "#EC NOTEXT " 根據實際 excel table頁, 要改 CALL METHOD OF sheet 'Activate'.
4.呼叫剪貼簿,將內表資料儲存到剪貼簿,然後貼上到EXCEL
注意:工作區欄位用製表符 cl_abap_char_utilities=>horizontal_tab 分割
"指定複製位置
r1 = 2.
r2 = 1.
PERFORM frm_paste_clipboard_to_ole USING r1 r2 sheet cell.
FORM frm_paste_clipboard_to_ole USING p_row TYPE i p_col TYPE i p_sheet p_cell. DATA lv_rc TYPE i. DATA lv_type TYPE c. FIELD-SYMBOLS <lv_field>. DATA: BEGIN OF lw_char, line(2200) TYPE c, END OF lw_char. DATA lt_char LIKE TABLE OF lw_char. DATA lt_char2 LIKE TABLE OF lw_char."將剪貼簿原來的資料拷貝出來 cl_gui_frontend_services=>clipboard_import( IMPORTING data = lt_char2 " Data Table * length = " Data length EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4 ). * Copy to clipboard into ABAP CALL FUNCTION 'CONTROL_FLUSH' EXCEPTIONS OTHERS = 3. DESCRIBE FIELD gw_download TYPE lv_type COMPONENTS lv_rc. LOOP AT gt_download INTO gw_download. CLEAR lw_char-line. DO lv_rc TIMES. ASSIGN COMPONENT sy-index OF STRUCTURE gw_download TO <lv_field>. CONCATENATE lw_char-line <lv_field> cl_abap_char_utilities=>horizontal_tab INTO lw_char-line. ENDDO. APPEND lw_char TO lt_char. ENDLOOP. cl_gui_frontend_services=>clipboard_export( EXPORTING no_auth_check = 'X' " Switch off Check for Access Rights IMPORTING data = lt_char " Data CHANGING rc = lv_rc " Return Code EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 no_authority = 4 OTHERS = 5 ). * Select the cell A1 CALL METHOD OF p_sheet 'Cells' = p_cell EXPORTING #1 = p_row #2 = p_col. * Paste clipboard from cell A1 CALL METHOD OF p_cell 'SELECT'. CALL METHOD OF p_sheet 'PASTE'. CALL METHOD OF p_sheet 'Cells' = p_cell EXPORTING #1 = p_row #2 = p_col. CALL METHOD OF p_cell 'SELECT'."取消全選 cl_gui_frontend_services=>clipboard_export( EXPORTING no_auth_check = 'X' " Switch off Check for Access Rights IMPORTING data = lt_char2 " Data CHANGING rc = lv_rc " Return Code EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 no_authority = 4 OTHERS = 5 ). ENDFORM. " PASTE_CLIPBOARD
5.儲存釋放EXCEL
GET PROPERTY OF excel 'ACTIVEWORKBOOK' = workbook.
*
*
CALL METHOD OF
workbook
'SAVE'.
* EXPORTING
* #1 = gv_fullpath "'d:\fuck.xlsx' "fn1
* #2 = 1.
*
* CALL METHOD OF
* workbook
* 'CLOSE'.
*
* CALL METHOD OF
* excel
* 'QUIT'.
*
*WRITE 'SUCCESS'.
*
FREE OBJECT sheet.
FREE OBJECT workbook.
FREE OBJECT excel.