SAP ABAP OLE 操作EXCEL小结说课讲解Word下载.docx
《SAP ABAP OLE 操作EXCEL小结说课讲解Word下载.docx》由会员分享,可在线阅读,更多相关《SAP ABAP OLE 操作EXCEL小结说课讲解Word下载.docx(8页珍藏版)》请在冰豆网上搜索。
2.在程序中下载模板
*下载EXCEL模板FORM
FORM
temp_excel_get.
DATA:
lo_objdataLIKEwwwdatatab,
lo_mimeLIKEw3mime,
lc_filename
TYPEstringVALUE'
test01'
lc_fullpath
TYPEstring
VALUE'
D:
\test\'
lc_path
TYPE
stringVALUE'
ls_destinationLIKErlgrap-filename,
ls_objnamTYPEstring,
li_rcLIKEsy-subrc,
ls_errtxtTYPEstring.
p_objidTYPEwwwdatatab-objid,
p_destLIKEsapb-sappfad.
p_objid='
ZTEST.XLS'
.“此处为EXCEL模板名称
CALLMETHODcl_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.
IFlc_fullpath='
'
.
MESSAGE
'
不能打开excel'
TYPE'
E'
ENDIF.
IFsy-subrc=0.
p_dest=lc_fullpath.
*
concatenatep_objid'
.XLS'
intols_objnam.
CONDENSEls_objnamNO-GAPS.
SELECTSINGLErelidobjidFROMwwwdataINTOCORRESPONDINGFIELDSOFlo_objdata
WHEREsrtf2=0ANDrelid='
MI'
ANDobjid=p_objid.
*检查表wwwdata中是否存在所指定的模板文件
IFsy-subrcNE0ORlo_objdata-objidEQspace.“如果不存在,则给出错误提示
CONCATENATE'
模板文件'
ls_objnam'
不存在'
INTOls_errtxt.
MESSAGEls_errtxtTYPE'
I'
ls_destination=p_dest.”保存路径
*如果存在,调用DOWNLOAD_WEB_OBJECT函数下载模板到路径下
CALLFUNCTION'
DOWNLOAD_WEB_OBJECT'
key
=lo_objdata
destination=ls_destination
IMPORTING
rc
=li_rc.
IFli_rcNE0.
模板文件:
下载失败'
fname=ls_destination.
ENDFORM.
"
fm_excel
3.下载完模板后,打开模板文件,填入数据
CREATEOBJECTexcel'
EXCEL.APPLICATION'
.
CreateEXCELOBJECT
IFsy-subrcNE0.
EXIT.
SETPROPERTYOFexcel'
Visible'
=0.
1/0是否显示EXCEL
CALLMETHODOFexcel'
Workbooks'
=workbook.
CALLMETHODOFworkbook'
Open'
#1=fname.."
打开上面下载路径下的excel文件
Sheets'
=sheet
#1=1.
CALLMETHODOFsheet'
Select'
.
ACTIVATE'
.“sheet激活
SETPROPERTYOFsheet'
NAME'
=sheetname.“设定sheet名称
*此处假设内表itab中已经存在需要写入excel中的数据
*并且假如从模板的第7行开始插入数据
LOOPATitabINTOwa.
tab=wa-tabix+6.
*在excel中插入一行
PERFORMexcel_row_insertUSINGsheettab1.
*填充所插入行的每个单元格的数据
PERFORMfill_rangeUSINGtab1wa-t01.
PERFORMfill_rangeUSINGtab2wa-t02.
PERFORMfill_rangeUSINGtab3wa-t03.
PERFORMfill_rangeUSINGtab4wa-t04.
PERFORMfill_rangeUSINGtab5wa-t05.
PERFORMfill_rangeUSINGtab6wa-t06.
ENDLOOP.
*设置EXCEL中所插入的数据行边框线格式为黑色有边框
bod=tab.
CONDENSEbodNO-GAPS.
A6:
F'
bodINTObod.
PERFORMborderrangeUSINGexcelbod.
PERFORMsub_excel_save."
保存excel数据
*&
---------------------------------------------------------------------*
Form
sub_excel_save
text
*----------------------------------------------------------------------*
FORMsub_excel_save.
GETPROPERTYOFexcel'
ActiveSheet'
=sheet.“获取活动SHEET
FREEOBJECTsheet.
FREEOBJECTworkbook.
ActiveWorkbook'
SAVE'
=1.
是否显示EXCEL此处显示不退出
*SETPROPERTYOFexcel'
=1.
CLOSE'
.
QUIT'
.注释部分为不显示直接退出
FREEOBJECTexcel.
save_book
向excel中的指定行插入N行
FORMexcel_row_insertUSINGlcobj_sheet
lc_row
lc_count.
DATAlc_rangeTYPEole2_object.
DATAh_borders
TYPEole2_object.
DOlc_countTIMES.
CALLMETHODOFlcobj_sheet'
Rows'
=lc_range
EXPORTING#1=6.
CALLMETHODOFlc_range'
Copy'
“COPY第6行插入一个新行
EXPORTING#1=lc_row.
Insert'
ClearContents'
."
是否需要清空Cell
ENDDO.