ABAP SMW0模板下载的错误处理及其优化
1、近期做这个模板下载的时候,发现有时候下载的excel打开时报如下错误
这个是后缀为XLSX时候的,如果是xls的话 打开时候里面直接是乱码。
所用代码如下:具体没深入研究 感觉是调用的几个函数问题
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 = 'ZSD115'. 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. DATA:BIN_FILESIZE TYPE I. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING FILENAME = L_PATH FILETYPE = 'BIN' BIN_FILESIZE = BIN_FILESIZE TABLES DATA_TAB = L_MIME. * * CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD * EXPORTING * BIN_FILESIZE = BIN_FILESIZE * FILENAME = L_PATH * FILETYPE = 'BIN' * CHANGING * DATA_TAB = L_MIME * . IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. WHEN 9. MESSAGE '下载取消' TYPE 'S'. WHEN OTHERS. ENDCASE.
2、将下载模板的函数改为调用类来解决
REPORT 11. DATA(L_FILENAME) = '示例导入模板.xlsx'. DATA(L_MUBAN) = 'ZSD115'. DATA:GV_FULLPATH TYPE STRING. DATA:GV_PATH TYPE STRING. DATA:GV_NAME TYPE STRING. *&---下载模板 PERFORM FRM_GET_FULLPATH CHANGING GV_FULLPATH GV_PATH GV_NAME. *&---路径为空则退出 IF GV_FULLPATH IS INITIAL. MESSAGE '用户取消操作' TYPE 'S'. RETURN. ENDIF. PERFORM FRM_DOWN USING GV_FULLPATH. *--------------------------------------------------------------------* * 获取下载到本地的路径 *--------------------------------------------------------------------* FORM FRM_GET_FULLPATH CHANGING PV_FULLPATH TYPE STRING PV_PATH TYPE STRING PV_NAME TYPE STRING. DATA: LV_INIT_PATH TYPE STRING, LV_INIT_FNAME TYPE STRING, LV_PATH TYPE STRING, LV_FILENAME TYPE STRING, LV_FULLPATH TYPE STRING. *&---初始名称(输出的文件名称) * concatenate 'Material_Doc_' SY-DATUM '.xslx' into L_INIT_FNAME. LV_INIT_FNAME = L_FILENAME. * 获取桌面路径 CALL METHOD CL_GUI_FRONTEND_SERVICES=>GET_DESKTOP_DIRECTORY CHANGING DESKTOP_DIRECTORY = LV_INIT_PATH EXCEPTIONS CNTL_ERROR = 1 ERROR_NO_GUI = 2 NOT_SUPPORTED_BY_GUI = 3 OTHERS = 4. IF SY-SUBRC <> 0. EXIT. ENDIF. *&---用户选择名称、路径 CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG EXPORTING * window_title = '指定保存文件名' * default_extension = 'DOC' DEFAULT_FILE_NAME = LV_INIT_FNAME * FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_EXCEL * FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_WORD INITIAL_DIRECTORY = LV_INIT_PATH PROMPT_ON_OVERWRITE = 'X' CHANGING FILENAME = LV_FILENAME PATH = LV_PATH FULLPATH = LV_FULLPATH * USER_ACTION = * FILE_ENCODING = EXCEPTIONS CNTL_ERROR = 1 ERROR_NO_GUI = 2 NOT_SUPPORTED_BY_GUI = 3 OTHERS = 4. IF SY-SUBRC = 0. PV_FULLPATH = LV_FULLPATH. PV_PATH = LV_PATH. ENDIF. ENDFORM. FORM FRM_DOWN USING PR_FILENAME. DATA: LV_OBJDATA LIKE WWWDATATAB, LV_MIME LIKE W3MIME, LV_DESTINATION LIKE RLGRAP-FILENAME, LV_OBJNAM TYPE STRING, LV_RC LIKE SY-SUBRC, LV_ERRTXT TYPE STRING. DATA: LV_FILENAME TYPE STRING, LV_RESULT, LV_SUBRC TYPE SY-SUBRC. DATA: LV_OBJID TYPE WWWDATATAB-OBJID . LV_OBJID = L_MUBAN. "上传的模版名称 *&---查找文件是否存在。 SELECT SINGLE RELID OBJID FROM WWWDATA INTO CORRESPONDING FIELDS OF LV_OBJDATA WHERE SRTF2 = 0 AND RELID = 'MI' AND OBJID = LV_OBJID. *&---判断模版不存在则报错 IF SY-SUBRC NE 0 OR LV_OBJDATA-OBJID EQ SPACE. CONCATENATE '模板文件:' LV_OBJID '不存在,请用TCODE:SMW0进行加载' INTO LV_ERRTXT. MESSAGE LV_ERRTXT TYPE 'E'. ENDIF. LV_FILENAME = PR_FILENAME. "判断本地地址是否已经存在此文件。 CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST EXPORTING FILE = LV_FILENAME RECEIVING RESULT = LV_RESULT EXCEPTIONS CNTL_ERROR = 1 ERROR_NO_GUI = 2 WRONG_PARAMETER = 3 NOT_SUPPORTED_BY_GUI = 4 OTHERS = 5. IF SY-SUBRC <> 0. CASE SY-SUBRC. WHEN 1. WHEN 2. WHEN OTHERS. ENDCASE. ENDIF. IF LV_RESULT EQ 'X'. "如果存在则删除原始文件,重新覆盖 CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_DELETE EXPORTING FILENAME = LV_FILENAME CHANGING RC = LV_SUBRC EXCEPTIONS FILE_DELETE_FAILED = 1 CNTL_ERROR = 2 ERROR_NO_GUI = 3 FILE_NOT_FOUND = 4 ACCESS_DENIED = 5 UNKNOWN_ERROR = 6 NOT_SUPPORTED_BY_GUI = 7 WRONG_PARAMETER = 8 OTHERS = 9. IF SY-SUBRC <> 0. CASE SY-SUBRC. WHEN 1. WHEN 2. WHEN OTHERS. ENDCASE. ENDIF. IF LV_SUBRC <> 0. "如果删除失败,则报错。 CONCATENATE '同名EXCEL文件已打开' '请关闭该EXCEL后重试。' INTO LV_ERRTXT. MESSAGE LV_ERRTXT TYPE 'E'. ENDIF. ENDIF. LV_DESTINATION = PR_FILENAME. *&---下载模版。 CALL FUNCTION 'DOWNLOAD_WEB_OBJECT' EXPORTING KEY = LV_OBJDATA DESTINATION = LV_DESTINATION IMPORTING RC = LV_RC. IF LV_RC NE 0. CONCATENATE '模板文件' '下载失败' INTO LV_ERRTXT. MESSAGE LV_ERRTXT TYPE 'E'. ENDIF. ENDFORM.
3、当然可以封装起来,方便调用。