ABAP_Excel 处理总结(二)
生活随笔
收集整理的這篇文章主要介紹了
ABAP_Excel 处理总结(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
*&------------------------------------------------ ---------------------*
*& (1) Form create_excel
*&------------------------------------------------ ---------------------*
* 創建excel
*------------------------------------------------- ---------------------*
FORM create_excel.
IF sy-subrc <> 0.
MESSAGE 'EXCEL ERROR' TYPE 'S' DISPLAY LIKE 'E'.
STOP.
ENDIF.
CALL METHOD OF application 'WORKBOOKS' = workbook.
SET PROPERTY OF application 'VISIBLE' = 1.
SET PROPERTY OF application 'SHEETSINNEWWORKBOOK' = 1.
CALL METHOD OF workbook 'ADD' = sheet.
CALL METHOD OF sheet 'ACTIVE'.
ENDFORM. "create_excel
*&------------------------------------------------ ---------------------*
*& (2) Form insert_row
*&------------------------------------------------ ---------------------*
* 根據行號插進一行
*------------------------------------------------- ---------------------*
* -->I_ROW 行號
*------------------------------------------------- ---------------------*
FORM insert_row USING i_row.
CALL METHOD OF application 'ROWS' = rows
EXPORTING
#1 = i_row.
CALL METHOD OF rows 'INSERT'.
ENDFORM. "insert_row
*&------------------------------------------------ ---------------------*
*& (3) Form fill_cell
*&------------------------------------------------ ---------------------*
* 填充單元格
*------------------------------------------------- ---------------------*
* -->I_ROW 行號
* -->I_COL 列號
* -->BOLD 是否加粗
* -->P_VALUE 值
*------------------------------------------------- ---------------------*
FORM fill_cell USING i_row i_col bold p_value.
CALL METHOD OF application 'CELLS' = cell
EXPORTING
#1 = i_row
#2 = i_col.
SET PROPERTY OF cell 'VALUE' = p_value.
SET PROPERTY OF cell 'HORIZONTALALIGNMENT' = 2.
GET PROPERTY OF cell 'FONT' = font.
SET PROPERTY OF font 'BOLD' = bold.
SET PROPERTY OF font 'COLORINDEX' = 41.
CALL METHOD OF cell 'BORDERS' = borders
EXPORTING
#1 = '2'.
SET PROPERTY OF borders 'LINESTYLE' = '0'.
SET PROPERTY OF borders 'WEIGHT' = 0.
ENDFORM. "fill_cell
*&------------------------------------------------ ---------------------*
*& (4) Form columnwidth
*&------------------------------------------------ ---------------------*
* 設置列寬為自動或是固定值
*------------------------------------------------- ---------------------*
* -->COLNUM 列號,可以為單個列或是多個列
* -->WIDTH 列寬值
*------------------------------------------------- ---------------------*
FORM columnwidth USING colnum width.
CALL METHOD OF application 'COLUMNS' = columns
EXPORTING
#1 = colnum.
IF width = ''.
CALL METHOD OF columns 'AutoFit'.
ELSE.
SET PROPERTY OF columns 'COLUMNWIDTH' = width.
ENDIF.
ENDFORM. "columnwidth
*&------------------------------------------------ ---------------------*
*& (5) Form create_sheet
*&------------------------------------------------ ---------------------*
* 創建sheet
*------------------------------------------------- ---------------------*
FORM create_sheet.
CALL METHOD OF application 'sheets' = sheet.
CALL METHOD OF sheet 'ADD'.
ENDFORM. "create_sheet
*&------------------------------------------------ ---------------------*
*& (6) Form copy_sheet_all
*&------------------------------------------------ ---------------------*
* 復制sheet全部內容到另一個sheet
*------------------------------------------------- ---------------------*
FORM copy_sheet_all.
CALL METHOD OF application 'Sheets' = sheet
EXPORTING
#1 = 'Sheet1'.
CALL METHOD OF sheet 'Cells' = cell.
CALL METHOD OF cell 'copy'.
CALL METHOD OF application 'Sheets' = sheet
EXPORTING
#1 = 'Sheet3'.
CALL METHOD OF application 'Range' = range
EXPORTING
#1 = 'A1'.
CALL METHOD OF range 'PasteSpecial'.
ENDFORM. "copy_sheet_all
*&------------------------------------------------ ---------------------*
*& (7) Form copy_sheet_area
*&------------------------------------------------ ---------------------*
* 復制sheet部分內容到另一個sheet
*------------------------------------------------- ---------------------*
FORM copy_sheet_area.
CALL METHOD OF application 'Sheets' = sheet
EXPORTING
#1 = 'Sheet1'.
CALL METHOD OF sheet 'Range' = range
EXPORTING
#1 = 'A1'
#2 = 'B10'.
CALL METHOD OF range 'copy'.
CALL METHOD OF application 'Sheets' = sheet1
EXPORTING
#1 = 'Sheet2'.
CALL METHOD OF sheet1 'Range' = range1
EXPORTING
#1 = 'A1'
#2 = 'B10'.
CALL METHOD OF range1 'PasteSpecial'.
ENDFORM. "copy_sheet_area
*&------------------------------------------------ ---------------------*
*& (8) Form copy_cells
*&------------------------------------------------ ---------------------*
* 在一個sheet內復制單元格
*------------------------------------------------- ---------------------*
FORM copy_cells.
CALL METHOD OF application 'Sheets' = sheet
EXPORTING
#1 = 'Sheet1'.
CALL METHOD OF sheet 'Range' = range
EXPORTING
#1 = 'A1'
#2 = 'B10'.
CALL METHOD OF range 'copy'.
CALL METHOD OF sheet 'Range' = range1
EXPORTING
#1 = 'A12'
#2 = 'B22'.
CALL METHOD OF range1 'PasteSpecial'.
ENDFORM. "copy_cells
*&------------------------------------------------ ---------------------*
*& (9) Form range_borders
*&------------------------------------------------ ---------------------*
* 指定range加邊框
*------------------------------------------------- ---------------------*
FORM range_borders.
CALL METHOD OF application 'Range' = range
EXPORTING
#1 = 'A1'
#2 = 'B10'.
CALL METHOD OF range 'BORDERS' = borders.
SET PROPERTY OF borders 'LINESTYLE' = 1.
SET PROPERTY OF borders 'WEIGHT' = 10.
ENDFORM. "range_borders
*&------------------------------------------------ ---------------------*
*& (10) Form row_color
*&------------------------------------------------ ---------------------*
* 設定行背景色
*------------------------------------------------- ---------------------*
* -->I_ROW 行號
*------------------------------------------------- ---------------------*
FORM row_color USING i_row.
CALL METHOD OF application 'ROWS' = rows
EXPORTING
#1 = i_row.
GET PROPERTY OF rows 'Interior' = font.
SET PROPERTY OF font 'ColorIndex' = '10'.
ENDFORM. "row_color
*&------------------------------------------------ ---------------------*
*& (11) Form preview_excel
*&------------------------------------------------ ---------------------*
* 預覽excel
*------------------------------------------------- ---------------------*
FORM preview_excel.
SET PROPERTY OF application 'VISIBLE' = 1.
CALL METHOD OF sheet 'PRINTPREVIEW'.
* m_message.
ENDFORM. "preview_excel
*&------------------------------------------------ ---------------------*
*& (12) Form free_object
*&------------------------------------------------ ---------------------*
* 開釋object
*------------------------------------------------- ---------------------*
FORM free_object.
FREE OBJECT font.
FREE OBJECT range.
FREE OBJECT range1.
FREE OBJECT columns.
FREE OBJECT rows.
FREE OBJECT cell.
FREE OBJECT cell1.
FREE OBJECT sheet1.
FREE OBJECT sheet.
FREE OBJECT workbook.
FREE OBJECT application.
ENDFORM. "free_object
*&------------------------------------------------ ---------------------*
*& (13) Form close_excel
*&------------------------------------------------ ---------------------*
* 封閉excel并保存
*------------------------------------------------- ---------------------*
FORM close_excel.
CALL METHOD OF workbook 'Save'.
CALL METHOD OF workbook 'Close'
EXPORTING
#1 = 0.
CALL METHOD OF application 'Quit'.
ENDFORM. "close_excel
*&------------------------------------------------ ---------------------*
*& (14) Form download_excel_fromserver
*&------------------------------------------------ ---------------------*
* 從服務器下載excel模板
*------------------------------------------------- ---------------------*
* -->P_OBJID 模板名稱
* -->P_DEST 存檔路徑
*------------------------------------------------- ---------------------*
FORM download_excel_fromserver
USING p_objid LIKE wwwdatatab-objid
p_dest LIKE sapb-sappfad.
DATA: lo_objdata LIKE wwwdatatab,
lo_mime LIKE w3mime,
ls_destination LIKE rlgrap-filename,
ls_objnam TYPE string,
li_rc LIKE sy-subrc,
ls_errtxt TYPE string.
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.
IF sy-subrc NE 0 OR lo_objdata-objid EQ space.
CONCATENATE '模板文件:' ls_objnam '不存在www.hcw163.com, 攪拌車請用TCODE:SMW0進行加載' INTO ls_errtxt.
MESSAGE e600(zdev) WITH ls_errtxt.
ENDIF.
ls_destination = p_dest.
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 e600(zdev) WITH ls_errtxt.
ENDIF.
ENDFORM. "download_excel_fromserver
*&------------------------------------------------ ---------------------*
*& (15) Form open_excel_hide
*&------------------------------------------------ ---------------------*
* 后臺打開指定的excel
*------------------------------------------------- ---------------------*
* -->P_FILE excel本地路徑
*------------------------------------------------- ---------------------*
FORM open_excel_hide USING p_file.
SET PROPERTY OF application 'Visible' = 0.
CALL METHOD OF application 'Workbooks' = workbook.
CALL METHOD OF workbook 'Open' = workbook
EXPORTING
#1 = p_file.
CALL METHOD OF workbook 'Worksheets' = sheet
EXPORTING
#1 = 'Sheet1'.
CALL METHOD OF sheet 'Activate'.
ENDFORM. "open_excel_hide
*&------------------------------------------------ ---------------------*
*& (16) Form open_excel
*&------------------------------------------------ ---------------------*
* 打開指定的excel
*------------------------------------------------- ---------------------*
* -->P_FILE excel本地路徑
*------------------------------------------------- ---------------------*
FORM open_excel USING p_file.
SET PROPERTY OF application 'Visible' = 1.
CALL METHOD OF application 'Workbooks' = workbook.
CALL METHOD OF workbook 'Open' = workbook
EXPORTING
#1 = p_file.
CALL METHOD OF workbook 'Worksheets' = sheet
EXPORTING
#1 = 'Sheet1'.
CALL METHOD OF sheet 'Activate'.
ENDFORM. "open_excel
*&------------------------------------------------ ---------------------*
*& (17) Form open_excel_sheet
*&------------------------------------------------ ---------------------*
* 打開指定的excel的sheet
*------------------------------------------------- ---------------------*
* -->P_SHEET 打開的sheet名
* -->P_FILE excel本地路徑
*------------------------------------------------- ---------------------*
FORM open_excel_sheet USING p_sheet p_file.
SET PROPERTY OF application 'Visible' = 1.
CALL METHOD OF application 'Workbooks' = workbook.
CALL METHOD OF workbook 'Open' = workbook
EXPORTING
#1 = p_file.
CALL METHOD OF workbook 'Worksheets' = sheet
EXPORTING
#1 = p_sheet.
CALL METHOD OF sheet 'Activate'.
ENDFORM. "open_excel_sheet
*&------------------------------------------------ ---------------------*
*& (18) Form open_excel_by_sheet
*&------------------------------------------------ ---------------------*
* 打開sheet
*------------------------------------------------- ---------------------*
* -->P_SHEET 打開的sheet名
*------------------------------------------------- ---------------------*
FORM open_excel_by_sheet USING p_sheet.
CALL METHOD OF workbook 'WORKSHEETS' = sheet
EXPORTING
#1 = p_sheet.
CALL METHOD OF sheet 'ACTIVATE'.
ENDFORM. "open_excel_by_sheet
*&------------------------------------------------ ---------------------*
*& (19) Form select_range
*&------------------------------------------------ ---------------------*
* 選擇范圍
*------------------------------------------------- ---------------------*
* -->P_LEFT 左
* -->P_TOP 上
* -->P_RIGHT 右
* -->P_BUTTOM 下
*------------------------------------------------- ---------------------*
FORM select_range USING
p_left TYPE i
p_top TYPE i
p_right TYPE i
p_buttom TYPE i.
CALL METHOD OF application 'CELLS' = cell
EXPORTING
#1 = p_top
#2 = p_left.
CALL METHOD OF application 'CELLS' = cell1
EXPORTING
#1 = p_buttom
#2 = p_right.
CALL METHOD OF application 'RANGE' = range
EXPORTING
#1 = cell
#2 = cell1.
CALL METHOD OF range 'SELECT'.
ENDFORM. "select_range
*&------------------------------------------------ ---------------------*
*& (20) Form clear_data
*&------------------------------------------------ ---------------------*
* 清除數據
*------------------------------------------------- ---------------------*
* -->P_LEFT 左
* -->P_TOP 上
* -->P_RIGHT 右
* -->P_BUTTOM 下
*------------------------------------------------- ---------------------*
FORM clear_data USING p_left TYPE i
p_top TYPE i
p_right TYPE i
p_buttom TYPE i.
PERFORM select_range USING p_left
p_top
p_right
p_buttom.
CALL METHOD OF range 'ClearContents'.
ENDFORM. "clear_data
*&------------------------------------------------ ---------------------*
*& (21) Form merge_cells
*&------------------------------------------------ ---------------------*
* 合并單元格
*------------------------------------------------- ---------------------*
* -->P_LEFT text
* -->P_TOP text
* -->P_RIGHT text
* -->P_BUTTOM text
*------------------------------------------------- ---------------------*
FORM merge_cells USING p_left TYPE i
p_top TYPE i
p_right TYPE i
p_buttom TYPE i.
PERFORM select_range USING p_left
p_top
p_right
p_buttom.
CALL METHOD OF range 'Merge'.
ENDFORM. "merge_cells
*&------------------------------------------------ ---------------------*
*& (22) Form process_indcator
*&------------------------------------------------ ---------------------*
* 進度指示
*------------------------------------------------- ---------------------*
* -->TEXT 提示文字
* -->PERCENTAGE 進度百分比
*------------------------------------------------- ---------------------*
FORM process_indcator USING text percentage.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
percentage = percentage
text = text.
ENDFORM. "process_indcator
*&------------------------------------------------ ---------------------*
*& (23) Form delete_row
*&------------------------------------------------ ---------------------*
* 刪除行
*------------------------------------------------- ---------------------*
* -->I_ROW 行號
*------------------------------------------------- ---------------------*
FORM delete_row USING i_row.
CALL METHOD OF application 'ROWS' = rows
EXPORTING
#1 = i_row.
CALL METHOD OF rows 'DELETE'.
ENDFORM. "delete_row
*&------------------------------------------------ ---------------------*
*& (24) Form add_comment
*&------------------------------------------------ ---------------------*
* 添加注釋
*------------------------------------------------- ---------------------*
* -->I_ROW 行號
* -->I_COL 列號
* -->P_TEXT 注釋內容
*------------------------------------------------- ---------------------*
FORM add_comment USING i_row i_col p_text.
CALL METHOD OF application 'Cells' = cell
EXPORTING
#1 = i_row
#2 = i_col.
CALL METHOD OF cell 'AddComment' = cell1.
CALL METHOD OF cell1 'Text'
EXPORTING
#1 = p_text.
CALL METHOD OF cell1 'Shape' = font.
SET PROPERTY OF font 'Width' = '20'.
SET PROPERTY OF font 'Height' = '20'.
ENDFORM. "add_comment
*&------------------------------------------------ ---------------------*
*& (25) Form hide_columns
*&------------------------------------------------ ---------------------*
* 隱躲列
*------------------------------------------------- ---------------------*
* -->COLNUM 輸進列號
*------------------------------------------------- ---------------------*
FORM hide_columns USING colnum.
CALL METHOD OF application 'COLUMNS' = columns
EXPORTING
#1 = colnum.
SET PROPERTY OF columns 'Hidden' = 'true'.
ENDFORM. "hide_columns
*&------------------------------------------------ ---------------------*
*& (26) Form delete_columns
*&------------------------------------------------ ---------------------*
* 刪除列
*------------------------------------------------- ---------------------*
* -->COLNUM 輸進列號
*------------------------------------------------- ---------------------*
FORM delete_columns USING colnum.
CALL METHOD OF application 'COLUMNS' = columns
EXPORTING
#1 = colnum.
CALL METHOD OF columns 'DELETE'.
ENDFORM. "delete_columns
*&------------------------------------------------ ---------------------*
*& (27) Form hide_row
*&------------------------------------------------ ---------------------*
* 隱躲行
*------------------------------------------------- ---------------------*
* -->I_ROW 輸進行號
*------------------------------------------------- ---------------------*
FORM hide_row USING i_row.
CALL METHOD OF application 'ROWS' = rows
EXPORTING
#1 = i_row.
SET PROPERTY OF rows 'Hidden' = 'true'.
ENDFORM. "hide_row
3 留意事項
3.1 Sheet名稱
Sheet名為中文時, 湖北航天雙龍在調用sheet對象時往往會調用不到,用英文字母不存在該題目www.zgjbc.comwww.szsnc.org。
3.2 開釋對象
程序結束時需要開釋對象,否則Excel有可能無法封閉。
開釋對象代碼如下:
FORM free_object.
FREE OBJECT font.
FREE OBJECT range.
FREE OBJECT range1.
FREE OBJECT columns.
FREE OBJECT rows.
FREE OBJECT cell.
FREE OBJECT cell1.
FREE OBJECT sheet1.
FREE OBJECT sheet.
FREE OBJECT workbook.
FREE OBJECT application.
ENDFORM.
總結
以上是生活随笔為你收集整理的ABAP_Excel 处理总结(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SAP ABAP STOP,EXIT,C
- 下一篇: ABAP函数集合