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.

相关推荐
开心点幸运点4 小时前
Excel——宏教程(1)
excel
boy快快长大13 小时前
将大模型生成数据存入Excel,并用增量的方式存入Excel
java·数据库·excel
Leuanghing13 小时前
使用Python生成F分布表并导出为Excel文件
开发语言·python·excel·f分布
爱编程的小生13 小时前
Easyexcel(4-模板文件)
java·excel
今日之风甚是温和15 小时前
【Excel】拆分多个sheet,为单一表格
java·excel·sheet·vb宏
如意机反光镜裸15 小时前
Excel如何批量导入图片
excel
滨HI01 天前
python中Pandas操作excel补全内容
python·excel·pandas
Leuanghing1 天前
使用Python生成卡方分布表并导出为Excel文件
python·excel·pandas·scipy·卡方分布表
叮当喵是mao1 天前
python基础知识(七)——写入excel
android·python·excel
阿呆5912 天前
使用 Excel 的功能来快速在 TXT 文件中添加一列零
excel