一个不错的方案:
WHEN 'EXPORT'. "导出数据
DATA : GT_TEMP TYPE TABLE OF TY_ITEM WITH HEADER LINE.
LOOP AT GT_IMERR INTO GT_TEMP.
APPEND GT_TEMP.
ENDLOOP.
INCLUDE OLE2INCL.
DATA : EXCEL TYPE OLE2_OBJECT, "excel对象
SHEET TYPE OLE2_OBJECT, "sheet工作簿页对象
CELL TYPE OLE2_OBJECT, "cell单元格对象
WORKBOOK TYPE OLE2_OBJECT, "workbook工作簿
XLSNAME TYPE STRING VALUE 'C:\导入失败数据.xls',
LINE TYPE I VALUE . "行号 CREATE OBJECT EXCEL 'EXCEL.APPLICATION'. "启动excel IF SY-SUBRC NE .
WRITE : / 'excel启动失败...'.
STOP.
ENDIF. CALL METHOD OF EXCEL 'WORKBOOKS' = WORKBOOK.
SET PROPERTY OF EXCEL 'VISIBLE' = . "使excel可见
SET PROPERTY OF EXCEL 'SHEETSINNEWWORKBOOK' = . "如果是读取excel的内容,则直接打开excel的第一页
CALL METHOD OF WORKBOOK 'ADD'. "例如:CALL METHOD OF EXCEL 'WORKSHEETS' = SHEET EXPORTING #1 = 1. LINE = LINE + .
CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = . "指定单元格
SET PROPERTY OF CELL 'VALUE' = '客户编号'. "写入值 CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = '客户名称'. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = '销售组织'. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = '产品大类'. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = '物料组'. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = '省区'. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = '城市'. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = '交货工厂'. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = '库存地点'. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = '价格清单'. LOOP AT GT_IMERR INTO GS_ITEM.
LINE = LINE + . "excel中行号从1开始 CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = . "指定单元格
SET PROPERTY OF CELL 'VALUE' = GS_ITEM-KUNNR. "写入值 CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = GS_ITEM-NAME1. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = GS_ITEM-VKORG. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = GS_ITEM-AUGRU. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = GS_ITEM-MATKL. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = GS_ITEM-ZXSHF. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = GS_ITEM-ZXSZS. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = GS_ITEM-WERKS. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = GS_ITEM-LGORT. CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING # = LINE # = .
SET PROPERTY OF CELL 'VALUE' = GS_ITEM-PLTYP. ENDLOOP. GET PROPERTY OF EXCEL 'ACTIVESHEET' = SHEET. "激活工作簿
GET PROPERTY OF EXCEL 'ACTIVEWORKBOOK' = WORKBOOK. "激活工作区 CALL METHOD OF WORKBOOK 'SAVEAS' EXPORTING # = XLSNAME # = . "将excel文件保存
* CALL METHOD OF WORKBOOK 'CLOSE'. "关闭工作区
* CALL METHOD OF EXCEL 'QUIT'. "退出excel WRITE : / XLSNAME, 'DONE'. "退出成功,输出done FREE OBJECT SHEET. "释放操作
FREE OBJECT WORKBOOK.
FREE OBJECT EXCEL.
ENDCASE.