设为首页收藏本站

SAP Best Business Solution

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1495|回复: 0

upload csv file with comma

[复制链接]

20

主题

21

帖子

164

积分

注册会员

Rank: 2

积分
164
发表于 2016-8-15 17:06:41 | 显示全部楼层 |阅读模式
本帖最后由 kevin_huo 于 2016-8-15 17:10 编辑

form read_forecast_file .
  types:begin of itab_type,
           value(2000),
         end of itab_type.
  data: it_data type standard table of itab_type with header line.
  data: it_data1 type standard table of itab_type with header line.
  data: wa_fcst_upload type t_fcst_upload.
  data: wa_fcst_cm_upload type t_fcst_cm_upload.
  data: wa_fcst_wcf_upload type t_fcst_wcf_upload.
  data: wa_fcst_commit_upload type t_fcst_commit_upload.
  data: l_ermsg(100).        "error message for OPEN DATASET
  data: l_rec(3000).
  data: l_col type i.

  if p_ufcstf(2) = '\\'.
    open dataset p_ufcstf for input in text mode
             encoding default message l_ermsg.
    if sy-subrc <> 0.
      write:/1 'Upload file open error. File directory and name:',
             p_ufcstf.
      stop.
    else.
      do.
        read dataset p_ufcstf into l_rec.
        if sy-subrc <> 0.
          exit.
        endif.
        clear it_fcst_upload.
        refresh it_data.
        clear it_data.
        perform process_csv_comma changing l_rec.
        split l_rec at ',' into table it_data.

        if sy-index = 1.
          describe table it_data lines l_col.
          if p_ufcst1 = 'X'.
            if l_col <> 67.
              message e001(00) with 'Upload file format is wrong'.
            endif.
          elseif p_ufcst2 = 'X'.
            if l_col <> 36 and l_col <> 71.
              message e001(00) with 'Upload file format is wrong'.
            endif.
          elseif p_ufcst4 = 'X'.
            if l_col <> 70 and l_col <> 72.
              message e001(00) with 'Upload file format is wrong'.
            endif.
          endif.
        endif.

        loop at it_data.
          replace all occurences of '"' in it_data-value with ''.
          replace all occurences of '|' in it_data-value with ','.
          condense it_data-value.
          if p_ufcst1 = 'X'.
            assign component sy-tabix of structure wa_fcst_upload to <fs>.
          elseif p_ufcst2 = 'X'.
            assign component sy-tabix of structure wa_fcst_commit_upload to <fs>.
          elseif p_ufcst3 = 'X'.
            assign component sy-tabix of structure wa_fcst_cm_upload to <fs>.
          elseif p_ufcst4 = 'X'.
            assign component sy-tabix of structure wa_fcst_wcf_upload to <fs>.
          endif.
          <fs> = it_data-value.
        endloop.
        if p_ufcst1 = 'X'.
          move-corresponding wa_fcst_upload to it_fcst_upload.
          append it_fcst_upload.
          clear  wa_fcst_upload.
        elseif p_ufcst2 = 'X'.
          move-corresponding wa_fcst_commit_upload to it_fcst_upload.
          it_fcst_upload-biz_process_flow = ''.
          append it_fcst_upload.
          clear  wa_fcst_commit_upload.
        elseif p_ufcst3 = 'X'.
          move-corresponding wa_fcst_cm_upload to it_fcst_upload.
          append it_fcst_upload.
          clear  wa_fcst_cm_upload.
        elseif p_ufcst4 = 'X'.
          move-corresponding wa_fcst_wcf_upload to it_fcst_upload.

          append it_fcst_upload.
          clear  wa_fcst_wcf_upload.
        endif.
      enddo.

* close file
      close dataset p_ufcstf.

      if sy-subrc <> 0.
        write:/1 'Uploading file error.File directory and name:',
                 p_ufcstf.
        stop.
      endif.
    endif.
  else.
    call function 'WS_UPLOAD'
      exporting
        filename            = p_ufcstf
        filetype            = 'DAT'
      tables
        data_tab            = it_data1
      exceptions
        conversion_error    = 1
        invalid_table_width = 2
        invalid_type        = 3
        no_batch            = 4
        unknown_error       = 5
        file_open_error     = 6
        file_read_error     = 7
        others              = 8.

    if not it_data1[] is initial.
      clear it_fcst_upload.
      refresh it_data.
      clear it_data.
      loop at  it_data1.
        perform process_csv_comma changing it_data1-value.
        split it_data1-value at ',' into table it_data.
        if sy-tabix = 1.
          describe table it_data lines l_col.
          if p_ufcst1 = 'X'.
            if l_col <> 67.
              message e001(00) with 'Upload file format is wrong'.
            endif.
          elseif p_ufcst2 = 'X'.
            if l_col <> 36 and l_col <> 71.
              message e001(00) with 'Upload file format is wrong'.
            endif.
          elseif p_ufcst4 = 'X'.
            if l_col <> 70 and l_col <> 72.
              message e001(00) with 'Upload file format is wrong'.
            endif.
          endif.
        endif.
        loop at it_data.
          replace all occurences of '"' in it_data-value with ''.
          replace all occurences of '|' in it_data-value with ','.
          condense it_data-value.

          if p_ufcst1 = 'X'.
            assign component sy-tabix of structure wa_fcst_upload to <fs>.
          elseif p_ufcst2 = 'X'.
            assign component sy-tabix of structure wa_fcst_commit_upload to <fs>.
          elseif p_ufcst3 = 'X'.
            assign component sy-tabix of structure wa_fcst_cm_upload to <fs>.
          elseif p_ufcst4 = 'X'.
            assign component sy-tabix of structure wa_fcst_wcf_upload to <fs>.
          endif.
          <fs> = it_data-value.
        endloop.
        if p_ufcst1 = 'X'.
          move-corresponding wa_fcst_upload to it_fcst_upload.
          append it_fcst_upload.
          clear  wa_fcst_upload.
        elseif p_ufcst2 = 'X'.
          move-corresponding wa_fcst_commit_upload to it_fcst_upload.
          append it_fcst_upload.
          clear  wa_fcst_commit_upload.
        elseif p_ufcst3 = 'X'.
          move-corresponding wa_fcst_cm_upload to it_fcst_upload.
          append it_fcst_upload.
          clear  wa_fcst_cm_upload.
        elseif p_ufcst4 = 'X'.
          move-corresponding wa_fcst_wcf_upload to it_fcst_upload.
          append it_fcst_upload.
          clear  wa_fcst_wcf_upload.
        endif.

      endloop.
    endif.
  endif.

  if it_fcst_upload[] is initial.
    message e001(00) with 'No data found!'.
    stop.
  else.
    read table it_fcst_upload index 1.
    if p_ufcst1 = 'X'.
      move-corresponding it_fcst_upload to  wa_fcst_title.
    elseif p_ufcst2 = 'X'.
      move-corresponding it_fcst_upload to  wa_fcst_commit_title.
    elseif p_ufcst3 = 'X'.
      move-corresponding it_fcst_upload to  wa_fcst_cm_title.
    elseif p_ufcst4 = 'X'.
      move-corresponding it_fcst_upload to   wa_fcst_wcf_title.
    endif.
    delete it_fcst_upload index 1.
  endif.

endform.                    " READ_forecast_FILE
form process_csv_comma  changing p_value.
  data: strlength type i,
        i type i,
        start.
  data: wa_file(5000).

  wa_file = p_value.
  condense wa_file.

  strlength = strlen( wa_file ).

  do strlength times.

    if wa_file+i(1) = '"'. "DOUBLE QUOTES
      if start is initial.
        start = 'X'.
      else.
        clear start.
      endif.
    endif.
    if not start is initial.
*    COMMA
      if wa_file+i(1) = ','.
*BETWEEN THE QUOTES WILL BE REPLACE BY WHAT TYPE OF CHARACTER
        wa_file+i(1) = '|'.
      endif.
    endif.
    add 1 to i.
  enddo.
  clear i.
  p_value = wa_file.
endform.                    " process_csv_comma
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



QQ|Archiver|手机版|小黑屋|www.sapbbs.com    

GMT+8, 2019-4-23 14:58 , Processed in 1.069322 second(s), 30 queries .

声明:本站严禁任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!

本站内容由网友原创或转载,如果侵犯了您的合法权益,请及时联系处理!© admin@sapbbs.com

快速回复 返回顶部 返回列表