01
Excel简单导入
大概写了一个如下图程式:今天给一下ALSM_EXCEL_TO_INTERNAL的使用方法
Excel导入的功能是项目实施中很常见的功能。主要用于批导的功能,期初数据上传,批量维护等。
首先在选择屏幕:
代码语言:javascript复制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.
PARAMETERS p_file TYPE localfile MODIF ID gp2. “文件路径
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.
zcl_document_jxzhu=>set_document_from_frontend 的方法代码如下:
同学们可以根据自己的喜好进行修改,SE24创建可以一直使用
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中导入数据到内表中: 你以为我会用ALSM_EXCEL_TO_INTERNAL_TABLE ? 好吧今天代码如下
代码语言:javascript复制 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[] “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 ).
执行效果如下: