OLE 使用实例

report ztest16.

*include ole2incl.
include officeintegrationinclude.

data: factory type ref to i_oi_document_factory.
data: document type ref to i_oi_document_proxy.
data: retcode type t_oi_ret_string.
data: link_server type ref to i_oi_link_server.

**定义对应EXCEL,workbook(工作簿),单元格, sheet(页),
data: excel_obj    type ole2_object,        " Excel object
      book_obj     type ole2_object,         " list of workbooks
      cell_obj     type ole2_object,           " cell
      sheet_obj    type ole2_object.        "sheet

data: is_closed type i.
data: p_file type string. "Smwo 上载的文件名


data: doc_table like w3mime occurs 0.
data: doc_size type i.
data: doc_type(100)      value soi_doctype_word97_document.
data: doc_format(100) type c.


start-of-selection.
  perform frm_init_factory.
  perform frm_output_excel.

form frm_output_excel .

***将值写入EXcel
  perform frm_file_open.  "打开Excel

  create object excel_obj 'EXCEL.APPLICATION'.
  set property of excel_obj  'Visible' = 1.
  call method of excel_obj 'Workbooks' = book_obj.
  call method of book_obj 'Open' exporting #1 = p_file.
  call method of excel_obj 'WORKSHEETS' = sheet_obj exporting #1 = 'Sheet1'.
  call method of sheet_obj 'ACTIVATE'.

  data: columns type i value 1, "
        rows    type i value 4.   "
  data value type i value 1000.
  do 80 times.
    add 1 to rows.
    columns = 1.
    do 12 times.
      add 1 to columns.
      perform frm_fill_cell using rows columns value.   "进行Excel的填值操作
    enddo.
  enddo.


  free object excel_obj.
  free object book_obj.
  free object sheet_obj.

  message 'Over' type 'S'.
endform.                    " FRM_OUTPUT_EXCEL
form frm_fill_cell using i  j  val.
  call method of excel_obj 'Cells' = cell_obj
    exporting
    #1 = i
    #2 = j.
  set property of cell_obj 'Value' = val .
*  get property of cell_obj 'Font' = h_f.
endform.                    "FILL_CELL
form frm_file_open .
  data: object_id(20) type c.
  clear object_id.

*如果文件存在先删除
  data: tmp_fcheck type c.

  data: rc     type i,
        return type c.

  p_file = 'D:\现金流量表.xls'.



  tmp_fcheck = cl_gui_frontend_services=>file_exist( p_file ).

  if tmp_fcheck is not initial.
    call function 'TB_LIMIT_WS_FILE_DELETE'
      exporting
        filename           = p_file
      importing
        return             = return
      exceptions
        file_delete_failed = 1
        cntl_error         = 2
        error_no_gui       = 3
        file_not_found     = 4
        access_denied      = 5
        unknown_error      = 6
        others             = 7.
    if sy-subrc <> 0.
      message '请先关闭D:现金流量表.xls这个文件后,再执行这个报表!' type 'S'.
      stop.
    endif.
  endif.


  call function 'SAP_OI_LOAD_MIME_DATA'
    exporting
      object_id        = 'ZFI001B'    "文件名 SMW0
    importing
      data_size        = doc_size
      document_format  = doc_format
      document_type    = doc_type
    tables
      data_table       = doc_table
    exceptions
      object_not_found = 1
      internal_error   = 2
      others           = 3.
  if sy-subrc ne 0.
    message id sy-msgid type 'E' number sy-msgno
         with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.

  if doc_size ne 0.
    call method factory->get_document_proxy
      exporting
        document_type  = doc_type
      importing
        document_proxy = document
        retcode        = retcode.
    call method c_oi_errors=>show_message
      exporting
        type = 'E'.

    call method document->play_document_from_table
      exporting
        document_table = doc_table[]
        document_size  = doc_size
        no_flush       = 'X'
      importing
        retcode        = retcode.

    call method document->save_copy_as
      exporting
        file_name   = 'D:\现金流量表.xls'
        no_flush    = 'X'
        prompt_user = ' '
      importing
        retcode     = retcode.

    call method c_oi_errors=>show_message
      exporting
        type = 'E'.

    perform frm_close_doc.
    perform frm_close_factory.
  else.
    message 'No document selected' type 'E'.
  endif.
endform.                    " FRM_FILE_OPEN
form frm_close_doc .
  if not document is initial.

    call method document->is_destroyed
      importing
        ret_value = is_closed.

    if is_closed is initial.
      call method document->close_document
        exporting
          do_save = 'X'
        importing
          retcode = retcode.
      call method c_oi_errors=>show_message
        exporting
          type = 'E'.
    endif.

    call method document->release_document
      exporting
        no_flush = ' '
      importing
        retcode  = retcode.
    free document.
  endif.

endform.                    " FRM_CLOSE_DOC
form frm_close_factory .
  if not link_server is initial.
    call method link_server->stop_link_server
      importing
        retcode = retcode.
    free link_server.
  endif.
*
  if not factory is initial.
    call method factory->stop_factory
      importing
        retcode = retcode.
    free factory.
  endif.
endform.                    " FRM_CLOSE_FACTORY

form frm_init_factory .

  if factory is initial.
    call method c_oi_factory_creator=>get_document_factory
      importing
        factory = factory
        retcode = retcode.
    if retcode ne c_oi_errors=>ret_ok.
      exit.
    endif.

    call method factory->start_factory
      exporting
        r3_application_name = sy-sysid
      importing
        retcode             = retcode.

    call method c_oi_errors=>show_message
      exporting
        type = 'E'.

    call method factory->get_link_server
      importing
        link_server = link_server
        retcode     = retcode.
    call method c_oi_errors=>show_message
      exporting
        type = 'W'.

    call method link_server->start_link_server
      importing
        retcode = retcode.

    call method c_oi_errors=>show_message
      exporting
        type = 'W'.
  else.
    call method link_server->stop_link_server
      importing
        retcode = retcode.
  endif.                               "factory IS INITIAL.

endform.                    " FRM_INIT_FACTORY

 

posted @ 2022-07-06 14:51  肆意点  阅读(109)  评论(0编辑  收藏  举报