kevin_huo 发表于 2016-8-15 17:06:41

upload csv file with comma

本帖最后由 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.
          clearwa_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.
          clearwa_fcst_commit_upload.
      elseif p_ufcst3 = 'X'.
          move-corresponding wa_fcst_cm_upload to it_fcst_upload.
          append it_fcst_upload.
          clearwa_fcst_cm_upload.
      elseif p_ufcst4 = 'X'.
          move-corresponding wa_fcst_wcf_upload to it_fcst_upload.

          append it_fcst_upload.
          clearwa_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 atit_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.
          clearwa_fcst_upload.
      elseif p_ufcst2 = 'X'.
          move-corresponding wa_fcst_commit_upload to it_fcst_upload.
          append it_fcst_upload.
          clearwa_fcst_commit_upload.
      elseif p_ufcst3 = 'X'.
          move-corresponding wa_fcst_cm_upload to it_fcst_upload.
          append it_fcst_upload.
          clearwa_fcst_cm_upload.
      elseif p_ufcst4 = 'X'.
          move-corresponding wa_fcst_wcf_upload to it_fcst_upload.
          append it_fcst_upload.
          clearwa_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 towa_fcst_title.
    elseif p_ufcst2 = 'X'.
      move-corresponding it_fcst_upload towa_fcst_commit_title.
    elseif p_ufcst3 = 'X'.
      move-corresponding it_fcst_upload towa_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_commachanging 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
页: [1]
查看完整版本: upload csv file with comma