abap将内表数据导出为excel文件详解编程语言

一个不错的方案: 

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 0. "行号   CREATE OBJECT EXCEL 'EXCEL.APPLICATION'. "启动excel   IF SY-SUBRC NE 0.     WRITE : / 'excel启动失败...'.     STOP.   ENDIF.   CALL METHOD OF EXCEL 'WORKBOOKS' = WORKBOOK.   SET PROPERTY OF EXCEL 'VISIBLE' = 1.         "使excel可见   SET PROPERTY OF EXCEL 'SHEETSINNEWWORKBOOK' = 1. "如果是读取excel的内容,则直接打开excel的第一页   CALL METHOD OF WORKBOOK 'ADD'.           "例如:CALL METHOD OF EXCEL 'WORKSHEETS' = SHEET EXPORTING #1 = 1.   LINE = LINE + 1.   CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 1.  "指定单元格   SET PROPERTY OF CELL 'VALUE' = '客户编号'.   "写入值   CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 2.   SET PROPERTY OF CELL 'VALUE' = '客户名称'.   CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 3.   SET PROPERTY OF CELL 'VALUE' = '销售组织'.   CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 4.   SET PROPERTY OF CELL 'VALUE' = '产品大类'.   CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 5.   SET PROPERTY OF CELL 'VALUE' = '物料组'.   CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 6.   SET PROPERTY OF CELL 'VALUE' = '省区'.   CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 7.   SET PROPERTY OF CELL 'VALUE' = '城市'.   CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 8.   SET PROPERTY OF CELL 'VALUE' = '交货工厂'.   CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 9.   SET PROPERTY OF CELL 'VALUE' = '库存地点'.   CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 10.   SET PROPERTY OF CELL 'VALUE' = '价格清单'.   LOOP AT GT_IMERR INTO GS_ITEM.     LINE = LINE + 1. "excel中行号从1开始     CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 1. "指定单元格     SET PROPERTY OF CELL 'VALUE' = GS_ITEM-KUNNR.            "写入值     CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 2.     SET PROPERTY OF CELL 'VALUE' = GS_ITEM-NAME1.     CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 3.     SET PROPERTY OF CELL 'VALUE' = GS_ITEM-VKORG.     CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 4.     SET PROPERTY OF CELL 'VALUE' = GS_ITEM-AUGRU.     CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 5.     SET PROPERTY OF CELL 'VALUE' = GS_ITEM-MATKL.     CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 6.     SET PROPERTY OF CELL 'VALUE' = GS_ITEM-ZXSHF.     CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 7.     SET PROPERTY OF CELL 'VALUE' = GS_ITEM-ZXSZS.     CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 8.     SET PROPERTY OF CELL 'VALUE' = GS_ITEM-WERKS.     CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 9.     SET PROPERTY OF CELL 'VALUE' = GS_ITEM-LGORT.     CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = LINE #2 = 10.     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 #1 = XLSNAME #2 = 1. "将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.

 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/18594.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论