IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> ABAP 事物码 SE11 的自动化 - 如何用 ABAP 代码创建一个数据库表并激活 -> 正文阅读

[系统运维]ABAP 事物码 SE11 的自动化 - 如何用 ABAP 代码创建一个数据库表并激活

*&---------------------------------------------------------------------*
*& Report  /SF0A0001/TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  /SF0A0001/TABLE.

INCLUDE ole2incl.

DATA:
      gt_title TYPE string_table,
      lr_type_descr type ref to cl_abap_typedescr,
      lr_type_descr2 type ref to cl_abap_structdescr,
      lr_table_descr type ref to cl_abap_tabledescr,
      gv_filename TYPE string,
      gv_path TYPE string,
      lr_table TYPE REF TO data,
      lt_comps type cl_abap_structdescr=>component_table,
      ls_comp like line of lt_comps,
      gv_fullpath TYPE string.

field-symbols: <lt_data> type table.

*&----------------------------------------------------*
*&   SELECTION-SCREEN                                 *
*&----------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1.
PARAMETERS: p_file TYPE string LOWER CASE OBLIGATORY,
            p_tab LIKE DD02L-TABNAME DEFAULT '/SF0A0001/REPORT'.
SELECTION-SCREEN END OF BLOCK b1.

*&----------------------------------------------------*
*&  START-OF-SELECTION                                *
*&----------------------------------------------------*
START-OF-SELECTION.
  PERFORM get_titles.
  PERFORM get_data.
  PERFORM create_excel USING p_file 'Flights Info' gt_title.
  WRITE: 'Export is finished successfully!'.

*&----------------------------------------------------*
*& AT SELECTION-SCREEN                                *
*&----------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title      = 'Select file'
      default_extension = 'xls'
      file_filter       = '*.xls'
    CHANGING
      filename          = gv_filename
      path              = gv_path
      fullpath          = gv_fullpath.

  IF sy-subrc EQ 0.
    p_file = gv_fullpath.
  ENDIF.


*&----------------------------------------------------*
*&      Macros                                        *
*&----------------------------------------------------*
  DEFINE handle_errors.
    if sy-subrc <> 0.
      write: 'Error in processing Excel file:', lv_step.
      stop.
    endif.
  END-OF-DEFINITION.


*&----------------------------------------------------*
*&      Form  get_titles                              *
*&----------------------------------------------------*
FORM get_titles.

  DATA: i_fieldcat TYPE LVC_T_FCAT.
  FIELD-SYMBOLS: <item> LIKE LINE OF i_fieldcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      I_STRUCTURE_NAME             = p_tab
    CHANGING
      ct_fieldcat                  = i_fieldcat.

  LOOP AT i_fieldcat ASSIGNING <item>.
    APPEND <item>-FIELDNAME TO gt_title.
  ENDLOOP.

ENDFORM.                    "get_titles

*&----------------------------------------------------*
*&      Form  get_data                                *
*&----------------------------------------------------*
FORM get_data.

  "call method cl_abap_typedescr=>describe_by_name
   CALL METHOD cl_abap_tabledescr=>describe_by_name
    exporting
      p_name         = p_tab
    receiving
      p_descr_ref    = lr_type_descr
    exceptions
      type_not_found = 1
      others         = 99.
  if sy-subrc <> 0.
    message 'dump' type 'X'.
  endif.

  ls_comp-type ?= lr_type_descr.
  ls_comp-as_include = 'X'.
  ls_comp-name = 'DATA'.
  APPEND ls_comp TO lt_comps.

  try.
      lr_type_descr2 = cl_abap_structdescr=>create(
       p_components = lt_comps ).
    catch cx_sy_struct_creation.
      message 'dump' type 'X'.
  endtry.

  try.
      call method cl_abap_tabledescr=>create
        exporting
          p_line_type  = lr_type_descr2
          p_table_kind = cl_abap_tabledescr=>tablekind_std
          p_unique     = abap_false
          p_key_kind   = cl_abap_tabledescr=>keydefkind_default
        receiving
          p_result     = lr_table_descr.
    catch cx_sy_table_creation.
      message 'dump' type 'X'.
  endtry.


  CREATE DATA lr_table TYPE HANDLE lr_table_descr.
  ASSIGN lr_table->* TO <lt_data>.
  ASSERT sy-subrc = 0.

  SELECT * FROM (p_tab) INTO CORRESPONDING FIELDS OF TABLE <lt_data>.
  check SY-SUBRC = 0.
  data: TT type TABLE OF /SF0A0001/REPORT.

  SELECT * FROM /SF0A0001/REPORT INTO CORRESPONDING FIELDS OF TABLE TT.
  CHECK SY-SUBRC = 0.
ENDFORM.                    " get_data

*&----------------------------------------------------*
*&      Form  create_excel                            *
*&----------------------------------------------------*
FORM create_excel USING pv_file TYPE string
                        pv_sheet_name TYPE string
                        pt_title TYPE string_table.


  DATA:
        lv_row TYPE i,
        lv_column TYPE i,
        lv_total_columns TYPE i,
        lv_field TYPE string,
        lv_step TYPE string.

*In a buffered method call (addition: NO FLUSH) of OLE you cannot use local variables at result variables.
*Use global variables or static variables (STATICS) instead.
  STATICS:lh_appl TYPE ole2_object,
          lh_workbook_list TYPE ole2_object,
          lh_workbook TYPE ole2_object,
          lh_sheet TYPE ole2_object,
          lh_columns TYPE ole2_object,
          lh_cell TYPE ole2_object,
          lh_interior TYPE ole2_object,
          lh_font TYPE ole2_object.


  FIELD-SYMBOLS:
         <ls_itab_row> TYPE ANY,
         <ls_title> TYPE string,
         <ls_itab_cell> TYPE ANY.

* We will use 'NO FLUSH' addition in this example:
* Normally, OLE statements are buffered by the ABAP processor and executed at the frontend collectively before
* the first statement which is not of OLE context. Using the 'NO FLUSH' addition prevents this and postpones
* the execution till just before the first non-OLE statement coming after an OLE statement without NO FLUSH
* addition.


* Start application and set it to invisible
  lv_step = 'Starting Excel...'.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' "Tell user what is going on
    EXPORTING
       percentage       = 0
       text             = lv_step
          .
  CREATE OBJECT lh_appl 'Excel.Application' NO FLUSH. " If addition 'NO FLUSH' is used, the return code sy-subrc is not filled so it remains 0.
  SET PROPERTY OF lh_appl 'SheetsInNewWorkbook' = 1 no flush.
  SET PROPERTY OF lh_appl 'Visible' = 0 . "Flush is executed after this OLE statement, so return code sy-subrc will be filled. We evaluate this return code for error handling.
  handle_errors.

* Add new workbook
  lv_step = 'Create workbook...'.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' "Tell user what is going on
    EXPORTING
       percentage       = 10
       text             = lv_step
       .
  GET PROPERTY OF lh_appl 'Workbooks' = lh_workbook_list no flush.
  CALL METHOD OF lh_workbook_list 'Add' = lh_workbook.
  handle_errors.

* Configure the active sheet
  lv_step = 'Configure sheet...'.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' "Tell user what is going on
    EXPORTING
       percentage       = 20
       text             = lv_step
       .
  GET PROPERTY OF lh_appl 'ActiveSheet' = lh_sheet  no flush.
  SET PROPERTY OF lh_sheet 'Name' = pv_sheet_name.
  handle_errors.

  lv_step = 'Writing title...'.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' "Tell user what is going on
    EXPORTING
       percentage       = 30
       text             = lv_step
       .
  LOOP AT pt_title ASSIGNING <ls_title>.
    lv_column = sy-tabix.

    CALL METHOD OF lh_appl 'Cells' = lh_cell NO FLUSH     "Get cell on the active worksheet in the application
      EXPORTING
      #1 = 1
      #2 = lv_column .

    handle_errors.

    SET PROPERTY OF lh_cell 'Value' =  <ls_title> no flush.
    GET PROPERTY OF lh_cell 'Interior' = lh_interior no flush.
    SET PROPERTY OF lh_interior 'ColorIndex' = 35 no flush.
    GET PROPERTY OF lh_cell 'Font' = lh_font no flush.
    SET PROPERTY OF lh_font 'Bold' = 1 no flush.

  ENDLOOP.
  CALL FUNCTION 'FLUSH'  "Trigger the flush explicitly
    EXCEPTIONS
      cntl_system_error = 1
      cntl_error        = 2
      OTHERS            = 3.
  handle_errors.


* Write actual data
  lv_step = 'Writing data...'.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' "Tell user what is going on
    EXPORTING
       percentage       = 40
       text             = lv_step
       .
  lv_total_columns = lv_column.
  lv_row = 1.

  LOOP AT <lt_data> ASSIGNING <ls_itab_row>.

    lv_row = lv_row + 1.
    DO lv_total_columns TIMES.
      lv_column = sy-index.
      CALL METHOD OF lh_appl 'Cells' = lh_cell NO FLUSH  "Get cell on the active worksheet in the application
        EXPORTING
        #1 = lv_row
        #2 = lv_column.

      handle_errors.

      ASSIGN COMPONENT lv_column OF STRUCTURE <ls_itab_row> TO <ls_itab_cell>.
      SET PROPERTY OF lh_cell 'Value' = <ls_itab_cell> no flush.

    ENDDO.
  ENDLOOP.
  CALL FUNCTION 'FLUSH'  "Trigger the flush explicitly
    EXCEPTIONS
      cntl_system_error = 1
      cntl_error        = 2
      OTHERS            = 3.
  handle_errors.


* Set column width
  lv_step = 'Setting column width...'.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' "Tell user what is going on
    EXPORTING
       percentage       = 50
       text             = lv_step
       .
  CALL METHOD OF lh_sheet 'Columns' = lh_columns NO FLUSH.
  CALL METHOD OF lh_columns 'Autofit'.
  handle_errors.

* Save and close
  lv_step = 'Saving and closing workbook...'.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' "Tell user what is going on
    EXPORTING
       percentage       = 60
       text             = lv_step
       .
  CALL METHOD OF lh_workbook 'SaveAs' NO FLUSH
    EXPORTING
    #1 = pv_file "File name
    .

  CALL METHOD OF lh_workbook 'Close' NO FLUSH.
  CALL METHOD OF lh_appl 'Quit' NO FLUSH.
  FREE OBJECT lh_appl.
  handle_errors.

ENDFORM.                    " create_excel
  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-03-30 19:10:50  更:2022-03-30 19:12:40 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 23:18:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码