首页 > 其他分享 >OLE 导出类

OLE 导出类

时间:2023-05-29 11:26:27浏览次数:33  
标签:OLE data 导出 lv type TYPE METHOD string

class ZCL_EXCEL_DOWNLOAD definition
  public
  final
  create public .

public section.

  data G_FILEPATH type RLGRAP-FILENAME .

  methods CONSTRUCTOR
    importing
      value(I_DEFAULT_FILENAME) type STRING optional
      value(I_OBJID) type W3OBJID optional
      value(I_FILEPATH) type RLGRAP-FILENAME optional .
  class-methods DOWNLOAD_TEMPLATE
    importing
      value(I_DEFAULT_FILENAME) type STRING optional
      value(I_OBJID) type WWWPARAMS-OBJID optional
    exporting
      value(E_PATH) type RLGRAP-FILENAME .
  methods OPEN_EXCEL
    importing
      value(I_SHEET_NAME) type STRING optional .
  methods SET_SINGLE_CELL
    importing
      value(I_ROW) type I
      value(I_COL) type I
      value(I_VALUE) type ANY default 3
      value(I_HA) type I optional .
  methods SET_ROW_CELL
    importing
      !LT_ITAB type STANDARD TABLE
      value(I_ROW) type I
      value(I_COL) type I .
  methods SET_RANGE_CELL
    importing
      !LT_ITAB type STANDARD TABLE
      value(I_ROW) type I
      value(I_COL) type I .
  methods CLOSE_EXCEL
    importing
      !I_CLOSE type C default 'X' .
  methods INS_ROW
    importing
      value(COPY) type I
      value(ROW) type I
      value(COUNT) type I .
protected section.
private section.

  data G_EXCEL type OLE2_OBJECT .
  data G_WORKBOOKS type OLE2_OBJECT .
  data G_WORKBOOK type OLE2_OBJECT .
  data G_SHEET type OLE2_OBJECT .
ENDCLASS.



CLASS ZCL_EXCEL_DOWNLOAD IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_EXCEL_DOWNLOAD->CLOSE_EXCEL
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_CLOSE                        TYPE        C (default ='X')
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD close_excel.
    SET PROPERTY OF g_excel 'DisplayAlerts' = 'false'.  "关闭保存提示。
    CALL METHOD OF
      g_workbook
      'SAVEAS'
      EXPORTING
        #1 = g_filepath. " #2 = 1.
    IF i_close = 'X'.
      CALL METHOD OF g_workbook 'CLOSE'. "关闭工作区
      CALL METHOD OF g_excel 'QUIT'."退出excel
    ENDIF .
    FREE OBJECT g_excel .
    FREE OBJECT g_workbooks .
    FREE OBJECT g_workbook .
    FREE OBJECT g_sheet .
    CLEAR:g_filepath .
  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_EXCEL_DOWNLOAD->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_DEFAULT_FILENAME             TYPE        STRING(optional)
* | [--->] I_OBJID                        TYPE        W3OBJID(optional)
* | [--->] I_FILEPATH                     TYPE        RLGRAP-FILENAME(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD constructor.
    DATA:
      l_filename  TYPE string,     " 文件名
      l_extension TYPE string ,   "文件后缀
      l_path      TYPE string,     " 路径
      l_fullpath  TYPE string.     " 全路径
    DATA:lv_title    TYPE string,
         lv_filename TYPE string.
    DATA:ls_template TYPE wwwdatatab."WEB资源库文件信息
    DATA:lv_subrc TYPE sysubrc .
    DATA:lv_file_filter TYPE string .
    IF i_filepath IS NOT INITIAL .
      g_filepath = i_filepath .
      RETURN .
    ENDIF .

    CHECK i_objid IS NOT INITIAL .

    CALL METHOD download_template
      EXPORTING
        i_default_filename = i_default_filename
        i_objid            = i_objid
      IMPORTING
        e_path             = g_filepath.
  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_EXCEL_DOWNLOAD=>DOWNLOAD_TEMPLATE
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_DEFAULT_FILENAME             TYPE        STRING(optional)
* | [--->] I_OBJID                        TYPE        WWWPARAMS-OBJID(optional)
* | [<---] E_PATH                         TYPE        RLGRAP-FILENAME
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD download_template.
    DATA:
      l_filename  TYPE string,     " 文件名
      l_extension TYPE string ,   "文件后缀
      l_path      TYPE string,     " 路径
      l_fullpath  TYPE string.     " 全路径
    DATA:lv_title    TYPE string,
         lv_filename TYPE string.
    DATA:ls_template TYPE wwwdatatab."WEB资源库文件信息
    DATA:lv_subrc TYPE sysubrc .
    DATA:lv_file_filter TYPE string .

    DATA: lv_directory TYPE string,
          lv_result    TYPE abap_bool.
    DATA:lv_extension(64).
    CALL FUNCTION 'WWWPARAMS_READ'
      EXPORTING
        relid            = 'MI'
        objid            = i_objid
        name             = 'fileextension'
      IMPORTING
        value            = lv_extension
      EXCEPTIONS
        entry_not_exists = 1
        OTHERS           = 2.
    IF sy-subrc <> 0.
*      实例不存在!
      MESSAGE s139(zmsg) DISPLAY LIKE 'E'.
      RETURN.
    ENDIF.


    IF lv_extension = '.xlsx' OR lv_extension = '.XLSX'.
      lv_file_filter = '*.xlsx|*.xlsx|*.xls|*.xls' .
    ELSEIF lv_extension = '.xls' OR   lv_extension = '.XLS' .
      lv_file_filter = '*.xls|*.xls|*.xlsx|*.xlsx' .
    ENDIF .
    l_extension = lv_extension.

    lv_directory =   'D:\' .
*    call method cl_gui_frontend_services=>directory_exist
*      exporting
*        directory            = lv_directory
*      receiving
*        result               = lv_result
*      exceptions
*        cntl_error           = 1
*        error_no_gui         = 2
*        wrong_parameter      = 3
*        not_supported_by_gui = 4
*        others               = 5.
*    if lv_result = abap_false.
*      clear:lv_directory.
*    endif.

    CALL METHOD cl_gui_frontend_services=>file_save_dialog   "保存文件路径对话框
      EXPORTING
        window_title         = 'Download Template'
        default_extension    = l_extension
        default_file_name    = i_default_filename
        file_filter          = lv_file_filter
        initial_directory    = lv_directory
      CHANGING
        filename             = l_filename
        path                 = l_path
        fullpath             = l_fullpath
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        not_supported_by_gui = 3
        OTHERS               = 4.

    CHECK l_fullpath IS NOT INITIAL.

    CLEAR ls_template.
    ls_template-relid = 'MI'.
    ls_template-objid = i_objid.
    DATA:lv_filepath TYPE rlgrap-filename.
    e_path = lv_filepath = l_fullpath .

    CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
      EXPORTING
        key         = ls_template
        destination = lv_filepath
      IMPORTING
        rc          = lv_subrc.

    IF lv_subrc NE  0 .
*      下载模板失败!
      MESSAGE s140(zmsg) DISPLAY LIKE 'E'.
    ENDIF .
  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_EXCEL_DOWNLOAD->INS_ROW
* +-------------------------------------------------------------------------------------------------+
* | [--->] COPY                           TYPE        I
* | [--->] ROW                            TYPE        I
* | [--->] COUNT                          TYPE        I
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD ins_row.
    DATA: lc_range TYPE ole2_object.

    DO count TIMES.
      CALL METHOD OF g_sheet 'Rows' = lc_range
       EXPORTING #1 = copy.

      CALL METHOD OF lc_range 'Copy'.

      CALL METHOD OF g_sheet 'Rows' = lc_range
       EXPORTING #1 = row.

      CALL METHOD OF lc_range 'Insert'.

      CALL METHOD OF lc_range 'ClearContents'.
    ENDDO.
  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_EXCEL_DOWNLOAD->OPEN_EXCEL
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_SHEET_NAME                   TYPE        STRING(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD open_excel.
    CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
      EXPORTING
*       PERCENTAGE = 0
        text = 'Data Loading.....'.
    CREATE OBJECT g_excel 'EXCEL.APPLICATION'.
    SET PROPERTY OF g_excel 'VISIBLE' = 0.

    CALL METHOD OF
      g_excel
        'WORKBOOKS' = g_workbooks.
    CALL METHOD OF
        g_workbooks
        'OPEN'      = g_workbook
      EXPORTING
        #1          = g_filepath.

    "有多个sheet页时。
    IF i_sheet_name IS NOT INITIAL .
      CALL METHOD OF
          g_workbook
          'WORKSHEETS' = g_sheet
        EXPORTING
          #1           = i_sheet_name.
      CALL METHOD OF
        g_sheet
        'Activate'.
    ELSE.
      CALL METHOD OF
        g_workbook
          'ActiveSheet' = g_sheet.
    ENDIF .
  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_EXCEL_DOWNLOAD->SET_RANGE_CELL
* +-------------------------------------------------------------------------------------------------+
* | [--->] LT_ITAB                        TYPE        STANDARD TABLE
* | [--->] I_ROW                          TYPE        I
* | [--->] I_COL                          TYPE        I
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD set_range_cell.
    CONSTANTS:
   lv_seperator TYPE c VALUE cl_abap_char_utilities=>horizontal_tab. " 值:#
*接口数据表相关
    DATA:wa_itab      TYPE REF TO data,                 "接口内表工作区,
         descr_wa     TYPE REF TO cl_abap_structdescr,  "接口内表工作区结构信息
         field_no     TYPE i,                           "接口内表工作区组件数量
         field_string TYPE string.                      "行结构组件的值

    FIELD-SYMBOLS:
      <wa_itab> TYPE any,  "接口内表工作区
      <field>   TYPE any.  "接口内表工作区中的组件
*粘贴内表
    DATA: BEGIN OF ls_data ,
            record(4096) TYPE c,
          END OF ls_data.

    DATA: lt_data LIKE TABLE OF ls_data,
          l_rc    TYPE i.
*EXCEL 对象
    DATA: excel_cell TYPE ole2_object.

    CHECK lt_itab[]   IS NOT INITIAL.
*创建接口数据表工作区
    CREATE DATA wa_itab LIKE LINE OF lt_itab.
    ASSIGN wa_itab->* TO <wa_itab>.
*获取内表工作区的组件数量
    descr_wa ?= cl_abap_typedescr=>describe_by_data( <wa_itab> ).
    DESCRIBE TABLE descr_wa->components LINES field_no.

*将接口表数据转存到粘贴内表中
    LOOP AT lt_itab INTO <wa_itab> .
      CLEAR:ls_data.
      IF field_no = 1.
*      ls_data-record = <wa_itab>.
        ASSIGN COMPONENT 1 OF STRUCTURE <wa_itab> TO <field>.
        field_string = <field>.
        ls_data-record = field_string.
      ELSE.
        DO field_no TIMES.
          ASSIGN COMPONENT sy-index OF STRUCTURE <wa_itab> TO <field>.
*        IF sy-subrc <> 0.
*          RAISE field_not_assigned.
*        ENDIF.
          field_string = <field>.
          IF sy-index = 1.
            ls_data-record = field_string.
          ELSE.
            CONCATENATE ls_data-record field_string  INTO ls_data-record SEPARATED BY lv_seperator.
          ENDIF.
        ENDDO.
      ENDIF.
      APPEND ls_data TO  lt_data.
    ENDLOOP.


    cl_gui_frontend_services=>clipboard_export( IMPORTING data = lt_data
                              CHANGING rc = l_rc ).
*设定单元格
    CALL METHOD OF
        g_sheet
        'Cells'         = excel_cell
      EXPORTING
        #1              = i_row
        #2              = i_col.
*选中单元格操作
    CALL METHOD OF
      excel_cell
      'Select'.
*粘贴操作
    CALL METHOD OF
      g_sheet
      'Paste'.
  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_EXCEL_DOWNLOAD->SET_ROW_CELL
* +-------------------------------------------------------------------------------------------------+
* | [--->] LT_ITAB                        TYPE        STANDARD TABLE
* | [--->] I_ROW                          TYPE        I
* | [--->] I_COL                          TYPE        I
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD set_row_cell.
    CONSTANTS:
   lv_seperator TYPE c VALUE cl_abap_char_utilities=>horizontal_tab. " 值:#
*接口数据表相关
    DATA:wa_itab      TYPE REF TO data,                 "接口内表工作区,
         descr_wa     TYPE REF TO cl_abap_structdescr,  "接口内表工作区结构信息
         field_no     TYPE i,                           "接口内表工作区组件数量
         field_string TYPE string.                      "行结构组件的值

    FIELD-SYMBOLS:
      <wa_itab> TYPE any,  "接口内表工作区
      <field>   TYPE any.  "接口内表工作区中的组件
*粘贴内表
    DATA: BEGIN OF ls_data ,
            record(4096) TYPE c,
          END OF ls_data.

    DATA: lt_data LIKE TABLE OF ls_data,
          l_rc    TYPE i.
*EXCEL 对象
    DATA: excel_cell TYPE ole2_object.

    CHECK lt_itab[]   IS NOT INITIAL.
*创建接口数据表工作区
    CREATE DATA wa_itab LIKE LINE OF lt_itab.
    ASSIGN wa_itab->* TO <wa_itab>.

*将接口表数据转存到粘贴内表中
    LOOP AT lt_itab INTO <wa_itab> .
      IF sy-tabix = 1 .
        ls_data-record = <wa_itab>.
      ELSE.
        field_string = <wa_itab>.
        CONCATENATE ls_data-record field_string  INTO ls_data-record SEPARATED BY lv_seperator.
      ENDIF.
    ENDLOOP.

    APPEND ls_data TO  lt_data.
    cl_gui_frontend_services=>clipboard_export( IMPORTING data = lt_data
                              CHANGING rc = l_rc ).
*设定单元格
    CALL METHOD OF
        g_sheet
        'Cells' = excel_cell
      EXPORTING
        #1      = i_row
        #2      = i_col.
*选中单元格操作
    CALL METHOD OF
      excel_cell
      'Select'.
*粘贴操作
    CALL METHOD OF
      g_sheet
      'Paste'.
  ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_EXCEL_DOWNLOAD->SET_SINGLE_CELL
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_ROW                          TYPE        I
* | [--->] I_COL                          TYPE        I
* | [--->] I_VALUE                        TYPE        ANY (default =3)
* | [--->] I_HA                           TYPE        I(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD set_single_cell.
    DATA:lo_cell TYPE ole2_object,
         lo_font TYPE ole2_object.
    CHECK i_value IS NOT INITIAL.

    CALL METHOD OF
        g_excel
        'CELLS' = lo_cell
      EXPORTING
        #1      = i_row
        #2      = i_col.

    SET PROPERTY OF lo_cell 'VALUE' = i_value.

* 设置列宽
*  SET PROPERTY OF lo_cell 'COLUMNWIDTH' = 0.
* 对齐方式  3为居中
*    SET PROPERTY OF lo_cell 'HORIZONTALALIGNMENT' = i_ha.
** 水平对齐方式
*    SET PROPERTY OF lo_cell 'VERTICALALIGNMENT' = 2.
*
*    SET PROPERTY OF lo_cell 'WRAPTEXT' = 0.
*
*    GET PROPERTY OF lo_cell 'FONT' = lo_font.
** 字体加粗
*    SET PROPERTY OF lo_font 'BOLD' = 0.
**字体大小
*    SET PROPERTY OF lo_font 'SIZE' = 11.

    FREE lo_font.
    FREE lo_cell.
  ENDMETHOD.
ENDCLASS.

 

标签:OLE,data,导出,lv,type,TYPE,METHOD,string
From: https://www.cnblogs.com/ZJY-1314/p/17439893.html

相关文章

  • Unity的Console的控制类LogEntries:深入解析与实用案例
    使用UnityConsole窗口的LogEntries私有类实现自定义日志系统在Unity开发过程中,我们经常需要使用Console窗口来查看程序运行时的日志信息。Unity内置的日志系统提供了基本的日志功能,但有时我们需要更多的自定义选项。本文将介绍如何使用UnityConsole窗口的LogEntries私有类来实现......
  • 前端导出 Excel
    需求背景在项目开发中遇到需要前端将后端返回的数据导出的任务,由于后端已经全部返回了相关数据,为避免重复请求接口、减少后端IO提升系统性能,因此需要在前端将后端返回的任务数据导出成Excel。之前没有做过前端根据数据直接导出excel的案例,特此做记录。使用的插件xlsx用于解......
  • RefsUtil 是 Windows 下一款用于管理 REFS 文件系统的实用工具,它提供了丰富的功能和命
    RefsUtil是Windows下一款用于管理REFS文件系统的实用工具,它提供了丰富的功能和命令行界面,可用于创建、修改、检查和修复REFS分区,以及导出和导入数据等操作。以下是一些使用RefsUtil工具的示例:创建REFS分区要创建一个新的REFS分区,可以使用以下命令:CopyCoderef......
  • 非Teams会议组织者如何导出参会列表
    需求背景通常来说,只有会议的组织者才能够从Teams客户端导出Teams会议的参会人列表,或者需要Teams管理员从后台导出。而在某些情况下,需要非Teams会议组织者协助导出次列表,那么我们可以使用Teams会议的共同组织者(Co-organizer)功能来实现这个需求。概念解释邀请他人参加会议后,您最多......
  • Swoole 面试题总结
    swoole提升性能1.进程常驻内存:swoole本⾝是进程常驻内存,在进程启动的时候就将PHP框架等代码读取并编译完成,不需要每次启动的时候都执⾏编译步骤,⼤⼤降低了脚本的运⾏时间;2.连接池php-fpm的模式php因为每次请求结束时都会销毁所有资源,因此⽆法使⽤连接池;⽽基于swoole的进程常驻......
  • 自定义导出Excel表列数据(根据所选的列,选几列导出几列数据,不选则全部导出)
       ......
  • solidworks vba 导出数据到文本文件
    Thismacroexportsalltheinformationintotheoutputtextfileinthefollowingformat:OptionExplicitConstcurrentDirAsString="D:\"'DimswAppAsSldWorks.SldWorksSubmain()DimfilePathAsStringfilePath=currentDi......
  • expdp 导出缓慢
    expdp导出缓慢查询等待事件,目前导出的等待事件是:selectinst_id,sql_id,event,count(*)fromgv$sessionwherewait_class<>'Idle'groupbyinst_id,sql_id,eventorderbycount(*)desc;INST_IDEVENTCOUNT(1)---------......
  • 野火RA6M5开发板 DHT11温湿度传感器 OLED显示测试学习
    野火RA6M5开发板DHT11温湿度传感器OLED显示测试学习DHT11温湿度传感器DHT11是一款有已校准数字信号输出的温湿度传感器。精度湿度+-5%RH,温度+-2℃,量程湿度20-90%RH,温度0~50℃。更多DHT11信息请参考:https://baike.sogou.com/v73984313.htm?fromTitle=DHT11下图为DHT11的引......
  • 解决mysqldump 导出中文乱码的问题
    导数据库mysqldump-uroot-p111111-P3306-h127.0.0.1test>/data/test.sql导出后的数据库打开是乱码,如下:开始以为打开的方式不对,就用记事本打开后,用utf-8的编码格式另保存下结果打开后,仍然是乱码。这时候,猜测是不是数据库的字符集的编码有问题,然后进入数据库,输入命......