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.

相关推荐
热心市民运维小孙12 小时前
Ubuntu重命名默认账户
linux·ubuntu·excel
又蓝19 小时前
使用 Python 操作 Excel 表格
开发语言·python·excel
m0_748230941 天前
Rust赋能前端: 纯血前端将 Table 导出 Excel
前端·rust·excel
Swift社区1 天前
Excel 列名称转换问题 Swift 解答
开发语言·excel·swift
neeef_se1 天前
Vue中使用a标签下载静态资源文件(比如excel、pdf等),纯前端操作
前端·vue.js·excel
℘团子এ1 天前
js和html中,将Excel文件渲染在页面上
javascript·html·excel
菠萝蚊鸭1 天前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
一只小灿灿1 天前
VB.NET在 Excel 二次开发中的全面应用
.net·excel
是小崔啊2 天前
开源轮子 - EasyExcel01(核心api)
java·开发语言·开源·excel·阿里巴巴
Excel_easy2 天前
批量识别工作表中二维码信息-Excel易用宝
excel·wps