ABAP 导出数据到EXCEL ,OLE

结果:

代码:

"导出EXCEL,OLE

TYPE-POOLS:OLE2.

DATA: GS_EXCEL TYPE OLE2_OBJECT ,

GS_WBOOKLIST TYPE OLE2_OBJECT ,

GS_APPLICATION TYPE OLE2_OBJECT ,

GS_WBOOK TYPE OLE2_OBJECT ,

GS_ACTIVESHEET TYPE OLE2_OBJECT ,

GV_SHEET_NAME(20) TYPE C,

GS_CELL1 TYPE OLE2_OBJECT ,

GS_CELL2 TYPE OLE2_OBJECT ,

GS_CELLS TYPE OLE2_OBJECT ,

SHEET TYPE OLE2_OBJECT,

GV_LINNO TYPE I.

DATA:LV_SELECTED_FOLDER TYPE STRING,LS_DESTINATION LIKE RLGRAP-FILENAME,P_OBJID TYPE WWWDATATAB-OBJID.

"用户选择路径 ,也可默认路径

CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE

CHANGING

SELECTED_FOLDER = LV_SELECTED_FOLDER

EXCEPTIONS

CNTL_ERROR = 1

ERROR_NO_GUI = 2

NOT_SUPPORTED_BY_GUI = 3

OTHERS = 4.

IF SY-SUBRC <> 0.

ENDIF.

IF LV_SELECTED_FOLDER = ''.

MESSAGE '保存路径为空!' TYPE 'S'.

EXIT.

ENDIF.

CONCATENATE LV_SELECTED_FOLDER '\' SY-DATUM '_' SY-UZEIT '.XLS' INTO LV_SELECTED_FOLDER."命名文件名称

GV_SHEET_NAME = 'SHEET1'."工作表命名

CREATE OBJECT GS_EXCEL 'EXCEL.APPLICATION' .

IF SY-SUBRC <> 0.

MESSAGE '不能打开EXCEL 对象,请检查系统是否安装Microsoft Excel!' TYPE 'S'.

EXIT.

ENDIF.

GET PROPERTY OF GS_EXCEL 'Workbooks' = GS_WBOOKLIST .

GET PROPERTY OF GS_WBOOKLIST 'Application' = GS_APPLICATION .

SET PROPERTY OF GS_APPLICATION 'SheetsInNewWorkbook' = 1 .

SET PROPERTY OF GS_APPLICATION 'DisplayAlerts' = 0.

CALL METHOD OF GS_WBOOKLIST 'Add' = GS_WBOOK .

GET PROPERTY OF GS_APPLICATION 'ActiveSheet' = GS_ACTIVESHEET .

SET PROPERTY OF GS_ACTIVESHEET 'Name' = GV_SHEET_NAME ."工作表命名

SET PROPERTY OF GS_EXCEL 'Visible' = 1."0 为后台运行,1为前台运行(打开EXCEL)。

DO 2 TIMES ."赋值

GV_LINNO = GV_LINNO + 1.

CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL1

EXPORTING

#1 = GV_LINNO

#2 = 1.

CALL METHOD OF GS_EXCEL 'Cells' = GS_CELL2

EXPORTING

#1 = GV_LINNO

#2 = 1.

CALL METHOD OF GS_EXCEL 'Range' = GS_CELLS "是否合并

EXPORTING

#1 = GS_CELL1

#2 = GS_CELL2.

CALL METHOD OF GS_CELLS 'Merge' .

* CALL METHOD OF GS_CELLS 'SELECT'.

SET PROPERTY OF GS_CELL1 'Value' = GV_LINNO."赋值

ENDDO.

WAIT UP TO '0.5' SECONDS.

GET PROPERTY OF GS_EXCEL 'ACTIVESHEET' = SHEET.

GET PROPERTY OF GS_EXCEL 'ACTIVEWORKBOOK' = GS_WBOOK.

*CALL METHOD OF GS_WBOOK 'SAVE'."AS' EXPORTING #1 = LV_SELECTED_FOLDER #2 = 1.

CALL METHOD OF GS_WBOOK 'SAVEAS' EXPORTING #1 = LV_SELECTED_FOLDER #2 = 1.

CALL METHOD OF GS_WBOOK 'CLOSE'.

CALL METHOD OF GS_EXCEL 'QUIT'.

FREE: GS_EXCEL,GS_WBOOKLIST,GS_APPLICATION,GS_WBOOK,GS_ACTIVESHEET,GS_CELL1,GS_CELL2,GS_CELLS,SHEET.

相关推荐
赵孝正3 小时前
自动用 Excel 转 .CSV 为 .xlsx 的原理
excel
互联网上的猪3 小时前
Excel时间类型函数(包括today、date、eomonth、year、month、day、weekday、weeknum、datedif)
笔记·学习·excel
专注VB编程开发20年6 小时前
无需安装Office进行 Word、Excel操作的微软开发库
microsoft·word·excel
hello_simon16 小时前
在线Excel 转换为 txt ,超方便超易用软件,在线转换,大力提升工作效率
excel
云只上1 天前
前端界面在线excel编辑器 。node编写post接口获取文件流,使用传参替换表格内容展示、前后端一把梭。
前端·javascript·node.js·excel
Ariel_提拉米苏1 天前
表格数据导出为Excel
前端·javascript·vue.js·excel
inxunoffice1 天前
导入 Excel 规则批量修改 txt/html/json/xml/csv 等记事本文本文件内容
xml·excel
loong_XL1 天前
AI excel表格分析:WPS、chatexcel
excel·wps
寒山独见君~1 天前
【Office办公】【Excel】VLOOKUP函数-高速查找指定匹配数据,可合并2个表格
excel
inxunoffice1 天前
根据模板将 Excel 明细数据生成 Txt 文档|邮件合并
excel