Совсем недавно стлокнулся в этой проблемой, вот как ее решил :
Скоприровал весь ALSM_EXCEL_TO_INTERNAL_TABLE и кое-что промодифицировал, попутно добавил возможность выбирать рабочий лист.
Цитата:
function zaqua_sd_read_excel .
*"----------------------------------------------------------------------
*"*"Локальный интерфейс:
*" 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
*" VALUE(I_SHEET_NUM) 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: excel type ole2_object,
application type ole2_object,
workbooks type ole2_object,
workbook type ole2_object,
range type ole2_object,
columns type ole2_object,
selection type ole2_object,
entirecolumn type ole2_object,
worksheets 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.
get property of application 'Parent' = excel.
m_message.
endif.
call method of application 'Workbooks' = workbooks.
m_message.
call method of workbooks 'Open' exporting #1 = filename
#2 = 0
#3 = 1.
m_message.
* set property of application 'Visible' = 1.
* m_message.
****************************************
* GET PROPERTY OF application 'ACTIVESHEET' = worksheet.
data sheet_count type i.
get property of application 'Worksheets' = worksheets.
m_message.
get property of worksheets 'Count' = sheet_count.
m_message.
if i_sheet_num > sheet_count or i_sheet_num < 0.
i_sheet_num = 1.
endif.
call method of worksheets 'Item' = worksheet exporting #1 = i_sheet_num.
m_message.
call method of worksheet 'Activate'. "<<<<<<<<<<<<< без этого не будет возможности выбирать рабочий лист
m_message.
****************************************
*Unhide all hidden columns and rows
get property of application 'Columns' = columns.
m_message.
call method of columns 'Select'.
m_message.
get property of application 'Selection' = selection.
m_message.
get property of selection 'EntireColumn' = entirecolumn.
m_message.
set property of entirecolumn 'Hidden' = 0 .
m_message.
****************************************
* 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
get property of application 'ActiveWorkbook' = workbook.
m_message.
set property of workbook 'Saved' = 1.
m_message.
* CALL METHOD OF workbook 'Close'.
* m_message.
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 workbooks. m_message.
free object workbook. m_message.
free object selection. m_message.
free object worksheets. m_message.
free object entirecolumn. m_message.
free object application. m_message.
free object excel. m_message.
* <<<<< End of change note 575877
endfunction.