• SAP ABAP基础语法-Excel上传(十)


    EXCEL BDS模板上传及赋值

    上传模板事务代码:OAER

    l  功能代码:向EXCEL模板中写入数据示例代码如下

    1. REPORT  ZEXCEL_DOI.
    2. “doi type pools
    3. TYPE-POOLS: soi.
    4. *SAP Desktop Office Integration Interfaces
    5. DATA: container TYPE REF TO cl_gui_custom_container,
    6.       control TYPE REF TO i_oi_container_control,
    7.       document TYPE REF TO i_oi_document_proxy,
    8.       spreadsheet TYPE REF TO i_oi_spreadsheet,
    9.       error TYPE REF TO i_oi_error,
    10.       errors TYPE REF TO i_oi_error OCCURS 0 WITH HEADER LINE.
    11. *dynpro fields
    12. CONTROLS: exceldata TYPE TABLEVIEW USING SCREEN 0100.
    13. DATA: tablename(10),ok_code(15),
    14.       save_ok(15),
    15.       row(4),column(4),data(39).
    16. *spreadsheet interface structures of the selected R/3 table
    17. DATA: cellitem TYPE soi_generic_item,
    18.       rangeitem TYPE soi_range_item,
    19.       ranges TYPE soi_range_list,
    20.       excel_input TYPE soi_generic_table,
    21.       excel_input_wa TYPE soi_generic_item,
    22.       initialized(1),
    23.       retcode TYPE soi_ret_string.
    24. *screen fields
    25. SET SCREEN 100.
    26. INCLUDE ZEXCEL_DOI_STATUS_0100O01.
    27. *&———————————————————————*
    28. *&      Module  USER_COMMAND_0100  INPUT
    29. *&———————————————————————*
    30. *       text
    31. *———————————————————————-*
    32. MODULE USER_COMMAND_0100 INPUT.
    33.   save_ok = ok_code.
    34.   CLEAR ok_code.
    35.   CASE save_ok.
    36.   WHEN ‘BACK’.
    37.     SET SCREEN 0.
    38.   WHEN ‘OUTPUT’.
    39.     DATA: fields_table TYPE TABLE OF rfc_fields,
    40.           tabname TYPE x030l-tabname,
    41.           ret TYPE i VALUE 0.
    42.     CHECK NOT tablename IS INITIAL.
    43.     tabname = tablename.
    44.     DATA: dref TYPE REF TO data,
    45.           dref_it TYPE REF TO data,
    46.           len_table TYPE i,
    47.           len_excel_len TYPE i.
    48.     FIELD-SYMBOLS: <tablewa> TYPE any,
    49.                    <it_table> TYPE any table.
    50.     CATCH SYSTEM-EXCEPTIONS create_data_unknown_type = 1
    51.                             OTHERS = 2.
    52.       CREATE DATA dref TYPE (tabname).
    53.       CREATE DATA dref_it TYPE TABLE OF (tabname).
    54.     ENDCATCH.
    55.     IF sy-subrc <> 0.
    56.       EXIT.
    57.     ENDIF.
    58.     ASSIGN: dref->* TO <tablewa>,
    59.             dref_it->* TO <it_table>.
    60.     DATA: count TYPE i.
    61.     CLEAR count.
    62.     DATA: BEGIN OF la_row,
    63.             row TYPE c LENGTH 500,
    64.           END OF la_row,
    65.           excel_table LIKE TABLE OF la_row,
    66.           myrow TYPE c LENGTH 500.
    67.     REFRESH excel_table.
    68.     SELECT * INTO TABLE <it_table>
    69.              FROM (tabname).
    70.     “create an excel range for data
    71.     DATA: rows_number TYPE i,
    72.           columns_number TYPE i.
    73.     CALL FUNCTION ‘RFC_GET_STRUCTURE_DEFINITION’
    74.      EXPORTING
    75.           tabname          = tabname
    76.      TABLES
    77.           fields           = fields_table
    78.      EXCEPTIONS
    79.           table_not_active = 1
    80.           OTHERS           = 2.
    81.     DESCRIBE TABLE fields_table LINES columns_number.
    82.     DESCRIBE TABLE <it_table> LINES rows_number.
    83.     CALL METHOD spreadsheet->insert_range_dim
    84.                   EXPORTING name = ‘SAP_Table’
    85.                             top  = ‘2
    86.                             left = ‘2
    87.                             rows = rows_number
    88.                             no_flush = ‘X’
    89.                             columns = columns_number
    90.                   IMPORTING error = errors.
    91.     APPEND errors.
    92.     CALL METHOD spreadsheet->set_color
    93.                   EXPORTING rangename = ‘SAP_Table’
    94.                             back = ‘50
    95.                             front = ‘2
    96.                             no_flush = ‘X’
    97.                   IMPORTING error = errors.
    98.     APPEND errors.
    99.     “Create range for comments
    100.     CALL METHOD spreadsheet->insert_range_dim
    101.                   EXPORTING name = ‘SAP_Comments’
    102.                             top  = ‘2
    103.                             left = ‘1
    104.                             rows = rows_number
    105.                             no_flush = ‘X’
    106.                             columns = 1
    107.                   IMPORTING error = errors.
    108.     APPEND errors.
    109.     CALL METHOD spreadsheet->set_color
    110.                   EXPORTING rangename = ‘SAP_Comments’
    111.                             back = ‘25
    112.                             front = ‘2
    113.                             no_flush = ‘X’
    114.                   IMPORTING error = errors.
    115.     APPEND errors.
    116.     DATA: title TYPE c LENGTH 30.
    117.     REFRESH:ranges,excel_input.
    118.     rangeitem-name = ‘SAP_Comments’.
    119.     rangeitem-columns = 1.
    120.     rangeitem-rows = 1.
    121.     APPEND rangeitem TO ranges.
    122.     excel_input_wa-column = 1.
    123.     excel_input_wa-row = 1.
    124.     CONCATENATE ‘R/3 table’ tabname ‘- comments’ INTO title
    125.                             SEPARATED BY space.
    126.     excel_input_wa-value = title.
    127.     APPEND excel_input_wa TO excel_input.
    128.     CALL METHOD spreadsheet->set_ranges_data
    129.                   EXPORTING ranges = ranges
    130.                             contents = excel_input
    131.                   IMPORTING error = errors.
    132.     APPEND errors.
    133.     “transfer R/3 table data into excel range
    134.     CALL METHOD spreadsheet->insert_one_table
    135.                   EXPORTING data_table = <it_table>
    136.                             ddic_name  = tabname
    137.                             rangename  = ‘SAP_Table’
    138.                             no_flush   = ‘X’
    139.                             wholetable = ‘X’
    140.                   IMPORTING error = errors.
    141.     APPEND errors.
    142.     “protect the R/3 data against input
    143.     CALL METHOD spreadsheet->fit_widest
    144.                   EXPORTING name = space
    145.                             no_flush = ‘X’.
    146.     CALL METHOD spreadsheet->protect_range
    147.                   EXPORTING name = ‘SAP_Table’
    148.                             protect = ‘X’
    149.                             no_flush = ‘X’
    150.                   IMPORTING error = errors.
    151.     APPEND errors.
    152.     “no flush automation queue
    153.     CALL METHOD control->set_focus
    154.             EXPORTING no_flush = ‘ ’
    155.             IMPORTING error = errors.
    156.     APPEND errors.
    157.     LOOP AT errors.
    158.       CALL METHOD errors->raise_message
    159.                   EXPORTING type = ‘E’.
    160.     ENDLOOP.
    161.     FREE errors.
    162.     REFRESH:excel_input,ranges.
    163.     CLEAR :excel_input_wa,rangeitem.
    164.   ENDCASE.
    165. ENDMODULE.                 “ USER_COMMAND_0100  INPUT
    166. *&———————————————————————*
    167. *&      Module  CREATE_BASIC_OBJECTS  OUTPUT
    168. *&———————————————————————*
    169. *       text
    170. *———————————————————————-*
    171. MODULE CREATE_BASIC_OBJECTS OUTPUT.
    172.   CHECK initialized IS INITIAL.
    173.   “first get doi i_oi_container_control interface
    174.   CALL METHOD c_oi_container_control_creator=>get_container_control
    175.               IMPORTING control = control
    176.                         error   = error.
    177.   “check no errors occured
    178.   CALL METHOD error->raise_message EXPORTING type = ‘E’.
    179.   “create a control container as defined in dynpro 0100
    180.   CREATE OBJECT container
    181.          EXPORTING container_name = ‘CONTAINER’.
    182.   “initialize the SAP DOI container,tell it to run in the container
    183.   “specified above and tell it to run excel inplace
    184.   CALL METHOD control->init_control
    185.         EXPORTING r3_application_name = ‘R/3 Basis’
    186.                   inplace_enabled = ”
    187.                   inplace_scroll_documents = ”
    188.                   parent = container
    189.                   register_on_close_event = ‘X’
    190.                   register_on_custom_event = ‘X’
    191.                   no_flush = ‘X’
    192.         IMPORTING error = errors.
    193. * save error object in collection
    194.   APPEND errors.
    195. * ask the SAP DOI container for a i_oi_document_proxy for Exel
    196.   CALL METHOD control->get_document_proxy
    197.           EXPORTING document_type = ‘Excel.Sheet’
    198.                     no_flush = ‘X’
    199.           IMPORTING document_proxy = document
    200.                     error = errors.
    201. *and then create a new Excel Sheet
    202.   CALL METHOD document->create_document
    203.               EXPORTING open_inplace = ”
    204.                         document_title = ‘R/3 table contents in Excel’
    205.                         no_flush = ‘X’
    206.               IMPORTING error = errors.
    207.   APPEND errors.
    208. * check if our document proxy can serve a spread sheet interface
    209.   DATA: has TYPE i.
    210.   CALL METHOD document->has_spreadsheet_interface
    211.              EXPORTING no_flush = ‘X’
    212.              IMPORTING is_available = has
    213.                        error = errors.
    214.   APPEND errors.
    215.   CALL METHOD document->get_spreadsheet_interface
    216.               EXPORTING no_flush = ‘ ’
    217.               IMPORTING sheet_interface = spreadsheet
    218.                         error = errors.
    219.   APPEND errors.
    220. * now loop through error collection because
    221. * Get_spreadsheet_interface flushed and synchronized
    222. * the automation queue !
    223.   LOOP AT errors.
    224.     CALL METHOD errors->raise_message
    225.                     EXPORTING  type     = ‘E’.
    226.   ENDLOOP.
    227.   initialized = ‘X’.
    228. ENDMODULE.                 “ CREATE_BASIC_OBJECTS  OUTPUT
    229. l  打开excel模板示例代码如下:
    230.   METHODS: open_excel_template IMPORTING                              im_clsnam TYPE sbdst_classname                                         im_clstyp TYPE sbdst_classtype                                   im_objkey TYPE sbdst_object_key                                  im_desc   TYPE char255,   “打开报表模板
    231. *———————————————-*
    232. * METHOD open_excel_template                   *
    233. * 打开excel模板
    234. *———————————————-*
    235.   METHOD open_excel_template.
    236.     DATA: locint_signature   TYPE sbdst_signature,
    237.           locint_uris        TYPE sbdst_uri,
    238.           locwa_signature    LIKE LINE OF locint_signature,
    239.           locwa_uris         LIKE LINE OF locint_uris.
    240. *   Create object for cl_bds_document_set
    241.     CREATE OBJECT r_document.
    242. *   Get Document with URL
    243.     locwa_signature-prop_name  = ‘DESCRIPTION’.
    244. *   Description of the table template in OAOR
    245.     locwa_signature-prop_value = im_desc.
    246.     APPEND locwa_signature TO locint_signature.
    247.     CALL METHOD r_document->get_with_url
    248.       EXPORTING
    249.         classname       = im_clsnam
    250.         classtype       = im_clstyp
    251.         object_key      = im_objkey
    252.       CHANGING
    253.         uris            = locint_uris
    254.         signature       = locint_signature
    255.       EXCEPTIONS
    256.         nothing_found   = 1
    257.         error_kpro      = 2
    258.         internal_error  = 3
    259.         parameter_error = 4
    260.         not_authorized  = 5
    261.         not_allowed     = 6.
    262.     IF sy-subrc NE 0.
    263.       MESSAGE ‘Error Retrieving Document’ TYPE ‘E’.
    264.     ENDIF.
    265. *   Create container control
    266.     CALL METHOD c_oi_container_control_creator=>get_container_control
    267.       IMPORTING
    268.         control = r_control
    269.         error   = r_error.
    270.     IF r_error->has_failed = ‘X’.
    271.       CALL METHOD c_oi_errors=>raise_message
    272.         EXPORTING
    273.           type = ‘E’.
    274.     ENDIF.
    275. * Initialize Custom Control
    276.     CREATE OBJECT r_container
    277.       EXPORTING
    278.         container_name = ‘TAB_EXCEL’. “Custom Control Name
    279.     CALL METHOD r_control->init_control
    280.       EXPORTING
    281.         r3_application_name      = ‘EXCEL INPLACE BDS’
    282.         inplace_enabled          = abap_false
    283.         inplace_scroll_documents = abap_true
    284.         parent                   = r_container
    285.       IMPORTING
    286.         error                    = r_error.
    287.     IF r_error->has_failed = ‘X’.
    288.       CALL METHOD c_oi_errors=>raise_message
    289.         EXPORTING
    290.           type = ‘E’.
    291.     ENDIF.
    292. * Create object for cl_bds_document_set
    293.     CREATE OBJECT r_document.
    294.     READ TABLE locint_uris INTO locwa_uris INDEX 1.
    295.     CALL METHOD r_control->get_document_proxy
    296.       EXPORTING
    297.         document_type  = ‘Excel.Sheet’
    298.       IMPORTING
    299.         document_proxy = r_proxy
    300.         error          = r_error.
    301.     IF r_error->has_failed = ‘X’.
    302.       CALL METHOD c_oi_errors=>raise_message
    303.         EXPORTING
    304.           type = ‘E’.
    305.     ENDIF.
    306. *   Open Document
    307.     CALL METHOD r_proxy->open_document
    308.       EXPORTING
    309.         document_url     = locwa_uris-uri
    310.         open_inplace     = abap_false
    311.         protect_document = abap_false “Protect Document initially
    312.       IMPORTING
    313.         error            = r_error.
    314.     IF r_error->has_failed = ‘X’.
    315.       CALL METHOD c_oi_errors=>raise_message
    316.         EXPORTING
    317.           type = ‘E’.
    318.     ENDIF.
    319. *   Get Excel Interface
    320.     CALL METHOD r_proxy->get_spreadsheet_interface
    321.       IMPORTING
    322.         sheet_interface = r_excel
    323.         error           = r_error.
    324.     IF r_error->has_failed = ‘X’.
    325.       CALL METHOD c_oi_errors=>raise_message
    326.         EXPORTING
    327.           type = ‘E’.
    328.     ENDIF.
    329.     CALL METHOD r_proxy->get_spreadsheet_interface
    330.         IMPORTING
    331.                 sheet_interface = r_handle.
    332.   ENDMETHOD.
    333. ENDCLASS.
    334. l  逐个CELL输入数据的示例代码如下:
    335.   fill_cell IMPORTING im_x TYPE i
    336.                       im_y TYPE i
    337.                       im_value TYPE char255,      “填充单元格
    338. ———————————————-*
    339. * METHOD fill_cell                             *
    340. * 向excel的单元格中插入数据
    341. *———————————————-*
    342.   METHOD fill_cell.
    343.     DATA: lv_columns TYPE i,
    344.           lv_rows TYPE i.
    345.     lv_columns = 1.
    346.     lv_rows = 1.
    347.     CALL METHOD r_handle->insert_range_dim
    348.          EXPORTING
    349.            NAME = ‘cell’
    350.            NO_FLUSH = ‘X’
    351.            TOP = im_x
    352.            LEFT = im_y
    353.            ROWS = lv_rows
    354.            COLUMNS = lv_columns
    355.          IMPORTING
    356.            error = r_error.
    357.     DATA: lt_ran TYPE soi_range_list,
    358.           lt_cont TYPE soi_generic_table,
    359.           la_ran LIKE LINE OF lt_ran,
    360.           la_cont LIKE LINE OF lt_cont.
    361.     la_ran-name = ‘cell’.
    362.     la_ran-columns = lv_columns.
    363.     la_ran-rows = lv_rows.
    364.     APPEND la_ran TO lt_ran.
    365.     la_cont-column = 1.
    366.     la_cont-row = 1.
    367.     la_cont-value = im_value.
    368.     APPEND la_cont TO lt_cont.
    369.     CALL METHOD r_handle->set_ranges_data
    370.       EXPORTING ranges = lt_ran
    371.                 contents = lt_cont
    372.       IMPORTING
    373.                 error = r_error.
    374.     CALL METHOD r_handle->fit_widest
    375.          EXPORTING
    376.            name  = space
    377.            no_flush = ‘X’.
    378.   ENDMETHOD.

  • 相关阅读:
    AgentVerse:清华等高校联手发布AI多智能体协作模拟框架
    如何写好技术文档 - 排版格式和规范(一)
    MySQL存储过程的基本用法
    用于Python降维的线性判别分析
    算法刷题日志——回溯算法
    Internet通过TCP/IP协议可以实现多个网络的无缝连接
    大数据分析平台有哪些应用价值
    Python Opencv实践 - 视频文件写入(格式和分辨率修改)
    Python程序设计实例 | 条形码图片识别
    08、http协议和dubbo协议的区别
  • 原文地址:https://blog.csdn.net/reagon2008/article/details/134307303