1.TOP
1.创建两个结构
1.1 对应excel模板 一一对应 顺序不能乱
1.2 对应alv显示
2.SEL
SELECTION-SCREEN BEGIN OF BLOCK bk1 WITH FRAME TITLE TEXT-001.
PARAMETERS p_file TYPE rlgrap-filename OBLIGATORY DEFAULT 'C:/'.
SELECTION-SCREEN END OF BLOCK bk1.
SELECTION-SCREEN: FUNCTION KEY 1.
3.INITIALIZATION
下载上传模板,模板在SWM0进行上传
DATA: lv_dyntxt TYPE smp_dyntxt.
lv_dyntxt-icon_id = icon_export.
lv_dyntxt-icon_text = '模板下载'.
lv_dyntxt-quickinfo = '模板下载'.
sscrfields-functxt_01 = lv_dyntxt.
4.AT SELECTION-SCREEN.
PAI事件
CASE sscrfields-ucomm.
WHEN 'FC01'. "模板下载
PERFORM frm_get_download_path.
WHEN 'ONLI'. "执行
WHEN OTHERS.
ENDCASE.
DATA: lv_filepath TYPE string.
DATA: lv_filename TYPE string,
lv_path TYPE string,
lv_fullpath TYPE string,
lv_titile TYPE string,
lv_init_dir TYPE string.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = '选择路径'
default_extension = '.xls'
initial_directory = 'C:/'
prompt_on_overwrite = 'X'
file_filter = 'Excel|*.xlsx;'
CHANGING
filename = lv_filename "文件名
path = lv_path "存放路径
fullpath = lv_fullpath "路径+文件名
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE '请选择正确的路径!' TYPE 'I' DISPLAY LIKE 'E'.
ENDIF.
PERFORM frm_download_excel USING lv_fullpath.
DATA:ls_objdata LIKE wwwdatatab,
lv_rc LIKE sy-subrc,
lv_filename LIKE rlgrap-filename.
IF p_filepath IS INITIAL OR p_filepath EQ space.
MESSAGE '请选择下载文件名' TYPE 'S' DISPLAY LIKE 'E'.
RETURN.
ENDIF.
SELECT
SINGLE relid objid
FROM wwwdata
INTO CORRESPONDING FIELDS OF ls_objdata
WHERE relid EQ 'MI' AND
objid EQ 'ZMMR034'.
IF sy-subrc NE 0.
MESSAGE '模板不存在!' TYPE 'I' DISPLAY LIKE 'E'.
RETURN.
ENDIF.
lv_filename = p_filepath.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = ls_objdata
destination = lv_filename
IMPORTING
rc = lv_rc
CHANGING
temp = lv_filename.
IF lv_rc NE 0.
MESSAGE '模板下载失败!' TYPE 'I' DISPLAY LIKE 'E'.
ENDIF.
5.POV事件
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CLEAR p_file.
"获取文件路径
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_path = 'C:/'
mask = 'Excel(*.xls;*.xlsx)|*.XLSX;*.XLS;'
title = '选择Excel文件'
IMPORTING
filename = p_file
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
IF sy-subrc <> 0 AND sy-subrc <> 3.
MESSAGE '选择文件出错' TYPE 'I' DISPLAY LIKE 'E'.
ENDIF.
6.START-OF-SELECTION
DATA: typedescr_ref TYPE REF TO cl_abap_typedescr.
DATA: BEGIN OF lt_excel OCCURS 0. "excel上载内表
INCLUDE STRUCTURE zalsmex_tabline.
DATA: END OF lt_excel.
DATA: lv_true TYPE c,
lv_flag TYPE c.
DATA:lv_kbetr TYPE kbetr_kond.
FIELD-SYMBOLS: <fs_excel> LIKE LINE OF lt_excel,
<fs_value>.
CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_file
* SHEET_NAME =
i_begin_col = 1
i_begin_row = 2
i_end_col = 16
i_end_row = 65000
TABLES
intern = lt_excel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE 'Excel解析失败!' TYPE 'S' DISPLAY LIKE 'E'.
RETURN.
ENDIF.
SORT lt_excel BY row col.
LOOP AT lt_excel ASSIGNING <fs_excel>.
"将第n列的值动态分配给<fs_value>
ASSIGN COMPONENT <fs_excel>-col OF STRUCTURE gs_excel TO <fs_value>.
IF sy-subrc = 0.
"通过值获取描述来对数量进行校验
typedescr_ref = cl_abap_typedescr=>describe_by_data( <fs_value> ).
CASE typedescr_ref->absolute_name.
"注意这比较的是数据元素
WHEN '/TYPE=MENGE_D'.
CALL FUNCTION 'ZNUMERIC_CHECK'
EXPORTING
input = <fs_excel>-value
i_decimals = 3
IMPORTING
boolean = lv_true
EXCEPTIONS
wrong_number = 1
OTHERS = 2.
IF lv_true = 'X'.
<fs_value> = <fs_excel>-value.
IF <fs_value> <= 0.
gs_alv-iconid = icon_led_red.
gs_alv-msg = gs_alv-msg && <fs_excel>-value && '数量不得为0;'.
ENDIF.
ELSE.
gs_alv-iconid = icon_led_red.
"拼接错误信息
gs_alv-msg = gs_alv-msg && <fs_excel>-value && '数字格式错误;'.
ENDIF.
WHEN OTHERS.
ENDCASE.
IF <fs_excel>-value = '必填' OR <fs_excel>-value = '可填'.
<fs_excel> = ''.
ELSE.
<fs_value> = <fs_excel>-value.
ENDIF.
ENDIF.
"在行变化的时候 将每行的数据添加到内表
AT END OF row.
MOVE-CORRESPONDING gs_excel TO gs_alv.
APPEND gs_alv TO gt_alv.
CLEAR: gs_alv.
gs_alv-iconid = icon_space.
ENDAT.
ENDLOOP.
7.Display数据即可
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/18011.html