DOI模板程序:下载批量导入模板

来源:互联网 发布:怎么禁止系统安装软件 编辑:程序博客网 时间:2024/05/01 14:52
*&---------------------------------------------------------------------*
*&  DOI示例模板,今后下载文件的例子可参照此include程序
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
*       Type-pools                                                     *
*----------------------------------------------------------------------*
TYPE-POOLS:soi.

*----------------------------------------------------------------------*
*       Class Load
*----------------------------------------------------------------------*
CLASS c_oi_errors DEFINITION LOAD.

*---------------------------------------------------------------------*
* internal tables and work areas
*---------------------------------------------------------------------*
DATA:it_error TYPE TABLE OF REF TO i_oi_error,
     wa_error  TYPE REF TO i_oi_error.
*----------------------------------------------------------------------*
*       Types
*----------------------------------------------------------------------*
TYPES: BEGIN OF ty_docu_descr,
          document_name(40),
          document_id(64),
       END OF ty_docu_descr.
TYPES: ty_docu_descr_tab TYPE ty_docu_descr OCCURS 0.

TYPES:BEGIN OF ty_errors,
       err TYPE REF TO i_oi_error,
      END OF ty_errors.

*----------------------------------------------------------------------*
*       Variables
*----------------------------------------------------------------------*
DATA:g_container_doi TYPE REF TO cl_gui_docking_container."容器自动画
DATA:g_bds_instance TYPE REF TO cl_bds_document_set.
DATA:g_document_type TYPE c LENGTH 80 VALUE soi_doctype_excel_sheet,
     g_document_format TYPE c LENGTH 80.
DATA:g_doc_url TYPE bds_uri.
*1.Declare an instance for the central object of the Office
*    Integration with reference to the interface i_oi_container_control:
DATA:g_control TYPE REF TO i_oi_container_control.
*2.Declare an object variable for all of the documents
*    that you want to have open at once.
DATA:g_document TYPE REF TO i_oi_document_proxy.

*----------------------------------------------------------------------*
*       Constants
*----------------------------------------------------------------------*
CONSTANTS:
*以下三个值为Tcode:OAOR里面新建模板文件的参数
  cos_classname  TYPE sbdst_classname  VALUE 'PICTURES',
  cos_classtype  TYPE sbdst_classtype  VALUE 'OT',
  cos_object_key TYPE sbdst_object_key VALUE 'XXXXXXX'."自行指定

*&---------------------------------------------------------------------*
*&      Form  FRM_DOI_INITIALIZATION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_doi_initialization.

*4.Create the instance control
  CALL METHOD c_oi_container_control_creator=>get_container_control
    IMPORTING
      control = g_control
      error   = wa_error.
  APPEND wa_error TO it_error.

*5.If you want to use Desktop Office Integration in-place, you also
*     need to create a container:
* 不画容器,自适应大小.
  CREATE OBJECT g_container_doi"声明容器对象
    EXPORTING
      repid     = sy-repid
      dynnr     = sy-dynnr
      extension = 2050
      side      = cl_gui_docking_container=>property_floating
    EXCEPTIONS
      cntl_error                  = 1
      cntl_system_error           = 2
      create_error                = 3
      lifetime_error              = 4
      lifetime_dynpro_dynpro_link = 5.
  IF sy-subrc NE 0.
    CALL FUNCTION 'POPUP_TO_INFORM'
      EXPORTING
        titel = sy-repid
        txt2  = sy-subrc
        txt1  = 'The control could not be created'.
  ENDIF.

*6. Call the method init_control.
  CALL METHOD g_control->init_control
    EXPORTING
      r3_application_name = g_title
      inplace_enabled     = 'X'
      parent              = g_container_doi
    IMPORTING
      error               = wa_error.
  APPEND wa_error TO it_error.

*7.Create an instance document for each document that you want to open:
  CALL METHOD g_control->get_document_proxy
    EXPORTING
      document_format    = soi_docformat_compound "OLE
      document_type      = 'Excel.Sheet'
      no_flush           = 'X'
*      register_container = ' '
    IMPORTING
      document_proxy     = g_document
      error   = wa_error.
  APPEND wa_error TO it_error.

ENDFORM. " FRM_DOI_INITIALIZATION

*&---------------------------------------------------------------------*
*&      Form  FRM_DOI_SELECT_DOCUMENT
*&---------------------------------------------------------------------*
*       获取模板在系统中存储的URL
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_doi_select_document USING fu_bds_propva TYPE bds_propva
                         CHANGING fc_document_type TYPE c
                                  fc_document_format TYPE c
                                  fc_doc_url TYPE bds_uri.

* 定义变量
  DATA: lt_doc_signature TYPE sbdst_signature,
        lw_doc_signature LIKE LINE OF lt_doc_signature,
        lt_documents     TYPE ty_docu_descr_tab,
        lt_doc_components TYPE sbdst_components,
        lw_doc_components LIKE LINE OF lt_doc_components,
        lt_doc_uris      TYPE sbdst_uri,
        lw_doc_uris      LIKE LINE OF lt_doc_uris.

  CLEAR:lw_doc_signature, lw_doc_uris.
  CLEAR:lt_doc_signature, lt_documents, lt_doc_uris.

  lw_doc_signature-prop_name  = 'DESCRIPTION'.
  lw_doc_signature-prop_value = fu_bds_propva.
  APPEND lw_doc_signature TO lt_doc_signature.

  IF g_bds_instance IS INITIAL.
    CREATE OBJECT g_bds_instance.
  ENDIF.

  CALL METHOD g_bds_instance->get_info
    EXPORTING
      classname       = cos_classname
      classtype       = cos_classtype
      object_key      = cos_object_key
    CHANGING
      components      = lt_doc_components
      signature       = lt_doc_signature
    EXCEPTIONS
      nothing_found   = 1
      error_kpro      = 2
      internal_error  = 3
      parameter_error = 4
      not_authorized  = 5
      not_allowed     = 6.
  CASE sy-subrc.
    WHEN 0.
    WHEN 1.
      MESSAGE e013."系统中没有预存储的excel模板
    WHEN OTHERS.
      MESSAGE e014."业务文档服务中存在错误 (BDS)
  ENDCASE.

  READ TABLE lt_doc_components INTO lw_doc_components INDEX 1.
  CASE lw_doc_components-mimetype.
    WHEN 'application/x-rtf' OR 'text/rtf'.
      fc_document_format = soi_docformat_rtf.
    WHEN 'application/vnd.ms-excel'.
      fc_document_format = soi_docformat_compound.
    WHEN 'application/x-oleobject'.
      fc_document_format = soi_docformat_compound.
    WHEN 'text/plain'.
      fc_document_format = soi_docformat_text.
    WHEN OTHERS.
      fc_document_format = soi_docformat_native.
  ENDCASE.

* 获取URL
  CALL METHOD g_bds_instance->get_with_url
    EXPORTING
      classname       = cos_classname
      classtype       = cos_classtype
      object_key      = cos_object_key
    CHANGING
      uris            = lt_doc_uris
      signature       = lt_doc_signature
    EXCEPTIONS
      nothing_found   = 1
      error_kpro      = 2
      internal_error  = 3
      parameter_error = 4
      not_authorized  = 5
      not_allowed     = 6.
  CASE sy-subrc.
    WHEN 0.
    WHEN 1.
      MESSAGE e013."系统中没有预存储的excel模板
    WHEN OTHERS.
      MESSAGE e014."业务文档服务中存在错误 (BDS)
  ENDCASE.

  READ TABLE lt_doc_uris  INTO lw_doc_uris  INDEX 1.
  fc_doc_url = lw_doc_uris-uri.

ENDFORM. " FRM_DOI_SELECT_DOCUMENT

*&---------------------------------------------------------------------*
*&      Form  frm_doi_processing
*&---------------------------------------------------------------------*
*       处理DOI数据,本例下载模板
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_doi_processing .
  DATA:l_title    TYPE string,
       l_filename TYPE string,
       l_fname    TYPE c LENGTH 100,
       l_retcode  TYPE soi_ret_string.

  l_title = text-808."指定保存路径'.
  l_filename = text-809." D:\批量导入模板.xls

  IF g_doc_url IS INITIAL.
    MESSAGE e013."系统中没有预存储的excel模板
  ENDIF.

* open a document saved in business document service.
  CHECK g_doc_url IS NOT INITIAL.
  CALL METHOD g_document->open_document
    EXPORTING
      document_title = g_title
      open_inplace   = ''
      document_url   = g_doc_url
    IMPORTING
      error          = wa_error.
  APPEND wa_error TO it_error.

* 指定保存文件的路径和名称
  PERFORM frm_local_file_save_dialog USING l_title
                              CHANGING l_filename.

  CHECK l_filename IS NOT INITIAL.
* Saves Document Locally on Frontend
  l_fname = l_filename.
  CALL METHOD g_document->save_copy_as
    EXPORTING
      file_name = l_fname
      no_flush  = 'X'
    IMPORTING
      error     = wa_error
      retcode   = l_retcode.

ENDFORM. " frm_doi_processing

*&---------------------------------------------------------------------*
*&      Form  FRM_DOI_CLOSING
*&---------------------------------------------------------------------*
*       清空不用的对象
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_doi_closing.

  CALL METHOD g_document->close_document
    EXPORTING
      do_save  = 'X'
      no_flush = ' '
    IMPORTING
      error    = wa_error.
  APPEND wa_error TO it_error.

  CALL METHOD g_control->destroy_control
    EXPORTING
      no_flush = ' '
    IMPORTING
      error    = wa_error.
  APPEND wa_error TO it_error.

* prompt errors
  PERFORM frm_doi_errors.

  FREE:g_control,
       g_container_doi,
       g_document.

ENDFORM. " FRM_DOI_CLOSING

*&---------------------------------------------------------------------*
*&      Form  FRM_DOI_ERRORS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_doi_errors .
  LOOP AT it_error INTO wa_error.
    CALL METHOD wa_error->raise_message
      EXPORTING
        type = 'S'.
  ENDLOOP.
  FREE:it_error,
       wa_error.
ENDFORM. " FRM_DOI_ERRORS

*---------------------------------------------------------------------*
*       FORM FRM_local_file_save_dialog
*---------------------------------------------------------------------*
*       文件保存对话
*---------------------------------------------------------------------*
*  -->  i_title   :弹出大标题
*  <->  C_filename:文件名(完整路径)
*---------------------------------------------------------------------*
FORM frm_local_file_save_dialog USING i_title TYPE string
                            CHANGING c_filename TYPE string.

  DATA : l_title          TYPE string,
         l_initdir        TYPE string,
         l_deffile        TYPE string,
         l_filename       TYPE string,
         l_path           TYPE string,
         l_fullpath       TYPE string.

  l_title   = i_title.
* 路径名和文件名的分割
  PERFORM frm_split_filename USING c_filename
                      CHANGING l_initdir
                               l_deffile.
* 文件保存对话
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title         = l_title
      initial_directory    = l_initdir
      default_file_name    = l_deffile
      file_filter          = '*.xls'
    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.

  c_filename = l_fullpath.

ENDFORM. " FRM_local_file_save_dialog

*&---------------------------------------------------------------------*
*&      Form  FRM_split_filename
*&---------------------------------------------------------------------*
*       完整路径文件名的路径和文件名的分割
*----------------------------------------------------------------------*
*  -->  i_full    :完整路径文件名
*  <--  C_path    :路径
*  <--  C_file    :文件名
*----------------------------------------------------------------------*
FORM frm_split_filename USING i_fullfile
                    CHANGING c_path
                             c_file.
  DATA : l_filename TYPE string,
         l_path     TYPE string,
         l_file     TYPE string.

  l_filename = i_fullfile.
  l_path     = c_path.
  l_file     = c_file.
  CALL METHOD cl_report_viewer=>split_path_filename
    EXPORTING
      i_filename = l_filename
    IMPORTING
      e_path     = l_path
      e_filename = l_file.
  c_path = l_path.
  c_file = l_file.

ENDFORM. " FRM_split_filename

使用模板代码:
*&---------------------------------------------------------------------*
*&      Form  frm_download_template
*&---------------------------------------------------------------------*
*       下载批量导入的模板
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_download_template .
  DATA:l_bds_propva TYPE bds_propva VALUE 'XXXXXX'.

*-----导出预存储的excel模板
* Initialization
  PERFORM frm_doi_initialization.

* 从系统中获取预先存储的模板(T-code:OAOR)
  PERFORM frm_doi_select_document USING l_bds_propva
                              CHANGING g_document_type
                                       g_document_format
                                       g_doc_url.
* Processing(open,close,save,……)
  PERFORM frm_doi_processing.

* Closing
  PERFORM frm_doi_closing.

ENDFORM. " frm_download_template

0 0
原创粉丝点击