下载模板,上传EXCEL详解编程语言

说道SAP里对EXCEL操作的大概就是上传,下载,显示了。。。

下载:(文档是通过SMW0上传的)注:如果下载的时候需要填充EXCEL的值,。。。请参考另一篇文档,OLE

CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG 
    EXPORTING 
      DEFAULT_EXTENSION    = 'xlsx' 
      DEFAULT_FILE_NAME    = LC_FILENAME 
    CHANGING 
      FILENAME             = LC_FILENAME 
      PATH                 = LC_PATH 
      FULLPATH             = LC_FULLPATH 
    EXCEPTIONS 
      CNTL_ERROR           = 1 
      ERROR_NO_GUI         = 2 
      NOT_SUPPORTED_BY_GUI = 3 
      OTHERS               = 4. 
  IF LC_FULLPATH = ''. 
    MESSAGE  '不能打开excel' TYPE 'E'. 
  ENDIF. 
  IF SY-SUBRC = 0. 
    P_DEST = LC_FULLPATH. 
*    concatenate p_objid '.XLS' into ls_objnam. 
    CONDENSE LS_OBJNAM NO-GAPS. 
    SELECT SINGLE RELID OBJID FROM WWWDATA INTO CORRESPONDING FIELDS OF LO_OBJDATA 
           WHERE SRTF2 = 0 AND RELID = 'MI' AND OBJID = P_OBJID. 
    IF SY-SUBRC NE 0 OR LO_OBJDATA-OBJID EQ SPACE. 
      CONCATENATE '模板文件' LS_OBJNAM '不存在' INTO LS_ERRTXT. 
      MESSAGE LS_ERRTXT TYPE 'I'. 
    ENDIF. 
    LS_DESTINATION = P_DEST. 
    CALL FUNCTION 'DOWNLOAD_WEB_OBJECT' 
      EXPORTING 
        KEY         = LO_OBJDATA 
        DESTINATION = LS_DESTINATION 
      IMPORTING 
        RC          = LI_RC. 
    IF LI_RC NE 0. 
      CONCATENATE '模板文件:' LS_OBJNAM '下载失败' INTO LS_ERRTXT. 
      MESSAGE LS_ERRTXT TYPE 'E'. 
    ENDIF. 
  ENDIF.

上传:

选择屏上加个文件路径选择:

SELECTION-SCREEN:BEGIN OF BLOCK BLK01 WITH FRAME TITLE TEXT-001. 
PARAMETERS:P_FILE LIKE RLGRAP-FILENAME. 
SELECTION-SCREEN END OF BLOCK BLK01.

给文件搜索帮助:

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE. 
  PERFORM FRM_GET_FILEPATH.
FORM FRM_GET_FILEPATH . 
  CALL FUNCTION 'WS_FILENAME_GET' 
    EXPORTING 
      MASK             = ',Excel(*.xls),*.XLS,*.XLSX,' 
      TITLE            = '选择文件'(100) 
    IMPORTING 
      FILENAME         = P_FILE 
    EXCEPTIONS 
      INV_WINSYS       = 1 
      NO_BATCH         = 2 
      SELECTION_CANCEL = 3 
      SELECTION_ERROR  = 4 
      OTHERS           = 5. 
  IF SY-SUBRC <> 0. 
    MESSAGE E100(ZDEV) WITH '选择文件出错!'(007). 
  ENDIF. 
ENDFORM.

然后就是或许EXCEL内容了

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE' 
      EXPORTING 
        FILENAME    = P_FILE 
        I_BEGIN_COL = '1' 
        I_BEGIN_ROW = '2' 
        I_END_COL   = '300' 
        I_END_ROW   = '50000' 
      TABLES 
        INTERN      = GT_EXCEL_T.

这时候获取的EXCEL行,列,值

数据处理

LOOP AT GT_EXCEL_T INTO GS_EXCEL_T. 
    AT NEW ROW. 
      CLEAR:GW_EXCEL. 
    ENDAT. 
    CASE GS_EXCEL_T-COL. 
      WHEN 1. 
        GW_EXCEL-LIFNR = GS_EXCEL_T-VALUE. 
        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' 
          EXPORTING 
            INPUT  = GW_EXCEL-LIFNR 
          IMPORTING 
            OUTPUT = GW_EXCEL-LIFNR. 
      WHEN 2. 
        GW_EXCEL-MATNR = GS_EXCEL_T-VALUE. 
        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' 
          EXPORTING 
            INPUT  = GW_EXCEL-MATNR 
          IMPORTING 
            OUTPUT = GW_EXCEL-MATNR. 
      WHEN 3. 
        GW_EXCEL-EKORG = GS_EXCEL_T-VALUE. 
      WHEN 4. 
        GW_EXCEL-WERKS = GS_EXCEL_T-VALUE. 
      WHEN 5. 
        GW_EXCEL-NETPR = GS_EXCEL_T-VALUE. 
      WHEN 6. 
        GW_EXCEL-KPEIN = GS_EXCEL_T-VALUE. 
      WHEN 7. 
        GW_EXCEL-LIFAB = GS_EXCEL_T-VALUE. 
      WHEN 8. 
*        GW_EXCEL-NORBM = GS_EXCEL_T-VALUE. 
        GW_EXCEL-LIFBI = GS_EXCEL_T-VALUE. 
      WHEN 9. 
        GW_EXCEL-MWSKZ = GS_EXCEL_T-VALUE. 
      WHEN OTHERS. 
    ENDCASE. 
 
    AT END OF ROW. 
      APPEND GW_EXCEL TO GT_EXCEL. 
    ENDAT. 
  ENDLOOP.

这块可以优化,按自己的实际情况来

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/18631.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论