ITPub博客

首页 > Linux操作系统 > Linux操作系统 > upload multiple excel sheet to internal table

upload multiple excel sheet to internal table

原创 Linux操作系统 作者:ultraluck 时间:2018-12-17 08:09:04 0 删除 编辑
upload multiple excel sheet to internal table
?概述:SAP系统提供了一个上传excel文件到sap 的函数 ALSM_EXCEL_TO_INTERNAL_TABLE,但是该函数只能上传一个sheet到内表中。本方法是扩展了该函数,使其可以上传多个sheet到内表。
??代码如下:
??function z_alsm_excel_to_internal_table .
??*"----------------------------------------------------------------------
??*"*"Lokale Schnittstelle:
??*" IMPORTING
??*" VALUE(FILENAME) LIKE RLGRAP-FILENAME
??*" VALUE(I_BEGIN_COL) TYPE I
??*" VALUE(I_BEGIN_ROW) TYPE I
??*" VALUE(I_END_COL) TYPE I
??*" VALUE(I_END_ROW) TYPE I
??*" TABLES
??*" INTERN STRUCTURE ALSMEX_TABLINE
??*" EXCEPTIONS
??*" INCONSISTENT_PARAMETERS
??*" UPLOAD_OLE
??*"----------------------------------------------------------------------
??
?? data: excel_tab type ty_t_sender.
?? data: ld_separator type c.
?? data: application type ole2_object,
?? workbook type ole2_object,
?? range type ole2_object,
?? worksheet type ole2_object.
?? data: h_cell type ole2_object,
?? h_cell1 type ole2_object.
?? data:
?? ld_rc type i.
??* Rückgabewert der Methode "clipboard_export "
??
??* Makro für Fehlerbehandlung der Methods
?? define m_message.
?? case sy-subrc.
?? when 0.
?? when 1.
?? message id sy-msgid type sy-msgty number sy-msgno
?? with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
?? when others. raise upload_ole.
?? endcase.
?? end-of-definition.
??
??
??* check parameters
?? if i_begin_row > i_end_row. raise inconsistent_parameters. endif.
?? if i_begin_col > i_end_col. raise inconsistent_parameters. endif.
??
??* Get TAB-sign for separation of fields
?? class cl_abap_char_utilities definition load.
?? ld_separator = cl_abap_char_utilities=>horizontal_tab.
??
??* open file in Excel
?? if application-header = space or application-handle = -1.
?? create object application 'Excel.Application'.
?? m_message.
?? endif.
?? call method of application 'Workbooks' = workbook.
?? m_message.
?? call method of workbook 'Open' exporting #1 = filename.
?? m_message.
??* set property of application 'Visible' = 1.
??* m_message.
??* get property of application 'ACTIVESHEET' = worksheet.
??* m_message.
??*---------------------------------------------------
?? call method of application 'WORKSHEETS' = worksheet
?? exporting
?? #1 = z_shname.
??
?? call method of worksheet 'ACTIVATE'.
??*---------------------------------------------------
??* mark whole spread sheet
?? call method of worksheet 'Cells' = h_cell
?? exporting #1 = i_begin_row #2 = i_begin_col.
?? m_message.
?? call method of worksheet 'Cells' = h_cell1
?? exporting #1 = i_end_row #2 = i_end_col.
?? m_message.
??
?? call method of worksheet 'RANGE' = range
?? exporting #1 = h_cell #2 = h_cell1.
?? m_message.
?? call method of range 'SELECT'.
?? m_message.
??
??* copy marked area (whole spread sheet) into Clippboard
?? call method of range 'COPY'.
?? m_message.
??
??* read clipboard into ABAP
?? call method cl_gui_frontend_services=>clipboard_import
?? importing
?? data = excel_tab
?? exceptions
?? cntl_error = 1
??* ERROR_NO_GUI = 2
??* NOT_SUPPORTED_BY_GUI = 3
?? others = 4
?? .
?? if sy-subrc <> 0.
?? message a037(alsmex).
?? endif.
??
?? perform separated_to_intern_convert tables excel_tab intern
?? using ld_separator.
??
??* clear clipboard
?? refresh excel_tab.
?? call method cl_gui_frontend_services=>clipboard_export
?? importing
?? data = excel_tab
?? changing
?? rc = ld_rc
?? exceptions
?? cntl_error = 1
??* ERROR_NO_GUI = 2
??* NOT_SUPPORTED_BY_GUI = 3
?? others = 4
?? .
??
??* quit Excel and free ABAP Object - unfortunately, this does not kill
??* the Excel process
?? call method of application 'QUIT'.
?? m_message.
??
??* >>>>> Begin of change note 575877
??* to kill the Excel process it's necessary to free all used objects
?? free object h_cell. m_message.
?? free object h_cell1. m_message.
?? free object range. m_message.
?? free object worksheet. m_message.
?? free object workbook. m_message.
?? free object application. m_message.
??* <<<<< End of change note 575877
??endfunction.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/190059/viewspace-476612/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2004-12-03

  • 博文量
    170
  • 访问量
    176243