DATA: BEGIN OF gs_data ,
    cola TYPE string,
    colb TYPE string,
    ...
  END OF gs_data,
  gt_data LIKE TABLE OF gs_data.

DATA lv_file LIKE rlgrap-filename VALUE '///'.
DATA lo_csv TYPE REF TO cl_rsda_csv_converter.
DATA lv_cpcodepage TYPE cpcodepage.
DATA lv_csv(1000).
DATA lv_cpcodepage TYPE cpcodepage.

CALL METHOD cl_rsda_csv_converter=>create
*  EXPORTING
*    i_delimiter = C_DEFAULT_DELIMITER
*    i_separator = C_DEFAULT_SEPARATOR
      RECEIVING
        r_r_conv    = lo_csv.
OPEN DATASET lv_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
  IF sy-subrc = 0.
    DO.
      READ DATASET lv_file INTO lv_csv.
      IF sy-subrc = 0.
        CLEAR gs_data.
*        去除CSV每一行结尾的换行符#        
        CALL FUNCTION 'NLS_GET_FRONTEND_CP'
          EXPORTING
            langu                 = sy-langu
          IMPORTING
            frontend_codepage     = lv_cpcodepage
          EXCEPTIONS
            illegal_syst_codepage = 1
            no_frontend_cp_found  = 2
            internal_or_db_error  = 3
            OTHERS                = 4.
        CALL FUNCTION 'SCP_REPLACE_STRANGE_CHARS'
          EXPORTING
            intext            = lv_csv
            inter_cp          = lv_cpcodepage
            replacement       = 32
          IMPORTING
            outtext           = lv_csv
          EXCEPTIONS
            invalid_codepage  = 1
            codepage_mismatch = 2
            internal_error    = 3
            cannot_convert    = 4
            fields_not_type_c = 5
            OTHERS            = 6.
        CALL METHOD lo_csv->csv_to_structure
          EXPORTING
            i_data   = lv_csv
          IMPORTING
            e_s_data = gs_data.
        APPEND gs_data TO gt_data.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.
    CLOSE DATASET lv_file.
  ELSE.
    MESSAGE 'Cannot open file!' TYPE 'E' .

 

posted on 2017-04-14 15:23  小伙儿  阅读(2683)  评论(0编辑  收藏  举报