可以通过过函数KCD_CSV_FILE_TO_INTERN_CONVERT将CSV格式的文件上载到本机。
代码如下:
REPORT z_upload_csv. TYPE-POOLS: kcde. TYPES: BEGIN OF ty_input, id TYPE string, name TYPE string, place TYPE string, phone TYPE string, END OF ty_input. CONSTANTS: c_separator TYPE c VALUE ‘,‘. DATA: gt_intern TYPE kcde_intern, gwa_intern TYPE kcde_intern_struc. DATA: gt_input TYPE TABLE OF ty_input, gwa_input TYPE ty_input. *----------------------------------------------------------------------* * SELECTION-SCREEN *----------------------------------------------------------------------* PARAMETERS: p_file TYPE localfile OBLIGATORY. *----------------------------------------------------------------------* * AT SELECTION-SCREEN ON VALUE-REQUEST *----------------------------------------------------------------------* AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. *F4 help for the file in selection screen CALL FUNCTION ‘F4_FILENAME‘ IMPORTING file_name = p_file. *----------------------------------------------------------------------* * START-OF-SELECTION *----------------------------------------------------------------------* START-OF-SELECTION. * Read data from File PERFORM readfile USING p_file. * Display data PERFORM display. *&---------------------------------------------------------------------* *& Form READFILE *&---------------------------------------------------------------------* FORM readfile USING p_file TYPE localfile. DATA: lv_filename TYPE rlgrap-filename. DATA: lv_index TYPE i. FIELD-SYMBOLS: <lfs_field> TYPE any. lv_filename = p_file. * 读取CSV文件 CALL FUNCTION ‘KCD_CSV_FILE_TO_INTERN_CONVERT‘ EXPORTING i_filename = lv_filename i_separator = c_separator TABLES e_intern = gt_intern EXCEPTIONS upload_csv = 1 upload_filetype = 2 OTHERS = 3. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. * 将读取的CSV内容转换保存到内表gt_input中 LOOP AT gt_intern INTO gwa_intern. MOVE : gwa_intern-col TO lv_index. ASSIGN COMPONENT lv_index OF STRUCTURE gwa_input TO <lfs_field>. MOVE : gwa_intern-value TO <lfs_field> . AT END OF row. APPEND gwa_input TO gt_input. CLEAR gwa_input. ENDAT. ENDLOOP. ENDFORM. " READFILE *&---------------------------------------------------------------------* *& Form DISPLAY *&---------------------------------------------------------------------* FORM display. * 输出CSV内容 LOOP AT gt_input INTO gwa_input. WRITE:/ gwa_input-id,8 gwa_input-name,16 gwa_input-place,26 gwa_input-phone. ENDLOOP. ENDFORM. " DISPLAY
引用来自 http://www.baidusap.com/abap/function/2114
原文地址:https://www.cnblogs.com/wangxiaowen/p/8997692.html
时间: 2024-10-15 11:09:58