abap-excel导入模板
FORM frm_down_template . DATA: l_name LIKE wwwdatatab, l_mime LIKE w3mime OCCURS 10, l_path TYPE string, filename TYPE string, user_action TYPE i, z_file TYPE rlgrap-filename. CALL FUNCTION 'GUI_FILE_SAVE_DIALOG' EXPORTING window_title = '请选择保存文件的位置:' default_extension = '.XLSX' default_file_name = '报价审批导入模板' initial_directory = 'C:\' IMPORTING filename = filename fullpath = l_path user_action = user_action. "0生成;1替换;2扩展;9取消。 CASE user_action. WHEN 0 OR 1 OR 2. *---从SAP WEB 资源库中下载EXCEL模板到本地 l_name-relid = 'MI'. l_name-objid = 'ZSD113'. CALL FUNCTION 'WWWDATA_IMPORT' EXPORTING key = l_name TABLES mime = l_mime EXCEPTIONS wrong_object_type = 1 import_error = 2 OTHERS = 3. IF sy-subrc <> 0. MESSAGE '下载模板失败,请联系运维人员!' TYPE 'S'. STOP. ENDIF. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = l_path filetype = 'BIN' TABLES data_tab = l_mime. WHEN 9. MESSAGE '下载取消' TYPE 'S'. WHEN OTHERS. ENDCASE. ENDFORM.
FORM frm_get_file USING p_p_file. CALL FUNCTION 'WS_FILENAME_GET' EXPORTING def_filename = p_file mask = ',*.xlsx,*.XLSX,*.xls,*.XLS,*.txt,*.TXT' *'',*.xls,*.XLS.'如果读入txt文件,在后边就需要用函数 mode = 'O' title = 'File Name' IMPORTING filename = p_file EXCEPTIONS inv_winsys = 1 no_batch = 2 selection_cancel = 3 selection_error = 4 OTHERS = 5. ENDFORM.
FORM upload_excel_to_sap TABLES p_table USING p_tab_wa p_fpatch. DATA lt_xls TYPE zalsmex_tabline OCCURS 0 WITH HEADER LINE. DATA l_itab_col TYPE i. FIELD-SYMBOLS <fs_field>. IF p_file IS NOT INITIAL. CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE' EXPORTING filename = p_fpatch i_begin_col = 1 i_begin_row = 2 i_end_col = 100 i_end_row = 1000 TABLES intern = lt_xls EXCEPTIONS inconsistent_parameters = 1 upload_ole = 2 OTHERS = 3. IF sy-subrc <> 0. MESSAGE '上传文件失败!' TYPE 'S' DISPLAY LIKE 'E'. STOP. ELSE. LOOP AT lt_xls. l_itab_col = lt_xls-col. ASSIGN COMPONENT l_itab_col OF STRUCTURE p_tab_wa TO <fs_field>. <fs_field> = lt_xls-value. AT END OF row. APPEND p_tab_wa TO p_table. CLEAR: l_itab_col, p_tab_wa. ENDAT. CLEAR lt_xls. ENDLOOP. ENDIF. ENDIF. ENDFORM.