2020.02.26 ABAP随笔- EXCEL批导程式1
Posted jxzhu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020.02.26 ABAP随笔- EXCEL批导程式1相关的知识,希望对你有一定的参考价值。
今天就只是复习下excel最简单得导入程式. 其他功能请后续关注。
*节选部分代码 TYPES:BEGIN OF ty_zmmt001, matnr TYPE zmmt001-matnr, id TYPE zmmt001-id, f1 TYPE zmmt001-f1, f2 TYPE zmmt001-f2, f3 TYPE zmmt001-f3, END OF ty_zmmt001. DATA lt_zmmt001 TYPE TABLE OF ty_zmmt001. DATA lt_excel TYPE TABLE OF alsmex_tabline. PARAMETERS p_file TYPE localfile MODIF ID gp2 OBLIGATORY MEMORY ID file. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file."这个就是获取文件路径得封装方法 见下方代码,可以直接使用 CALL METHOD zcl_document_jxzhu=>set_document_from_frontend CHANGING et_file_table = file_table file_path = p_file EXCEPTIONS file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_support_by_gui = 4 other_reasons = 5 OTHERS = 6. START-OF-SELECTION. CASE ‘X‘. WHEN p3."导入excel-ALSM_EXCEL_TO_INTERNAL_TABLE CHECK p_file IS NOT INITIAL. PERFORM frm_get_excel_1. ENDCASE. FORM frm_get_excel_1 . DATA lt_excel TYPE TABLE OF alsmex_tabline. *这个是SAP标准常用的函数,通过OLE来操作excel *缺点: 速度略慢,只能接收9999行数据 CALL FUNCTION ‘ALSM_EXCEL_TO_INTERNAL_TABLE‘ EXPORTING filename = p_file i_begin_col = ‘1‘ i_begin_row = ‘2‘ i_end_col = ‘20‘ i_end_row = ‘9999‘ TABLES intern = lt_excel[] EXCEPTIONS inconsistent_parameters = 1 s_file_ole = 2 OTHERS = 3. CHECK sy-subrc EQ 0. DATA ls_zmmt001 TYPE ty_zmmt001. LOOP AT lt_excel INTO DATA(ls_excel). AT NEW row. APPEND INITIAL LINE TO lt_zmmt001 ASSIGNING FIELD-SYMBOL(<fs>). ENDAT. ASSIGN COMPONENT ls_excel-col OF STRUCTURE <fs> TO FIELD-SYMBOL(<fs_filed>). IF sy-subrc EQ 0. <fs_filed> = ls_excel-value. ENDIF. ENDLOOP. cl_demo_output=>display( lt_zmmt001 ). ENDFORM.
METHOD set_document_from_frontend. DATA it_tab TYPE filetable. DATA returncode TYPE i. DATA extension TYPE string. CASE doc_type. WHEN ‘EXCEL‘. IF iv_window_title IS INITIAL. iv_window_title = |SELECT EXCEL FILE, E.G. *.XLSX|. ENDIF. extension = ‘.XLSX|.XLS‘. IF iv_file_filter IS INITIAL. iv_file_filter = ‘Excel(*.xlsx)|*.xlsx|Excel(*.xlsm)|*.xlsm|Excel 97-2003(*.xls)|*.xls‘. ENDIF. WHEN OTHERS. ENDCASE. CALL METHOD cl_gui_frontend_services=>file_open_dialog EXPORTING window_title = iv_window_title default_extension = extension * default_filename = file_filter = iv_file_filter * with_encoding = * initial_directory = multiselection = iv_multiselection CHANGING file_table = et_file_table rc = ev_rc * user_action = * file_encoding = EXCEPTIONS file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_supported_by_gui = 4 OTHERS = 5. IF sy-subrc <> 0. CASE sy-subrc. WHEN 1.RAISE file_open_dialog_failed. WHEN 2.RAISE cntl_error. WHEN 3.RAISE error_no_gui. WHEN 4.RAISE not_support_by_gui. WHEN OTHERS. RAISE other_reasons. ENDCASE. ENDIF. IF ev_rc < 1. RETURN. ENDIF. READ TABLE et_file_table ASSIGNING FIELD-SYMBOL(<selectedfilename>) INDEX 1. IF sy-subrc = 0. DATA(lv_len_s) = strlen( <selectedfilename>-filename ). DESCRIBE FIELD file_path LENGTH DATA(lv_len_d) IN CHARACTER MODE. IF lv_len_s > lv_len_d. MESSAGE i398(00) WITH ‘The filename length is too long‘(003). RETURN. ENDIF. file_path = <selectedfilename>-filename. ENDIF. ENDMETHOD.
效果为:
将excel中输入导入后显示。
-Tab Zhu 不念过去 不畏将来
METHOD set_document_from_frontend.
DATA it_tab TYPE filetable.
DATA returncode TYPE i.
DATA extension TYPE string.
CASE doc_type.
WHEN ‘EXCEL‘.
IF iv_window_title IS INITIAL.
iv_window_title = |SELECT EXCEL FILE, E.G. *.XLSX|.
ENDIF.
extension = ‘.XLSX|.XLS‘.
IF iv_file_filter IS INITIAL.
iv_file_filter = ‘Excel(*.xlsx)|*.xlsx|Excel(*.xlsm)|*.xlsm|Excel 97-2003(*.xls)|*.xls‘.
ENDIF.
WHEN OTHERS.
ENDCASE.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = iv_window_title
default_extension = extension
* default_filename =
file_filter = iv_file_filter
* with_encoding =
* initial_directory =
multiselection = iv_multiselection
CHANGING
file_table = et_file_table
rc = ev_rc
* user_action =
* file_encoding =
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.RAISE file_open_dialog_failed.
WHEN 2.RAISE cntl_error.
WHEN 3.RAISE error_no_gui.
WHEN 4.RAISE not_support_by_gui.
WHEN OTHERS. RAISE other_reasons.
ENDCASE.
ENDIF.
IF ev_rc < 1.
RETURN.
ENDIF.
READ TABLE et_file_table ASSIGNING FIELD-SYMBOL(<selectedfilename>) INDEX 1.
IF sy-subrc = 0.
DATA(lv_len_s) = strlen( <selectedfilename>-filename ).
DESCRIBE FIELD file_path LENGTH DATA(lv_len_d) IN CHARACTER MODE.
IF lv_len_s > lv_len_d.
MESSAGE i398(00) WITH ‘The filename length is too long‘(003).
RETURN.
ENDIF.
file_path = <selectedfilename>-filename.
ENDIF.
ENDMETHOD.
以上是关于2020.02.26 ABAP随笔- EXCEL批导程式1的主要内容,如果未能解决你的问题,请参考以下文章
2020.02.27 ABAP随笔- EXCEL批导程式2 - 突破9999行的束缚
2020.02.28 ABAP随笔- EXCEL批导程式3 - CL_EHFND_XLSX类 比较好用快速准确
2020.05.07 ABAP随笔- ABAP-SM30删除前检查
2020.05.07 ABAP随笔- ABAP-SM30删除前检查