• 2020.03.03 【ABAP随笔】- Excel批量导出6-OLE客制导出


    今天简单说一下使用OLE的方法来输出自己想要的EXCEL。

    结果:

    部分代码如下:

    FORM frm_export_excel_2 .
    
      DATA ls_destination TYPE rlgrap-filename.
      DATA ls_filename TYPE string.
    * 写入数据
      SELECT * FROM zmmt001 INTO CORRESPONDING FIELDS OF TABLE lt_zmmt001 .
      READ TABLE lt_zmmt001 INTO DATA(ls_zmmt001) INDEX 1.
      DATA lv_tabix TYPE i.
      DATA tablestructure TYPE REF TO cl_abap_structdescr.
      tablestructure ?= cl_abap_typedescr=>describe_by_data( ls_zmmt001 ).
    
      ls_filename = p_file.
      ls_destination = p_file.
    
      CALL METHOD zcl_document_jxzhu=>download_template_to_frontend
        EXPORTING
          iv_fun           = '2'
          name_of_template = '物料主数据客制表'
          iv_filepath      = ls_filename
    *     iv_relid         = 'MI'
          iv_objid         = 'ZMMT001'
        IMPORTING
          rv_filepath      = ls_filename
        EXCEPTIONS
          download_error   = 1
          OTHERS           = 2.
    
      WAIT UP TO 2 SECONDS.
    
    *open excel
    
      CREATE OBJECT excel_obj 'EXCEL.APPLICATION'.
      IF sy-subrc NE 0.
        MESSAGE 'EXCEL创建错误' TYPE 'S' DISPLAY LIKE 'E'.
        STOP.
      ENDIF.
    
      CALL METHOD OF
        excel_obj
          'WORKBOOKS' = book_obj.
      SET PROPERTY OF excel_obj 'VISIBLE' = 0.
    *  SET PROPERTY OF EXCEL_OBJ 'SheetInNewWorkbook' = 1.
    
    * 打开sheet页,(新建使用:call method of book_obj 'Add' = sheet_obj)
      CALL METHOD OF
      book_obj
      'Open'   = sheet_obj
      EXPORTING
       #1       = ls_destination.
    
      CALL METHOD OF
          excel_obj
          'Sheets'  = sheet_obj"切换sheets
        EXPORTING
          #1        = 1.
    
      CALL METHOD OF
        sheet_obj
        'Select'.
    
      CALL METHOD OF sheet_obj 'ACTIVATE'."激活
    
      SET PROPERTY OF sheet_obj 'NAME' = '物料属性表'.
    
      LOOP AT tablestructure->components INTO DATA(ls_comps).
        lv_tabix = sy-tabix.
        PERFORM fill_cell USING 1 sy-tabix ls_comps-name .
        GET PROPERTY OF cell_obj 'Interior' = color_obj.
        SET PROPERTY OF color_obj 'ColorIndex' = 15.
      ENDLOOP.
    
      DATA(lv_row) = 2.
      LOOP AT lt_zmmt001 INTO ls_zmmt001.
        LOOP AT  tablestructure->components INTO ls_comps.
          lv_tabix = sy-tabix.
          ASSIGN COMPONENT lv_tabix OF STRUCTURE ls_zmmt001 TO FIELD-SYMBOL(<fs1>).
          IF sy-subrc EQ 0.
            PERFORM fill_cell USING lv_row lv_tabix <fs1> .
          ENDIF.
        ENDLOOP.
        lv_row = lv_row + 1.
      ENDLOOP.
    
    
      FREE OBJECT cell_obj.
      GET PROPERTY OF excel_obj 'ActiveSheet' = sheet_obj. "获取活动SHEET
    
    
      FREE OBJECT sheet_obj.
      GET PROPERTY OF excel_obj 'ACTIVEWORKBOOK' = book_obj."激活工作区
      "save
      CALL METHOD OF
        book_obj
        'SAVE'.
    *   SET PROPERTY OF excel 'Visible' = 1.  "是否显示EXCEL 此处显示不退出
      CALL METHOD OF
        book_obj
        'CLOSE'.
    
      CALL METHOD OF
        excel_obj
        'QUIT'.
    
      FREE OBJECT book_obj.
      FREE OBJECT excel_obj.
    
    ENDFORM.
    FORM fill_cell  USING "设置单元格的值
                             VALUE(row)
                             VALUE(col)
                             VALUE(value).
      CALL METHOD OF
          excel_obj
          'CELLS'   = cell_obj
        EXPORTING
          #1        = row
          #2        = col.
      SET PROPERTY OF cell_obj 'VALUE' = value.
    
    ENDFORM.

    关于

    zcl_document_jxzhu=>download_template_to_frontend的方法具体内容:https://www.cnblogs.com/jxzhu/p/12359692.html
    -Tab Zhu 不念过去 不畏将来

  • 相关阅读:
    python浏览本地文件,实现文件路径的选择
    《一线架构师实践指南》--- Refined Architecture
    第七周学习记录
    第五周学习记录
    中国国民经济行业分类数据下载(含爬取代码)
    python机器学习kmeans算法——对文本进行聚类分析
    TensorFlow2深度学习
    深度学习---Caffe之经典模型详解与实战a乐毅,王斌a
    《深度学习实践-基于Caffe的解析》_薛云峰
    Python带我起飞——入门、进阶、商业实战
  • 原文地址:https://www.cnblogs.com/jxzhu/p/12399176.html
Copyright © 2020-2023  润新知