文件上传与下载

span { font-family: "Courier New"; font-size: 12pt; color: rgb(0, 0, 0); background: none repeat scroll 0% 0% rgb(255, 255, 255); }.L1S31 { font-style: italic; color: rgb(128, 128, 128); }.L1S32 { color: rgb(51, 153, 255); }.L1S33 { color: rgb(77, 166, 25); }.L1S52 { color: rgb(0, 0, 255); } *&---------------------------------------------------------------------*
*& Report  ZBC_SMARTFORM
*&
*&---------------------------------------------------------------------*
*&Program title:作業区計画能力マスタの件数が多いので、CSVデータを取り込み、
*               追加更新したい。
*&Created by:
*&Description:CSVデータを取り込み、作業区計画能力マスタを追加、更新する。
*&            (極数標準能力)
*&---------------------------------------------------------------------*

REPORT   zbc_uploadfile NO  STANDARD  PAGE  HEADING LINE -SIZE  170  LINE -COUNT  58  MESSAGE -ID  z9as.
*上传的单条记录
TYPES :BEGIN  OF  t_record,
        zrecord TYPE  string,
      END  OF  t_record.
DATA  :i_record TYPE  STANDARD  TABLE  OF  t_record,
      wa_record LIKE  LINE  OF  i_record.
DATA : i_error TYPE  STANDARD  TABLE  OF  t_record,
      wa_error TYPE  t_record.
CONCATENATE  text -035  ','  text -036  INTO  wa_error-zrecord.
APPEND  wa_error TO  i_error.

TYPES :BEGIN  OF  t_upload,
  zflag TYPE  c ,                 "Wrong Flag
  arbpl TYPE  crhd-arbpl,        "作業区&プラント
  werks TYPE  t001w-werks,       "プラント
  zpole TYPE  z2cpwrkst-zpole,   "極数
  zstdcp TYPE  string,           "標準能力
  zerror TYPE  string,           "Wrong Describe
  END  OF  t_upload.
DATA  :i_upload TYPE  STANDARD  TABLE  OF  t_upload,
      wa_upload TYPE  t_upload.
DATA  : wa_z2cpwrkcp_mj TYPE  z2cpwrkcp_mj.

DATA  : i_z2cpwrkst TYPE  STANDARD  TABLE  OF  z2cpwrkst,
       wa_z2cpwrkst LIKE  LINE  OF  i_z2cpwrkst.

DATA : gv_sum TYPE  i ,
      gv_error TYPE  i ,
      gv_right TYPE  i .

*****************************************************************************
*SELECTION-SCREEN .                                                         *
*****************************************************************************
*SELECTION-SCREEN BEGIN OF SCREEN 500 TITLE text-b00.
SELECTION-SCREEN  BEGIN  OF  BLOCK  b1 WITH  FRAME  TITLE  text -b01.
PARAMETERS  : p_upload TYPE  rlgrap-filename LOWER  CASE .     "作業区計画能力マスタ極数標準能力ファイル
SELECTION-SCREEN  END  OF  BLOCK  b1.

SELECTION-SCREEN  BEGIN  OF  BLOCK  b2 WITH  FRAME  TITLE  text -b02.
PARAMETERS  : p_error TYPE  rlgrap-filename LOWER  CASE .      "取込エラーファイルディレクトリ
SELECTION-SCREEN  END  OF  BLOCK  b2.
*SELECTION-SCREEN END OF SCREEN 500.

AT  SELECTION-SCREEN  ON  p_upload.
  IF  p_upload IS  INITIAL .
    MESSAGE  e066(z9as) WITH  text -001  .
  ENDIF .

AT  SELECTION-SCREEN  ON  p_error.
  IF  p_error IS  INITIAL .
    MESSAGE  e066(z9as) WITH  text -003  .
  ENDIF .

AT  SELECTION-SCREEN  ON  VALUE -REQUEST FOR  p_upload.
  PERFORM  file_upload USING  p_upload.

AT  SELECTION-SCREEN  ON  VALUE -REQUEST FOR  p_error.
  PERFORM  file_download USING  p_error.

AT  SELECTION-SCREEN .
  PERFORM  check_file USING  p_upload.
  PERFORM  check_file USING  p_error.

AT  SELECTION-SCREEN  OUTPUT .
  SET  TITLEBAR  'TITLE' .
*****************************************************************************
*START-OF-SELECTION.                                                        *
*****************************************************************************
START-OF -SELECTION.
*  CALL SCREEN 500.
  DATA : filename TYPE  string.
  filename = p_upload.
  CALL  METHOD  cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                      = filename
      filetype                      = 'ASC'
*   HAS_FIELD_SEPARATOR           = ' '
*   HEADER_LENGTH                 = 0
*   READ_BY_LINE                  = 'X'
*   DAT_MODE                      = ' '
    codepage                      = '8400'
*   IGNORE_CERR                   = ABAP_TRUE
*   REPLACEMENT                   = '#'
*   CHECK_BOM                     = ' '
*   VIRUS_SCAN_PROFILE            =
*   NO_AUTH_CHECK                 = ' '
* IMPORTING
*   FILELENGTH                    =
*   HEADER                        =
  CHANGING
      data_tab                      = i_record
   EXCEPTIONS
     file_open_error               = 1
     file_read_error               = 2
     no_batch                      = 3
     gui_refuse_filetransfer       = 4
     invalid_type                  = 5
     no_authority                  = 6
     unknown_error                 = 7
     bad_data_format               = 8
     header_not_allowed            = 9
     separator_not_allowed         = 10
     header_too_long               = 11
     unknown_dp_error              = 12
     access_denied                 = 13
     dp_out_of_memory              = 14
     disk_full                     = 15
     dp_timeout                    = 16
     OTHERS                         = 17
            .
  IF  sy-subrc <> 0 .
    MESSAGE  ID  sy-msgid TYPE  sy-msgty NUMBER  sy-msgno
            WITH  sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF .
  LOOP  AT  i_record INTO  wa_record FROM  2 .
    IF  wa_record IS  INITIAL  .
      CONTINUE .
    ENDIF .
    CLEAR  wa_upload.
    gv_sum = gv_sum + 1 .
    SPLIT  wa_record-zrecord AT  ','
      INTO  wa_upload-arbpl wa_upload-werks wa_upload-zpole wa_upload-zstdcp.
    "校验werks是否存在于T001W
    DATA  lv_werks TYPE  t001w-werks.
    SELECT  SINGLE  werks
      INTO  lv_werks
      FROM  t001w
      WHERE  werks = wa_upload-werks.
    IF  sy-subrc <> 0  .
      gv_error = gv_error + 1 .
      wa_upload-zflag = 'X' .
      wa_upload-zerror = text -013 .
      APPEND  wa_upload TO  i_upload.
      CONCATENATE  text -038  ','  text -039  INTO  wa_error-zrecord.
      APPEND  wa_error TO  i_error.
      MESSAGE  i068(z9as) WITH  text -006  text -007  .
      CONTINUE .
    ENDIF .
    "校验arbpl,werks是否存在于CRHD
    DATA : lv_objty TYPE  crhd-objty,
          lv_objid TYPE  crhd-objid.
    SELECT  SINGLE  objty objid
      INTO  (lv_objty,lv_objid)
      FROM  crhd
      WHERE  arbpl = wa_upload-arbpl
        AND  werks = wa_upload-werks.
    IF  sy-subrc <> 0  .
      gv_error = gv_error + 1 .
      wa_upload-zflag = 'X' .
      wa_upload-zerror = text -014 .
      APPEND  wa_upload TO  i_upload.
      CONCATENATE  text -040  ','  text -041  INTO  wa_error-zrecord.
      APPEND  wa_error TO  i_error.
      MESSAGE  i069(z9as) WITH  text -008  text -009  .
      CONTINUE .
    ELSE .
      "校验objty ,objid是否存在于z2cpwrkcp_mj
      SELECT  SINGLE  *
        INTO  CORRESPONDING FIELDS  OF  wa_z2cpwrkcp_mj
        FROM  z2cpwrkcp_mj
        WHERE  zobjty = lv_objty
          AND  zobjid = lv_objid.
      IF  sy-subrc <> 0  .
        gv_error = gv_error + 1 .
        wa_upload-zflag = 'X' .
        wa_upload-zerror = text -037 .
        APPEND  wa_upload TO  i_upload.
        MESSAGE  i068(z9as) WITH  text -048  text -049  .
        CONTINUE .
      ENDIF .
    ENDIF .

*    DATA i TYPE i.
*    捕获字符赋值给数字的错误
*    CONVERSION_ERRORS contains CONVT_NO_NUMBER
*    CATCH SYSTEM-EXCEPTIONS conversion_errors = 1.
*      MOVE 'abc' TO i.          " <- Error: CONVT_NO_NUMBER
*    ENDCATCH.
*    IF sy-subrc = 1.
*      ...
*    ENDIF.
    DATA  : lv_zpole TYPE  zccp_pole,
           lv_zstdcp TYPE  zccp_stdcp.
    "判断極数类型是否正确
    CATCH  SYSTEM-EXCEPTIONS  conversion_errors = 1 .
      MOVE  wa_upload-zpole TO  lv_zpole.
    ENDCATCH.
    IF  sy-subrc = 1  .
      gv_error = gv_error + 1 .
      wa_upload-zflag = 'X' .
      wa_upload-zerror = text -042 .
      APPEND  wa_upload TO  i_upload.
      CONCATENATE  text -043  ','  text -044  INTO  wa_error-zrecord.
      APPEND  wa_error TO  i_error.
      MESSAGE  i071(z9as) WITH  text -043 .
      CONTINUE .
    ENDIF .
    "判断標準能力类型是否正确
    CATCH  SYSTEM-EXCEPTIONS  conversion_errors = 1 .
      MOVE  wa_upload-zstdcp TO  lv_zstdcp.
    ENDCATCH.
    IF  sy-subrc = 1  .
      gv_error = gv_error + 1 .
      wa_upload-zflag = 'X' .
      wa_upload-zerror = text -045 .
      APPEND  wa_upload TO  i_upload.
      CONCATENATE  text -046  ','  text -047  INTO  wa_error-zrecord.
      APPEND  wa_error TO  i_error.
      MESSAGE  i071(z9as) WITH  text -046 .
      CONTINUE .
    ENDIF .
    APPEND  wa_upload TO  i_upload.
    IF  wa_upload-zpole <> ''  AND   wa_upload-zstdcp <> ''
       AND  lv_objty <> ''  AND  lv_objid <> ''  .
      "判断记录是否存在,存在则更新,否则进行插入
      SELECT  SINGLE  *
        INTO  CORRESPONDING FIELDS  OF  wa_z2cpwrkst
        FROM  z2cpwrkst
        WHERE  zobjty = lv_objty
          AND  zobjid = lv_objid
          AND  zpole = wa_upload-zpole.
      "AND zstdcp = wa_upload-zstdcp.
      IF  sy-subrc <> 0 .
        "に同一キーのデータが存在しない場合、データを追加する。
        wa_z2cpwrkst-zobjty = lv_objty.         "オブジェクトタイプ
        wa_z2cpwrkst-zobjid = lv_objid.         "オブジェクトID
        wa_z2cpwrkst-zpole = wa_upload-zpole.   "極数
        wa_z2cpwrkst-zstdcp = wa_upload-zstdcp. "標準能力
        wa_z2cpwrkst-erdat = sy-datum.          "作成日
        wa_z2cpwrkst-erzet = sy-uzeit.          "作成時刻
        wa_z2cpwrkst-zepgm = sy-repid.          "作成プログラム
        wa_z2cpwrkst-ernam = sy-uname.          "作成ユーザー
        INSERT  into  z2cpwrkst values wa_z2cpwrkst.
        IF  sy-subrc <> 0  .
          MESSAGE  i006(z9as) WITH  text -012 .
        ENDIF .
      ELSE .
        "更新前加锁
        CALL  FUNCTION  'ENQUEUE_EZ2CPWRKST'
          EXPORTING
            mode_z2cpwrkst = 'E'
            mandt          = sy-mandt
            zobjty         = wa_z2cpwrkst-zobjty
            zobjid         = wa_z2cpwrkst-zobjid
            zpole          = wa_z2cpwrkst-zpole
            x_zobjty       = ' '
            x_zobjid       = ' '
            x_zpole        = ' '
            x_zstdcp       = ' '
            _scope         = '2'
            _wait          = ' '
            _collect       = ' '
          EXCEPTIONS
            foreign_lock   = 1
            system_failure = 2
            OTHERS          = 3 .
        IF  sy-subrc <> 0 .
          MESSAGE  e070.
        ENDIF .
        "に同一キーのデータが存在する場合、データを更新する。
        wa_z2cpwrkst-zstdcp = wa_upload-zstdcp. "標準能力
        wa_z2cpwrkst-aedtm = sy-datum.          "更新日
        wa_z2cpwrkst-aeuhr = sy-uzeit.          "更新時刻
        wa_z2cpwrkst-zvpgm = sy-repid.          "更新プログラム
        wa_z2cpwrkst-uname = sy-uname.          "更新ユーザー
*        MODIFY z2cpwrkst FROM wa_z2cpwrkst.
        UPDATE  z2cpwrkst FROM  wa_z2cpwrkst.
        "更新后解锁
        CALL  FUNCTION  'DEQUEUE_EZ2CPWRKST'
          EXPORTING
            mode_z2cpwrkst = 'E'
            mandt          = sy-mandt
            zobjty         = wa_z2cpwrkst-zobjty
            zobjid         = wa_z2cpwrkst-zobjid
            zpole          = wa_z2cpwrkst-zpole
            x_zobjty       = ' '
            x_zobjid       = ' '
            x_zpole        = ' '
            x_zstdcp       = ' '
            _scope         = '3'
            _synchron      = ' '
            _collect       = ' ' .
      ENDIF .
    ENDIF .
    CLEAR  wa_z2cpwrkst.
  ENDLOOP .
  gv_right = gv_sum - gv_error.                "正确的记录条数
  PERFORM  download_file.                       "导出错误信息
  PERFORM  output_detail.                       "输出详细记录清单
*  CALL SELECTION-SCREEN 500.
*&---------------------------------------------------------------------*
*&      Form  file_upload
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_UPLOAD  text
*----------------------------------------------------------------------*
FORM  file_upload  USING     p_upload.
  CALL  FUNCTION  'WS_FILENAME_GET'
   EXPORTING
     def_filename           = p_upload      "默认文件
     def_path               = 'C:'           "默认路径
     mask                    = ',*.CSV.'      "匹配模式
     mode                    = 'O'            "上传模式
     title                   = text -004       "弹出框名称
    IMPORTING
     filename               = p_upload
*    RC                     =
   EXCEPTIONS
     inv_winsys             = 1
     no_batch               = 2
     selection_cancel       = 3
     selection_error        = 4
     OTHERS                  = 5
            .
  IF  sy-subrc <> 0 .
    MESSAGE  ID  sy-msgid TYPE  sy-msgty NUMBER  sy-msgno
           WITH  sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF .

ENDFORM .                    " file_upload

*&---------------------------------------------------------------------*
*&      Form  file_download
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_ERROR  text
*----------------------------------------------------------------------*
FORM  file_download  USING     p_error.
  CALL  FUNCTION  'TMP_GUI_BROWSE_FOR_FOLDER'
    EXPORTING
      window_title    = text -005
      initial_folder  = 'C:'
    IMPORTING
      selected_folder = p_error
    EXCEPTIONS
      cntl_error      = 1
      OTHERS           = 2 .
  IF  sy-subrc <> 0 .
    MESSAGE  ID  sy-msgid TYPE  sy-msgty NUMBER  sy-msgno
           WITH  sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF .
ENDFORM .                    " file_download
*&---------------------------------------------------------------------*
*&      Form  check_file
*&---------------------------------------------------------------------*
*       text  检查文件是否存在
*----------------------------------------------------------------------*
*      -->file  text
*----------------------------------------------------------------------*
FORM  check_file  USING   file.
  DATA  : lv_result TYPE  c .
*  CALL METHOD cl_gui_frontend_services=>file_exist   "Checks if a File Exists
*    EXPORTING
*      file                 = file
*    RECEIVING
*      result               = lv_result
*    EXCEPTIONS
*      cntl_error           = 1
*      error_no_gui         = 2
*      wrong_parameter      = 3
*      not_supported_by_gui = 4
*      OTHERS               = 5.
  CALL  FUNCTION  'TMP_GUI_GET_FILE_EXIST'
    EXPORTING
      fname                = file
    IMPORTING
      exist                = lv_result
*     ISDIR                =
*     FILESIZE             =
   EXCEPTIONS
     fileinfo_error       = 1
     OTHERS                = 2
            .
  IF  sy-subrc <> 0 .
    SET  CURSOR  FIELD  'P_UPLOAD' .
    MESSAGE  ID  sy-msgid TYPE  'E'  NUMBER  sy-msgno
      WITH  sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF .
  IF  lv_result <> 'X' .
    SET  CURSOR  FIELD  'P_UPLOAD' .
    MESSAGE  e067(z9as) WITH  file.
  ENDIF .
ENDFORM .                    " check_file
*&---------------------------------------------------------------------*
*&      Form  output_detail
*&---------------------------------------------------------------------*
*       打印清单
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM  output_detail .
  DATA : lv_time TYPE  sy-uzeit,
        lv_execnum TYPE  i ,
        lv_execid(5TYPE  n.
  lv_time = sy-uzeit + 300 .
  "生成五位的随机数
  CALL  FUNCTION  'QF05_RANDOM_INTEGER'
    EXPORTING
      ran_int_max   = 99999
      ran_int_min   = 10000
    IMPORTING
      ran_int       = lv_execnum
    EXCEPTIONS
      invalid_input = 1
      OTHERS         = 2 .
  IF  sy-subrc <> 0 .
    MESSAGE  ID  sy-msgid TYPE  sy-msgty NUMBER  sy-msgno
            WITH  sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE .
    lv_execid = lv_execnum.
  ENDIF .

  WRITE :/ text -034  ,sy-pagno,
          /,
          / text -015 ,
          /2 text -016 ,40  text -00050  lv_execid,
          /2 text -017 ,40  text -00050  sy-datum USING  EDIT  MASK  '____/__/__'  ,
                70  sy-uzeit USING  EDIT  MASK  '__:__:__' ,'~' ,lv_time USING  EDIT  MASK  '__:__:__' ,
          /2 text -018 ,40  text -00050  sy-uname,
          /,
          /,
          / text -019 ,
          /2 text -020 ,40  text -00050  p_upload,
          /2 text -021 ,40  text -00050  p_error,
          /,
          /,
          / text -022 ,
          /,
          /,
          /,
          / text -023 ,
          /2 text -024 ,40  text -00050  gv_sum, 62  text -025 ,
          /2 text -026 ,40  text -00050  gv_right, 62  text -025 ,
          /2 text -027 ,40  text -00050  gv_error, 62  text -025 .
  NEW -PAGE  LINE -SIZE  170  LINE -COUNT  58 .
  WRITE : / text -034  ,sy-pagno,
         /,
         / text -028 ,
         / '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -' ,
         /10 text -029 ,30  text -030 ,50  text -031 ,70  text -032 ,90  text -033 ,
         / '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -' .
  LOOP  AT  i_upload INTO  wa_upload.
    IF  wa_upload-zflag = 'X' .
      WRITE  : / 'E' ,10  wa_upload-arbpl,30  wa_upload-werks,50  wa_upload-zpole,
        70  wa_upload-zstdcp,90  wa_upload-zerror.
    ELSE .
      WRITE  : /10 wa_upload-arbpl,30  wa_upload-werks,50  wa_upload-zpole,
        70  wa_upload-zstdcp,90  wa_upload-zerror.
    ENDIF .
  ENDLOOP .
ENDFORM .                    " output_detail
*&---------------------------------------------------------------------*
*&      Form  download_file
*&---------------------------------------------------------------------*
*      输出错误信息到文件
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM  download_file .
  DATA : lv_filename TYPE  string,
        lv_dat TYPE  string.
  CONCATENATE  sy-datum+0 (4 ) sy-datum+4 (2 ) sy-datum+6 (2 ) sy-uzeit+0 (2 ) sy-uzeit+4 (2INTO  lv_dat.
  CONCATENATE  p_error '/'  'download'  lv_dat '.csv'  INTO  lv_filename.
*  lv_filename = 'c:/download.csv'.
  CALL  FUNCTION  'GUI_DOWNLOAD'
    EXPORTING
*     BIN_FILESIZE                    =
     filename                        = lv_filename
     filetype                        = 'ASC'
     append                           = ' '
     write_field_separator           = ' '
     header                           = '00'
     trunc_trailing_blanks           = ' '
     write_lf                        = 'X'
     col_select                      = ' '
     col_select_mask                 = ' '
     dat_mode                        = ' '
     confirm_overwrite               = ' '
     no_auth_check                   = ' '
     codepage                        = '8400'
     ignore_cerr                     = abap_true
     replacement                     = '#'
     write_bom                       = ' '
     trunc_trailing_blanks_eol       = 'X'
     wk1_n_format                    = ' '
     wk1_n_size                      = ' '
     wk1_t_format                    = ' '
     wk1_t_size                      = ' '
*   IMPORTING
*     FILELENGTH                      =
   TABLES
     data_tab                        = i_error
*     FIELDNAMES                      =
   EXCEPTIONS
     file_write_error                = 1
     no_batch                        = 2
     gui_refuse_filetransfer         = 3
     invalid_type                    = 4
     no_authority                    = 5
     unknown_error                   = 6
     header_not_allowed              = 7
     separator_not_allowed           = 8
     filesize_not_allowed            = 9
     header_too_long                 = 10
     dp_error_create                 = 11
     dp_error_send                   = 12
     dp_error_write                  = 13
     unknown_dp_error                = 14
     access_denied                   = 15
     dp_out_of_memory                = 16
     disk_full                       = 17
     dp_timeout                      = 18
     file_not_found                  = 19
     dataprovider_exception          = 20
     control_flush_error             = 21
     OTHERS                           = 22
            .
  IF  sy-subrc <> 0 .
    MESSAGE  ID  sy-msgid TYPE  sy-msgty NUMBER  sy-msgno
            WITH  sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF .
ENDFORM .                    " download_file

你可能感兴趣的:(文件上传与下载)