SAP 爱好者,记录一些知识点吧
分类: WINDOWS
2010-06-10 16:58:34
1. 利用t-code smw0上传你要的标准excel 表格到sap 内存
2. 调用OLE 方法来操作EXCEL.
注意, excel里面有几个概念,workbook , 就是我们所说的excel ,比如 D:\volcano\ABAP\PM\zpm0004_new zpm0005_new\固定资产调拨申请单_20100610.xls这个excel,
Worksheet ,就是新建立一个excel后里面有3个sheet,就是指的这个。
首先定义这几个与excel 相关的变量
DATA: v_excel TYPE ole2_object,
v_sheet TYPE ole2_object,
v_book TYPE ole2_object,
v_rows TYPE ole2_object,
v_cell TYPE ole2_object。
其次,创建application.
CREATE OBJECT v_excel 'Excel.Application'.
IF sy-subrc NE 0 .
CREATE OBJECT v_excel 'ET.APPLICATION'.
IF sy-subrc NE 0.
MESSAGE a001(00) WITH '无法创建OLE OBJECT,请检查是否安装金山WPS或微软Office!'.
ENDIF.
ENDIF .
CALL METHOD OF v_excel 'Workbooks' = v_book.
*这个是调用excel.application 里面的方法创建一个excel.workbook.
CALL METHOD OF v_book 'Open' = v_book
EXPORTING
#1 = pa_path.
按照路径打开这个excel.
CALL METHOD OF v_book 'Sheets' = v_sheet
EXPORTING
#1 = 1.
到这个指定的 sheet.
然后按照指定的格式向这个excel里面写数据了
DATA: date LIKE sy-datum.
ls_indcatortxt = '正在导出EXCEL,请稍等。。。'.
PERFORM frm_process_indcator USING ls_indcatortxt 0 .
LOOP AT i_itobs INTO wa_itob.
CONCATENATE '申请号:' wa_itob-lsm INTO wa_itob-lsm.
PERFORM f_write_cell USING 2 1 wa_itob-lsm.
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
ENDLOOP.
FORM f_write_cell USING pa_row
pa_col
pa_val.
CALL METHOD OF v_sheet 'Cells' = v_cell
EXPORTING
#1 = pa_row
#2 = pa_col.
SET PROPERTY OF v_cell 'Value' = pa_val.
ENDFORM. " f_WRITE_CELL
*--设置excel可见
CALL METHOD OF v_excel 'Worksheets' = v_sheet
EXPORTING
#1 = 1.
CALL METHOD OF v_sheet 'Activate'.
SET PROPERTY OF v_excel 'Visible' = 1.
保存excel
SET PROPERTY OF v_excel 'DisplayAlerts' = 0.
CALL METHOD OF v_book 'SAVEAS'
EXPORTING
#1 = pa_fullpath.
CALL METHOD OF v_book 'Exit' = v_book.
FREE OBJECT v_excel.
注意一定要用FREE OBJECT v_excel. 来释放这个OBJECT avoid memory bottle necks
之前,我们要从内存中将模版导出来:
DATA: l_name LIKE wwwdatatab,
l_mine LIKE w3mime OCCURS 10,
l_title TYPE string,
l_filename TYPE string,
l_path TYPE string,
l_fullpath TYPE string.
l_name-relid = 'MI'.
l_name-objid = 'ZPMF0004'. "上传模板名
l_name-text = sy-title.
CONCATENATE '固定资产调拨申请单_' sy-uname '_' sy-datum INTO l_title . "---------------------
*模板下载
CALL FUNCTION 'WWWDATA_IMPORT'
EXPORTING
key = l_name
TABLES
mime = l_mine
EXCEPTIONS
wrong_object_type = 1
import_error = 2
OTHERS = 3.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = l_title
default_extension = 'xls'
default_file_name = l_title
file_filter = '(电子表格EXCEL)'
CHANGING
filename = l_filename
path = l_path
fullpath = l_fullpath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF sy-subrc <> 0.
STOP.
ENDIF. CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = l_fullpath
filetype = 'BIN'
TABLES
data_tab = l_mine.