IT干货网

【ABAP系列】SAP ABAP DOI展示EXCEL或WORD

xmjava 2022年03月17日 SAP 143 0

以下为正文

DOI技术算是比较老的技术了

用来直接调用office展示结果

可以是EXCEL也可以是WORD

​ 
data: begin of s_fal. 
        include structure faglflext. 
data: end of s_fal. 
data: i_fal like table of s_fal. 
data: ok_code like sy-ucomm. 
type-pools: soi,sbdst,abap. 
class c_oi_errors definition load. 
 
data control type ref to i_oi_container_control. 
data retcode  type  soi_ret_string. 
 
data: container type ref to cl_gui_custom_container. 
 
data: document type ref to i_oi_document_proxy. 
data: error        type ref to        i_oi_error. 
data: errors type ref to i_oi_error occurs 0. 
 
data spreadsheet type ref to i_oi_spreadsheet. 
data sheetname(20) type c. 
select * from faglflext into corresponding fields of table i_fal where rbukrs = '9900' and ryear = '2008' and racct = '0020110101'. 
 
 
call screen 100. 
*&---------------------------------------------------------------------* 
*&      Module  status_0100  OUTPUT 
*&---------------------------------------------------------------------* 
*       text 
*----------------------------------------------------------------------* 
module status_0100 output. 
  set pf-status '100'. 
 
 
  call method c_oi_container_control_creator=>get_container_control 
    importing 
      control = control 
      retcode = retcode. 
 
 
  create object container 
    exporting 
      container_name = 'DOI_PARENT'."100屏幕上的控件名. 
 
  call method control->init_control 
    exporting 
      r3_application_name = 'Demo Document Container' 
      inplace_enabled     = 'X' 
      parent              = container 
    importing 
      retcode             = retcode. 
 
  call method control->get_document_proxy 
    exporting 
      document_type   = 'Excel.Sheet.8' 
      document_format = 'OLE' 
    importing 
      document_proxy  = document 
      retcode         = retcode. 
 
  call method document->create_document 
    exporting 
      create_view_data = 'X' 
      open_inplace     = 'X' 
    importing 
      retcode          = retcode. 
  call method document->get_spreadsheet_interface 
    exporting 
      no_flush        = ' ' 
    importing 
      sheet_interface = spreadsheet 
      error           = error. 
  call method spreadsheet->get_active_sheet 
    exporting 
      no_flush  = '' 
    importing 
      sheetname = sheetname 
      error     = error 
      retcode   = retcode. 
  call method spreadsheet->add_sheet 
    exporting 
      name     = '年度报表' 
      no_flush = '' 
    importing 
      error    = error 
      retcode  = retcode. 
  call method spreadsheet->delete_sheet 
    exporting 
      name     = sheetname 
      no_flush = '' 
    importing 
      error    = error 
      retcode  = retcode. 
  call method spreadsheet->select_sheet 
    exporting 
      name     = '年度报表' 
      no_flush = '' 
    importing 
      error    = error 
      retcode  = retcode. 
 
  data: rows like sy-tabix. 
  data: field_count type i. 
  data: rangeitem type soi_range_item. 
  data: ranges type soi_range_list. 
  data: excel_input type soi_generic_table. 
  data: excel_input_wa type soi_generic_item. 
  field-symbols: <field> type any, 
                 <wa> type any. 
  field_count = 1. 
  do. 
    assign component field_count of structure s_fal to <field>."assign成功subrc = 0. 
    if sy-subrc <> 0. 
      exit. 
    endif. 
    add 1 to field_count. 
  enddo. 
  field_count = field_count - 1. 
  describe table i_fal lines rows. 
  call method spreadsheet->insert_range_dim 
    exporting 
      name     = 'CELL' 
      no_flush = 'X' 
      top      = 1 
      left     = 1 
      rows     = rows 
      columns  = field_count 
    importing 
      error    = error. 
 
  clear rangeitem. 
  refresh ranges. 
  rangeitem-name = 'CELL'. 
  rangeitem-columns = field_count. 
  rangeitem-rows = rows. 
  append rangeitem to ranges. 
  call method spreadsheet->set_font 
    exporting 
      rangename = 'CELL' 
      family    = 'Times New Roman' 
      size      = 9 
      bold      = 0 
      italic    = 0 
      align     = 0 
    importing 
      error     = error 
      retcode   = retcode. 
  call method spreadsheet->set_format 
    exporting 
      rangename = 'CELL' 
      typ       = 0 
      currency  = 'RMB' 
    importing 
      error     = error 
      retcode   = retcode. 
 
  refresh excel_input. 
 
  data: field_value type string. 
  loop at i_fal assigning <wa>. 
    rows = sy-tabix. 
    field_count = 1. 
    do. 
      assign component field_count of structure <wa> to <field>."assign成功subrc = 0. 
      if sy-subrc <> 0. 
        exit. 
      endif. 
      clear excel_input_wa. 
      excel_input_wa-column = field_count. 
      excel_input_wa-row = rows. 
      field_value = <field>. 
      excel_input_wa-value = field_value. 
      append excel_input_wa to excel_input. 
      add 1 to field_count. 
    enddo. 
  endloop. 
* set data 
  call method spreadsheet->set_ranges_data 
    exporting 
      ranges   = ranges 
      contents = excel_input 
      no_flush = 'X' 
    importing 
      error    = error. 
*get desktop directory 
  data: desktop_directory type string. 
  call method cl_gui_frontend_services=>get_sapgui_workdir 
    changing 
      sapworkdir            = desktop_directory 
    exceptions 
      get_sapworkdir_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. 
  endif. 
 
  if desktop_directory is initial. 
    desktop_directory = 'C:'. 
  endif. 
  concatenate desktop_directory '\' '年度报表.xls' into desktop_directory. 
 
  data: result type abap_bool. 
  call method cl_gui_frontend_services=>file_exist 
    exporting 
      file                 = desktop_directory 
    receiving 
      result               = result 
    exceptions 
      cntl_error           = 1 
      error_no_gui         = 2 
      wrong_parameter      = 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. 
  endif. 
  data: rc type i. 
  if result = 'X'. 
    call method cl_gui_frontend_services=>file_delete 
      exporting 
        filename             = desktop_directory 
      changing 
        rc                   = rc 
      exceptions 
        file_delete_failed   = 1 
        cntl_error           = 2 
        error_no_gui         = 3 
        file_not_found       = 4 
        access_denied        = 5 
        unknown_error        = 6 
        not_supported_by_gui = 7 
        wrong_parameter      = 8 
        others               = 9. 
    if sy-subrc <> 0. 
      message id sy-msgid type sy-msgty number sy-msgno 
                 with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 
    endif. 
  endif. 
  data:file_name(250) type c. 
  file_name = desktop_directory. 
  call method document->save_as 
    exporting 
      file_name   = file_name 
      prompt_user = '' 
    importing 
      error       = error 
      retcode     = retcode. 
*放到FTP 
*............................. 
*............................. 
*在这里写放到FTP上的语句. 
endmodule.                 " status_0100  OUTPUT 
*&---------------------------------------------------------------------* 
*&      Module  user_command_0100  INPUT 
*&---------------------------------------------------------------------* 
*       text 
*----------------------------------------------------------------------* 
module user_command_0100 input. 
  case ok_code. 
    when 'EXIT' or 'BACK'. 
      leave to screen 0. 
  endcase. 
endmodule.                 " user_command_0100  INPUT 
 
​

评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

【ABAP系列】SAP ABAP 开发中的SMARTFORMS 参数