结果:
代码:
"导出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.