记录ABAP开发的日常——扩展EXCEL上载函数
前言:在日常的开发批导报表中,使用的excel上载标准函数ALSM_EXCEL_TO_INTERNAL_TABLE,其中存放数据的内表结构,行列数4位长度最大只支持到9999,这样在遇到一些数据量比较大的文件就不满足需求了,所以就需要对该函数进行扩展,保证它能够满足我们的需求。
扩展步骤:
1.复制标准函数ALSM_EXCEL_TO_INTERNAL_TABLE
2.编辑自定义函数
TYPE-POOLS: ole2.
* value of excel-cell
TYPES: ty_d_itabvalue TYPE zalsmex_tabline-value, "注意:若表结构更改了,这个也要改为对应的类型"
* internal table containing the excel data
ty_t_itab TYPE zalsmex_tabline OCCURS 0, "注意:若表结构更改了,这个也要改为对应的类型"
* line type of sender table
BEGIN OF ty_s_senderline,
line(4096) TYPE c,
END OF ty_s_senderline,
* sender table
ty_t_sender TYPE ty_s_senderline OCCURS 0.
*
CONSTANTS: gc_esc VALUE '"'.
引用:INCLUDE LALSMEXF01.
结构的内容如下:
3.扩展sheet
如果还想让函数根据sheet 页来读取数据,需要在自定义函数里添加参数:
然后在程序源代码里加入以下代码:
4.附函数主题代码
FUNCTION zfm_excel_to_internal_table .
*"----------------------------------------------------------------------
*"*"本地接口:
*" 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_NAME) TYPE CHAR50 OPTIONAL
*" TABLES
*" INTERN STRUCTURE ZALSMEX_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.
*<START>------「Add By Zhangyh Time: 17.12.2021 15:07:51」-------<START>*
IF i_sheet_name = space.
GET PROPERTY OF application 'ACTIVESHEET' = worksheet.
m_message.
ELSE.
CALL METHOD OF application 'WORKSHEETS' = worksheet
EXPORTING #1 = i_sheet_name.
CALL METHOD OF worksheet 'Activate'.
m_message.
ENDIF.
*<END>--------「Add By Zhangyh Time: 17.12.2021 15:07:51」---------<END>*
* 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.
觉得博主的文章对你有帮助的,动动鼠标一键三连,激励博主写出更多精彩文章!