ABAP随笔-EXCEL笔记 6-批量导出-OLE客制导出

2022-06-27 16:36:40 浏览数 (1)

01

OLE客制EXCEL导出

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

结果:

部分代码如下

代码语言:javascript复制
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' = '物料属性表'."修改sheet的名字
*给第一列写上列名  
  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."设置单元格颜色 具体可以查询VBA颜色查询
  ENDLOOP.

  DATA(lv_row) = 2.
*给Excel写入内容
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.    

关于

代码语言:javascript复制
zcl_document_jxzhu=>download_template_to_frontend的方法具体内容

ABAP随笔-EXCEL操作笔记 1-Excel模板下载

0 人点赞