文章目录
1.封装思路
直接复制粘贴激活直接用
首先,需要你在SE11中创建一个和你Excel中的字段相同的结构,然后把这个结构名字以字符串的形式传给方法.几乎可以实现任意扁平结构的Excel转到内表.
2.参数
2.1.参数解析
上面的图片可以比较清晰的说明各个参数的用法,以下字段详细介绍个别字段.
FILENAME 这个字段是你导入Excel文件的路径名称
IV_STRUCTURE_NAME 这个是你在SE11
创建的结构名, 结构名要以字符串的形式传,比如我的结构名叫 ZTEST_S
,你在传输的时候不能直接 IV_STRUCTURE_NAME = ZTEST_S
,要给他加上单引号!!
ET_TABLE 这个就是把Excel储存到内表了 ,要用你上面创建的结构为参考的内表去接收.
3.代码
代码具体实现逻辑相对简单不在阐述,自己去研究.
c
METHOD EXCEL_TO_INTERNAL_TABLE.
FIELD-SYMBOLS: <fv_value> TYPE any.
DATA: es_intern TYPE alsmex_tabline,
lv_index LIKE sy-tabix,
lo_structure TYPE REF TO cl_abap_structdescr,
lr_data TYPE REF TO data.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = filename
i_begin_col = i_begin_col
i_begin_row = i_begin_row
i_end_col = i_end_col
i_end_row = i_end_row
TABLES
intern = et_intern
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE 'Excel parsing failed' TYPE 'S' DISPLAY LIKE 'E' .
LEAVE LIST-PROCESSING.
ENDIF.
lo_structure ?= cl_abap_structdescr=>describe_by_name( p_name = iv_structure_name ).
CREATE DATA lr_data TYPE HANDLE lo_structure.
ASSIGN lr_data->* TO FIELD-SYMBOL(<lfs_structure>).
LOOP AT et_intern INTO es_intern.
lv_index = es_intern-col.
UNASSIGN <fv_value>.
ASSIGN COMPONENT lv_index OF STRUCTURE <lfs_structure> TO <fv_value>.
MOVE es_intern-value TO <fv_value>.
AT END OF row.
APPEND <lfs_structure> TO et_table.
ENDAT.
ENDLOOP.
ENDMETHOD.
4.调用案例
c
*&---------------------------------------------------------------------*
*& Report ZDEMO_REPORT006
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdemo_report006.
DATA:gv_structure_name TYPE tabname VALUE 'ZTEST_S'.
DATA gt_alv TYPE TABLE OF zafsfd.
DATA gs_alv TYPE zafsfd.
INCLUDE zdemo_report001_s01. "这个包含是显示alv的,你们自己加一下.
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE TEXT-001.
PARAMETERS: p_file LIKE rlgrap-filename DEFAULT 'C:\Users\Desktop\ExcelToITable.xlsx'
MODIF ID z01 MEMORY ID zfile OBLIGATORY.
SELECTION-SCREEN END OF BLOCK blk1.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM frm_get_filename CHANGING p_file.
START-OF-SELECTION.
PERFORM frm_read_excel_to_itable USING p_file.
IF gt_alv IS NOT INITIAL.
PERFORM frm_process_data(zdemo_report006) IF FOUND .
PERFORM pf_show_alv TABLES gt_alv.
ELSE.
MESSAGE '暂未读取到数据,请检查Excel!' TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form FRM_PROCESS_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_process_data .
DATA lv_ine TYPE c LENGTH 3.
SORT gt_alv BY ze3dh zzcdh zspkh .
DELETE ADJACENT DUPLICATES FROM gt_alv.
lv_ine = lines( gt_alv ).
CONCATENATE '共有组件条数:' lv_ine '条' INTO gs_alv-zline.
APPEND gs_alv TO gt_alv.
CLEAR gs_alv.
SORT gt_alv BY zline DESCENDING.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_FILENAME
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_P_FILE text
*----------------------------------------------------------------------*
FORM frm_get_filename CHANGING p_file.
DATA lo_fuc TYPE REF TO zcl_function_set02.
CREATE OBJECT lo_fuc.
DATA: lt_file_table TYPE filetable,
lv_action TYPE i.
CALL METHOD lo_fuc->file_open_dialog
EXPORTING
window_title = '打开Excel文件'
default_extension = '*.XLSX'
file_filter = 'Excel Files (*.xlsx)|*.xlsx|Excel Files (*.xls)|*.xls'
CHANGING
file_table = lt_file_table
user_action = lv_action
ev_path = p_file.
IF lv_action = 9.
MESSAGE '已取消选择!' TYPE 'S' DISPLAY LIKE 'E'.
RETURN.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_READ_EXCEL_TO_ITABLE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 textt
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_read_excel_to_itable USING p_file TYPE rlgrap-filename.
DATA lo_fuc TYPE REF TO zcl_function_set02.
CREATE OBJECT lo_fuc.
CALL METHOD lo_fuc->excel_to_internal_table
EXPORTING
filename = p_file
i_begin_col = 1
i_begin_row = 2
i_end_col = 4
i_end_row = 9999
iv_structure_name = 'ZAFSFD'
IMPORTING
et_table = gt_alv.
ENDFORM.
5.该类中的其他方法截图
本博客只介绍了一个方法,如果有感兴趣的靓仔靓女,可以私信我拿.
简单介绍一下,创建应用日志 的这个方法 目前已实现可配置的方式,对报表,屏幕实现日志跟踪,(思路:根据配置报表或屏幕的事务码+字段,就可跟踪该事务该字段的信息).
后续还会有更多方法更新!