SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景

文章目录

  • 前言
  • 使用 gui_upload 从本地电脑读取文件
  • 使用 gui_download 将文件写入本地电脑
  • 与服务器端文件有关的基本事务
  • 向应用服务器写数据
  • 从应用服务器读取文件
  • 从 Microsoft Excel 文件读取数据
  • 将数据保存到 Microsoft Excel 文件
  • 将数据保存到 Microsoft Word 文件中
  • 在 ABAP 报告中创建桌面办公集成 (DOI)
  • 其他技术
  • 总结


前言

要使本章中的所有示例都能正常运行,必须满足以下要求: 桌面 Office 集成 (DOI) 和对象链接与嵌入 (OLE): 本章中显示的大多数示例都需要安装 Microsoft Office 软件包。


提示:以下是本篇文章正文内容,下面案例可供参考

使用 gui_upload 从本地电脑读取文件

文件处理层面上分为两层:
在本地电脑环境工作交互:
1.一般只有两种:文件上传,文件下载
常用的FM如:
GUI_DOWNLOAD 下载
GUI_UPLOAD 上载

在SAP服务器上做文件处理:
打开数据集 命令:
OPEN DATASET 打开文件
CLOSE DATASET 关闭
DELETE DATASET 删除
READ DATASET 读
TRANSFER 写记录

AL11事务代码,可以查看SAP系统内的 文件


示例操作:

"上传文件
"首先定义一个输入口,选择屏幕创建
PARAMETERS: p_file TYPE localfile.

SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景_第1张图片此时 屏幕显示效果如上图,很显然不是特别方便。因为我们在上传文件时,并不能让用户自己手输文件地址,所以这里我们需要加一个F4help 作为点击事件,

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  CALL FUNCTION 'F4_FILENAME'
   EXPORTING
     PROGRAM_NAME        = SYST-CPROG
     DYNPRO_NUMBER       = SYST-DYNNR
     FIELD_NAME          = ' '
   IMPORTING
     FILE_NAME           = p_file

在这里插入图片描述点击按钮
SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景_第2张图片SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景_第3张图片


完整代码

"上传文件
TYPES: BEGIN OF lty_data,
         eid(10)  TYPE n,
         name(40) TYPE c,
       END OF lty_data.
DATA : lt_data TYPE TABLE OF lty_data.

DATA: lv_file TYPE string.


"首先定义一个输入口,选择屏幕创建
PARAMETERS: p_file TYPE localfile.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  "该函数将桌面的文件地址传给p_file
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
      field_name    = ' '
    IMPORTING
      file_name     = p_file.

START-OF-SELECTION.

  lv_file = p_file.
  "点击执行,上传文件
  "要注意文件类型 string char


  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = lv_file
*     FILETYPE                = 'ASC'
      has_field_separator     = 'X'    "是否有分隔符
*     HEADER_LENGTH           = 0
*     READ_BY_LINE            = 'X'
*     DAT_MODE                = ' '
*     CODEPAGE                = ' '
*     IGNORE_CERR             = ABAP_TRUE
*     REPLACEMENT             = '#'
*     CHECK_BOM               = ' '
*     VIRUS_SCAN_PROFILE      =
*     NO_AUTH_CHECK           = ' '
*     IMPORTING
*     FILELENGTH              =
*     HEADER                  =
    TABLES
      data_tab                = lt_data  "根据文件,会相应生成对应的结构内表,需要定义
*    changing
*     isscanperformed         = ' '
    EXCEPTIONS
      file_open_error         = 1  
      file_read_error         = 2  
      no_batch                = 3  
      gui_refuse_filetransfer = 4  
      invalid_type            = 5  
      no_authority            = 6  
      unknown_error           = 7  
      bad_data_format         = 8  
      header_not_allowed      = 9  
      separator_not_allowed   = 10  
      header_too_long         = 11  
      unknown_dp_error        = 12  
      access_denied           = 13  
      dp_out_of_memory        = 14  
      disk_full               = 15  
      dp_timeout              = 16  
      OTHERS                  = 17.  
  IF sy-subrc <> 0.  
* Implement suitable error handling here


  ENDIF.  

面向对象的示例代码:

CLASS lcl_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS lcl_demo IMPLEMENTATION.
  METHOD main.
    DATA: lv_filepath TYPE string.
    DATA: lt_data TYPE TABLE OF string.

    DATA: lt_filetable TYPE filetable.
    DATA: lv_filetable TYPE file_table.
    DATA: lv_rc TYPE i.

    CALL METHOD cl_gui_frontend_services=>file_open_dialog
      CHANGING
        file_table              = lt_filetable
        rc                      = lv_rc
      EXCEPTIONS
        file_open_dialog_failed = 1
        cntl_error              = 2
        error_no_gui            = 3
        not_supported_by_gui    = 4
        OTHERS                  = 5.

    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      RETURN.
    ELSEIF lv_rc < 1.
      MESSAGE 'No File choosen' TYPE 'W'.
      RETURN.
    ENDIF.

    READ TABLE lt_filetable INTO lv_filepath INDEX 1.

    CALL METHOD cl_gui_frontend_services=>gui_upload
      EXPORTING
        filename = lv_filepath
      CHANGING
        data_tab = lt_data
      EXCEPTIONS
       file_open_error         = 1
       file_read_error         = 2
       no_batch                = 3
       gui_refuse_filetransfer = 4
       invalid_type            = 5
       no_authority            = 6
       unknown_error           = 7
       bad_data_format         = 8
       header_not_allowed      = 9
       separator_not_allowed   = 10
       header_too_long         = 11
       unknown_dp_error        = 12
       access_denied           = 13
       dp_out_of_memory        = 14
       disk_full               = 15
       dp_timeout              = 16
       not_supported_by_gui    = 17
       error_no_gui            = 18
       others                  = 19
      .

    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      RETURN.
    ENDIF.

    cl_demo_output=>display_data( lt_data ).


  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_demo=>main( ).

使用 gui_download 将文件写入本地电脑

下载文件则相反,预定义结果,将SAP 数据存进内表中

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
*   BIN_FILESIZE                    =
    filename                        =   "该参数为你所需要保存在电脑的那个位置的地址
*   FILETYPE                        = 'ASC'
*   APPEND                          = ' '
*   WRITE_FIELD_SEPARATOR           = ' '   ”是否分隔符也下载
*   HEADER                          = '00'
*   TRUNC_TRAILING_BLANKS           = ' '
*   WRITE_LF                        = 'X'
*   COL_SELECT                      = ' '
*   COL_SELECT_MASK                 = ' '
*   DAT_MODE                        = ' '
*   CONFIRM_OVERWRITE               = ' '
*   NO_AUTH_CHECK                   = ' '
*   CODEPAGE                        = ' '
*   IGNORE_CERR                     = ABAP_TRUE
*   REPLACEMENT                     = '#'
*   WRITE_BOM                       = ' '
*   TRUNC_TRAILING_BLANKS_EOL       = 'X'
*   WK1_N_FORMAT                    = ' '
*   WK1_N_SIZE                      = ' '
*   WK1_T_FORMAT                    = ' '
*   WK1_T_SIZE                      = ' '
*   WRITE_LF_AFTER_LAST_LINE        = ABAP_TRUE
*   SHOW_TRANSFER_STATUS            = ABAP_TRUE
*   VIRUS_SCAN_PROFILE              = '/SCET/GUI_DOWNLOAD'
* IMPORTING
*   FILELENGTH                      =
  tables
    data_tab                        =
*   FIELDNAMES                      =
* EXCEPTIONS
*   FILE_WRITE_ERROR                = 1
*   NO_BATCH                        = 2
*   GUI_REFUSE_FILETRANSFER         = 3
*   INVALID_TYPE                    = 4
*   NO_AUTHORITY                    = 5
*   UNKNOWN_ERROR                   = 6
*   HEADER_NOT_ALLOWED              = 7
*   SEPARATOR_NOT_ALLOWED           = 8
*   FILESIZE_NOT_ALLOWED            = 9
*   HEADER_TOO_LONG                 = 10
*   DP_ERROR_CREATE                 = 11
*   DP_ERROR_SEND                   = 12
*   DP_ERROR_WRITE                  = 13
*   UNKNOWN_DP_ERROR                = 14
*   ACCESS_DENIED                   = 15
*   DP_OUT_OF_MEMORY                = 16
*   DISK_FULL                       = 17
*   DP_TIMEOUT                      = 18
*   FILE_NOT_FOUND                  = 19
*   DATAPROVIDER_EXCEPTION          = 20
*   CONTROL_FLUSH_ERROR             = 21
*   OTHERS                          = 22
          .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

完整示例代码

CLASS lcl_demo DEFINITION. 
  PUBLIC SECTION. 
    CLASS-METHODS main. 
ENDCLASS. 

CLASS lcl_demo IMPLEMENTATION. 
  METHOD main. 
    DATA: lt_data_tab TYPE TABLE OF string.
    DATA: lv_filename TYPE  string.  
    DATA: lv_path TYPE  string. 
    DATA: lv_fullpath TYPE  string. 

    CALL METHOD cl_gui_frontend_services=>file_save_dialog

      CHANGING 
        filename                  = lv_filename 
        path                      = lv_path 
        fullpath                  = lv_fullpath 
      EXCEPTIONS 
        cntl_error                = 1 
        error_no_gui              = 2 
        not_supported_by_gui      = 3 
        invalid_default_file_name = 4 
        OTHERS                    = 5. 

    IF sy-subrc <> 0. 
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

      RETURN. 
    ENDIF. 

    APPEND 'ABC1' TO lt_data_tab. 
    APPEND 'ABC2' TO lt_data_tab. 
    APPEND 'ABC3' TO lt_data_tab. 

    CALL METHOD cl_gui_frontend_services=>gui_download

      EXPORTING
        filename                = lv_fullpath
      CHANGING
        data_tab                = lt_data_tab
      EXCEPTIONS
        file_write_error        = 1
        no_batch                = 2
        gui_refuse_filetransfer = 3
        invalid_type            = 4
        no_authority            = 5
        unknown_error           = 6
        header_not_allowed      = 7
        separator_not_allowed   = 8
        filesize_not_allowed    = 9
        header_too_long         = 10
        dp_error_create         = 11
        dp_error_send           = 12
        dp_error_write          = 13
        unknown_dp_error        = 14
        access_denied           = 15
        dp_out_of_memory        = 16
        disk_full               = 17
        dp_timeout              = 18
        file_not_found          = 19
        dataprovider_exception  = 20
        control_flush_error     = 21
        not_supported_by_gui    = 22
        error_no_gui            = 23
        OTHERS                  = 24.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      RETURN.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_demo=>main( ).

与服务器端文件有关的基本事务

T-CODE:AL11
SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景_第4张图片在 AL11 中,可以显示服务器端 SAP 目录、文件和文件内容。我们将使用 AL11 事务来检查下面示例中的文件是否真的上传到了服务器上。完整的目录结构取决于服务器操作系统,但其中一些目录是通用的,在每个 SAP 安装中都很相似。

  • DIR_PROFILE 是 SAP 系统的中央配置目录。使用存储在 DIR_PROFILE 目录中的配置文件对实例进行配置。
  • DIR_SAPUSERS 是用户文件的默认目录
  • DIR_TRANS 是传输请求目录。基本上,每个已发布的传输请求都以文件形式存储在这里。你可以复制这些文件并移入另一个系统。备份您的工作以供将来使用可能是个好主意。
  • DIR_TEMP 是临时数据目录。

CG3Z-> 上载文件

SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景_第5张图片
CG3Y 下载文件
SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景_第6张图片

向应用服务器写数据


CLASS lcl_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS lcl_demo IMPLEMENTATION.
  METHOD main.
    DATA: lv_file TYPE string VALUE 'testfile4_3.txt'.

    OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

    TRANSFER '1st line on application server' TO lv_file.
    TRANSFER '2nd line on application server' TO lv_file.
    TRANSFER '3rd line on application server' TO lv_file.

    CLOSE DATASET lv_file.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_demo=>main( ).

该代码没有指定目录,默认存在 DIR_SAPUSERS 目录下

从应用服务器读取文件

CLASS lcl_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS lcl_demo IMPLEMENTATION.
  METHOD main.
    DATA: lv_file TYPE string VALUE 'testfile4_3.txt'.
    DATA: lv_line TYPE string.
    DATA: lt_data TYPE TABLE OF string.
    
    OPEN DATASET lv_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
    DO.
      READ DATASET lv_file INTO lv_line.
      IF sy-subrc = 0.
        APPEND lv_line TO lt_data.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.

    CLOSE DATASET lv_file.

    cl_demo_output=>display_data( lt_data ).

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_demo=>main( ).

从 Microsoft Excel 文件读取数据

Microsoft Word 或 Microsoft Excel 格式是 Windows 操作系统最著名、最具特色的扩展。Office 软件包与 SAP 的集成度也非常高。在许多标准事务中,我们都可以将结果导出到 Excel 电子表格中。导出到 Excel 也是 ABAP 列表查看器 (ALV) 标准功能的一部分。

CLASS lcl_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS lcl_demo IMPLEMENTATION.
  METHOD main.
    DATA: lv_filename TYPE localfile VALUE 'c:/temp/testfile4_5.xlsx'.
    DATA: lt_excel TYPE TABLE OF alsmex_tabline.


    CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename                = lv_filename
        i_begin_col             = 1
        i_begin_row             = 1
        i_end_col               = 1000
        i_end_row               = 1000
      TABLES
        intern                  = lt_excel
      EXCEPTIONS
        inconsistent_parameters = 1
        upload_ole              = 2
        OTHERS                  = 3.
    IF sy-subrc <> 0.

    ENDIF.

    cl_demo_output=>display_data( lt_excel ).

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_demo=>main( ).

将数据保存到 Microsoft Excel 文件

常规方法使用OLE

INCLUDE ole2incl.

CLASS lcl_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS lcl_demo IMPLEMENTATION.
  METHOD main.
    DATA: lv_filename TYPE localfile VALUE 'c:\temp\testfile4_6.xls'.
    DATA: lo_excel TYPE ole2_object.
    DATA: lo_workbook TYPE ole2_object.
    DATA: lo_sheet TYPE ole2_object.
    DATA: lo_cell TYPE ole2_object.

    CREATE OBJECT lo_excel 'EXCEL.APPLICATION'.

    SET PROPERTY OF lo_excel 'visible' = 1.

    CALL METHOD OF lo_excel 'Workbooks' = lo_workbook.
    CALL METHOD OF lo_workbook 'Add'.

    CALL METHOD OF lo_excel 'Worksheets' = lo_sheet
                                 EXPORTING #1 = 1.
    CALL METHOD OF lo_sheet 'Activate'.

    SET PROPERTY OF lo_sheet 'Name' = 'TestSheet'.
这将初始化 lo_workbook 和 lo_sheet。lo_workbook 代表我们的整个工作簿,而 lo_sheet 相当于我们的单张电子表格。你还可以使用工作表对象上的 "NAME "属性更改名称。运行该程序后,Excel 将在本地桌面上打开。如果将 "可见 "参数改为 0,程序将在后台运行。

现在,我们要在 Excel 中输入一些信息。为此,我们将从 lo_sheet 对象中获取一个 lo_cell 对象,并设置 "Value "属性:


    DO 10 TIMES.
      CALL METHOD OF lo_sheet 'Cells' = lo_cell EXPORTING #1 = sy-index #2 = 1.
      SET PROPERTY OF lo_cell 'Value' = sy-index.
    ENDDO.

    CALL METHOD OF lo_sheet 'SaveAs'
      EXPORTING
        #1 = lv_filename
        #2 = 1. 

    SET PROPERTY OF lo_excel 'visible' = 0.
这里有两个导出参数:第一个是行参数,第二个是列参数。我们将把当前索引的值放在各个单元格中。最后要做的是保存文件并关闭 Excel OLE 对象:


    CALL METHOD OF lo_sheet 'CLOSE'
      EXPORTING
        #1 = 'YES'.

    CALL METHOD OF lo_excel 'QUIT'.

    FREE OBJECT: lo_excel,
                 lo_sheet.


  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_demo=>main( ).

如果一切顺利,C:/temp 目录中就会出现一个新的 Excel 文件。进入该目录,检查内容是否正确:

将数据保存到 Microsoft Word 文件中

INCLUDE ole2incl.

CLASS lcl_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS lcl_demo IMPLEMENTATION.
  METHOD main.
    DATA: lo_word TYPE ole2_object.
    DATA: lo_doc TYPE ole2_object.
    DATA: lo_selection TYPE ole2_object.
    DATA: lo_font TYPE ole2_object.
    DATA: lo_paragraph TYPE ole2_object.

    CREATE OBJECT lo_word 'Word.Application'.
    CALL METHOD OF lo_word 'Documents' = lo_doc.
    CALL METHOD OF lo_doc 'Add'.
    GET PROPERTY OF lo_word 'Selection' = lo_selection.
    GET PROPERTY OF lo_selection 'ParagraphFormat' = lo_paragraph.
    GET PROPERTY OF lo_selection 'Font' = lo_font.


    SET PROPERTY OF lo_word 'Visible' = 1.
    SET PROPERTY OF lo_font 'Size' = 22.
    SET PROPERTY OF lo_font 'Bold' = 1.
    SET PROPERTY OF lo_paragraph 'Alignment' = 1. " Centered

    CALL METHOD OF lo_selection 'TypeText'
      EXPORTING
        #1 = 'First Word Report of Airlines with OLE'.
    CALL METHOD OF lo_selection 'TypeParagraph'.

    DATA: lt_carrname TYPE TABLE OF s_carrname.
    DATA: lv_carrname TYPE s_carrname.
    SELECT carrname FROM scarr INTO TABLE lt_carrname.

    SET PROPERTY OF lo_font 'Size' = 10.

    SET PROPERTY OF lo_font 'Bold' = 0.
    SET PROPERTY OF lo_paragraph 'Alignment' = 0.

    LOOP AT lt_carrname INTO lv_carrname.
      CALL METHOD OF lo_selection 'TypeText'
        EXPORTING
          #1 = lv_carrname.
      CALL METHOD OF lo_selection 'TypeParagraph'.
    ENDLOOP.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_demo=>main( ).

在 ABAP 报告中创建桌面办公集成 (DOI)

DOI 是一个 ABAP 对象接口,可用于使用 OLE2 接口与 Office 应用程序配合使用。您可以用它来编辑 Office 格式,也可以用它将 Office 应用程序集成到 ABAP 报表中。这可以是双向集成,即 ABAP 报表可以对 Office 事件做出反应,而 Office 可以根据 ABAP 逻辑做出反应。DOI 大大扩展了前几章介绍的使用 OLE 类的可能性。

使用 DOI 也比使用 OLE 对象要复杂得多,因此我们将只关注一个将 Word 文档集成到 ABAP 程序中的示例。暂且不考虑我们的标准程序模板.

TYPE-POOLS: soi.

DATA: lo_container TYPE REF TO cl_gui_custom_container.
DATA: lo_control   TYPE REF TO i_oi_container_control.
DATA: lo_proxy     TYPE REF TO i_oi_document_proxy.

DATA: lv_okcode TYPE syst_ucomm.

DATA: lv_closed  TYPE i.
DATA: lv_init   TYPE boolean.
DATA: lv_changed TYPE i.

TYPES: ty_row TYPE x LENGTH 2048.
DATA: lt_doc_table TYPE STANDARD TABLE OF ty_row.
DATA: lv_doc_size TYPE i.


SET SCREEN 100.

MODULE init OUTPUT.

  CHECK lv_init = abap_false.

  SET PF-STATUS '0100'.
  SET TITLEBAR '0100'.

  CALL METHOD c_oi_container_control_creator=>get_container_control
    IMPORTING
      control = lo_control.

  CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

  CREATE OBJECT lo_container
    EXPORTING
      container_name = 'CUSTOMCONTAINER'.
  CALL METHOD lo_container->set_visible EXPORTING visible = ' '.


  CALL METHOD lo_control->init_control
    EXPORTING
      r3_application_name      = 'R/3 Basis'
      inplace_enabled          = abap_true
      inplace_scroll_documents = abap_true
      parent                   = lo_container
      register_on_close_event  = abap_true
      register_on_custom_event = abap_true
      no_flush                 = abap_false.

  CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

  CALL METHOD lo_control->get_document_proxy
    EXPORTING
      document_type  = 'Word.Document.8'
      no_flush       = abap_false
    IMPORTING
      document_proxy = lo_proxy.

  CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

  lv_init = abap_true.
ENDMODULE.


MODULE user_command_0100 INPUT.
  CASE lv_okcode.

    WHEN 'OPEN'.
      CALL METHOD lo_proxy->is_destroyed
        IMPORTING
          ret_value = lv_closed.

      CHECK NOT lv_closed IS INITIAL.
      CALL METHOD lo_container->set_visible
        EXPORTING
          visible = abap_true.

      IF lv_doc_size > 0.

        CALL METHOD lo_proxy->open_document_from_table
          EXPORTING
            document_table = lt_doc_table
            document_size  = lv_doc_size
            document_title = 'DOI Test Document'
            open_inplace   = abap_true.
      ELSE.


        CALL METHOD lo_proxy->create_document
          EXPORTING
            open_inplace   = abap_true
            document_title = 'DOI Test Document'
            no_flush       = abap_false.

      ENDIF.
      CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.


    WHEN 'CLOSE'.
      CALL METHOD lo_proxy->is_destroyed
        IMPORTING
          ret_value = lv_closed.

      IF lv_closed IS INITIAL.
        CALL METHOD lo_proxy->close_document
          EXPORTING
            do_save     = 'X'
          IMPORTING
            has_changed = lv_changed.

        CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

        IF NOT lv_changed IS INITIAL.
          CALL METHOD lo_proxy->save_document_to_table
            CHANGING
              document_table = lt_doc_table
              document_size  = lv_doc_size.
          CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.
        ENDIF.

        CALL METHOD lo_proxy->release_document.
        CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.
      ENDIF.

      CALL METHOD lo_container->set_visible EXPORTING visible = ' '.


  ENDCASE.
  CLEAR: lv_okcode.
ENDMODULE.


MODULE exit INPUT.  
  CASE lv_okcode.  
    WHEN 'STOP'.  
      IF NOT lo_proxy IS INITIAL.  
        CALL METHOD lo_proxy->close_document.


        FREE lo_proxy.  
      ENDIF.  
      IF NOT lo_control IS INITIAL.  
        CALL METHOD lo_control->destroy_control.


        FREE lo_control.  
      ENDIF.  
      LEAVE PROGRAM.  
  ENDCASE.  
ENDMODULE.  

同时在程序中 创建屏幕
SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景_第7张图片SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景_第8张图片SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景_第9张图片
SAP-ABAP:一篇教你所有ABAP相关文件处理的程序示例与场景_第10张图片


什么是 DOI?

DOI 是Desktop Office Integration的缩写,是 SAP 提供的解决与 Office 集成的技术方案。早期 SAP 用 OLE 技术解决 与 Office 集成。OLE 语法参照 VBA,在 ABAP 中调用方法来操作 Excel,语法的友好性差;另外将数据从ABAP 内表写入到 Excel ,速度特别慢,性能难以接受。

DOI 可以看作 OLE 的替代品,用面向对象的方式实现。很好的解决了上面所说的两个问题。(参考:Desktop Office Integration官方帮助)
DOI 开发的技术要点
相关对象

SAP 与 Office 集成,既可以通过代码创建 Excel 文档,也可以将 Excel 模板文档放在应用程序服务器上由 DOI 调用,DOI 打开这个文档,对文档进行操作和处理。我们先介绍代码创建 Excel 文档的方式。为了操作 Excel 文档,至少需要四个对象:

container: 存放 Excel 电子表格 (spreadsheet) 的容器。这个应该比较容易理解,容器一般在 dialog screen 中定义,也可以直接使用 ABAP 程序中默认的 screen(即 screen 号码为 1000 的屏幕)

container control: 容器中用于创建和管理其他 Office 集成所需要的对象,container control 是一个接口,类型为 i_oi_container_control。

document proxy: 每一个 document proxy 的实例代表用 office application 打开的文档,可以是 Excel,也可以是 Word,如果想打开多个文档,需要定义多个实例。document proxy 是一个接口,类型为 i_oi_document_proxy。

spreadsheet: spreadsheet 接口,代表最终要操作的 Excel文档,spreadhseet 的类型是 i_oi_spreadsheet

business document set: 如果读取服务器上的文档模板,用 cl_bds_document_set 类,将business document set缩写为 bds,bds 用于管理要操作的文档,可以包含一个或多个文档。

DOI 操作 Excel 的步骤

获取 container
创建 container control 对象实例
创建 document proxy 对象的实例
打开一个服务器上的模板文档或新建一个新的 Excel 文档
操作打开的 Excel 文档,设置 Excel 相关属性或调用相关方法
退出时关闭 Excel 文档,释放资源

我们的第一个例子,不使用 dialog screen,新建一个 Excel 文档,在屏幕 1000 中显示。以下是开发步骤:
定义公共变量

  • desktop office integration interface
    data: gr_container type ref to cl_gui_container,
    gr_control type ref to i_oi_container_control,
    gr_document type ref to i_oi_document_proxy,
    gr_spreadsheet type ref to i_oi_spreadsheet.

这些就是我们前面提到的四个必须的对象。
获取container

cl_gui_container 类的静态方法 screen0 获取屏幕 1000,并赋值给 gr_container。
form get_container.
gr_container = cl_gui_container=>screen0.
endform.
创建 container control 对象实例并初始化

代码比较直观,不多解释。
form create_container_control.

  • create container control
    call method c_oi_container_control_creator=>get_container_control
    importing
    control = gr_control.

  • initialize control
    call method gr_control->init_control
    exporting
    inplace_enabled = 'X ’
    inplace_scroll_documents = ‘X’
    register_on_close_event = ‘X’
    register_on_custom_event = ‘X’
    r3_application_name = ‘DOI demo by Stone Wang’
    parent = gr_container.
    endform.
    创建 document proxy 对象的实例
    form create_excel_document.
    call method gr_control->get_document_proxy
    exporting
    document_type = ‘Excel.Sheet’
    no_flush = ‘X’
    importing
    document_proxy = gr_document.

    call method gr_document->create_document
    exporting
    document_title = 'DOI test by Stone Wang ’
    no_flush = 'X ’
    open_inplace = ‘X’.
    endform. "create_excel_document

open_inplace 参数控制 Excel 文档是独立显示还是在 SAP GUI 中嵌入显示。如果嵌入式显示,gr_control 的 init_control 方法中,inplace_enabled 参数要设为 X。

将以上代码综合在一起:
form main.
skip 1.

perform get_container.
perform create_container_control.
perform create_excel_document.
endform.

注意第一句 skip 1 必须,否则不能创建屏幕。
完整代码
report zdoi_hello.

type-pools: soi.

data: gr_container type ref to cl_gui_container,
gr_control type ref to i_oi_container_control,
gr_document type ref to i_oi_document_proxy,
gr_spreadsheet type ref to i_oi_spreadsheet.

start-of-selection.
perform main.

form get_container.
gr_container = gr_container = cl_gui_container=>screen0.
endform. "get_container

form create_container_control.

  • create container control
    call method c_oi_container_control_creator=>get_container_control
    importing
    control = gr_control.

  • initialize control
    call method gr_control->init_control
    exporting
    inplace_enabled = 'X ’
    inplace_scroll_documents = ‘X’
    register_on_close_event = ‘X’
    register_on_custom_event = ‘X’
    r3_application_name = ‘DOI demo by Stone Wang’
    parent = gr_container.
    endform. "create_container_control

form create_excel_document.
call method gr_control->get_document_proxy
exporting
document_type = ‘Excel.Sheet’
no_flush = ‘X’
importing
document_proxy = gr_document.

call method gr_document->create_document
exporting
document_title = 'DOI test by Stone Wang ’
no_flush = 'X ’
open_inplace = ‘X’.
endform. "create_excel_document

form main.
skip 1.

perform get_container.
perform create_container_control.
perform create_excel_document.
endform.

其他技术

ABAP2XLXS,OAOR,SMW0
将会在其他文章中着重介绍。

总结

-涉及到的一些面试问题:

  • 应使用哪个类将文件从表现层上载到应用层?
    gui_upload method of CL_GUI_FRONTEND_SERVICES class

  • file_open_dialog 方法的哪个参数可控制可选择的文件数量?

    default_extension = ‘.xls’
    default_filename = '
    .xls’
    initial_directory = ‘c:’
    multiselection = ’ ’

  • 只需调用一次函数,就能将文件从表现层上传到应用层的函数模块是什么?

    ARCHIVFILE_CLIENT_TO_SERVER
    C13Z_FRONT_END_TO_APPL. -> CG3Z

  • gui_upload 方法中文件类型参数(ASC、BIN、DAT)的不同设置有何作用?

数据格式不同

  • 什么事务可用于将文件上传到 SAP 应用服务器?
  • CG3Z
  • 如果不在 OPEN_DATASET 语句中指定目录路径,会发生什么情况?

存在默认目录 DIR_SAPUSERS 目录下

  • 在 ABAP 中与 Microsoft Office 应用程序配合使用的库名称是什么?

DOI

  • 应用程序必须满足哪些要求才能使用 DOI 将其集成到 ABAP 报表中?

桌面 Office 集成 (DOI) 和对象链接与嵌入 (OLE): 本章中显示的大多数示例都需要安装 Microsoft Office 软件包。

如有不对,请指正,仅作学习交流~

代码参考:Github

https://blog.csdn.net/IamRunjiang/article/details/122922467

你可能感兴趣的:(ABAP,开发语言)