转自:http://blog.csdn.net/jy00873757/article/details/8534492
在SAP我们经常会用到*.txt, *.csv, *.xls三种文件格式
*.TXT
1. 一般用GUI_UPLOAD来处理, 请看下面的例子:
- TYPES: BEGIN OF ty_luke,
- key1 TYPE zluke-key1,
- year1 TYPE zluke-year1,
- month1 TYPE zluke-month1,
- test TYPE zluke-test,
- END OF ty_luke.
- DATA: lw_luke TYPE ty_luke,
- lt_luke TYPE STANDARD TABLE OF ty_luke.
- DATA: lv_file TYPE string VALUE 'D:\ZZERO2.TXT'.
- CALL FUNCTION 'GUI_UPLOAD'
- EXPORTING
- filename = lv_file
- filetype = 'ASC'
- has_field_separator = 'X'
- TABLES
- data_tab = lt_luke
- EXCEPTIONS
- file_open_error = 1
- file_read_error = 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.
- BREAK-POINT ID z_zero_test.
2. 这是upload的文件
3. 读取文件的结果
*CSV
1. 首先CSV是不能处理中文的, 请看下面截图:
当我保存这个文件后再打开, 字已经变了
2. 常用FM: KCD_CSV_FILE_TO_INTERN_CONVERT
- TYPES truxs_t_text_data(4096) TYPE c OCCURS 0.
- DATA: lv_file TYPE rlgrap-filename VALUE 'D:\UPLOAD.CSV'.
- TYPES: BEGIN OF kcde_intern_struc.
- INCLUDE STRUCTURE kcde_cells.
- * row type kcde_row_col,
- * col type kcde_row_col,
- * value type kcde_intern_value,
- TYPES: END OF kcde_intern_struc.
- TYPES: BEGIN OF ty_file,
- zfld1(4) TYPE c,
- zfld2(5) TYPE c,
- zfld3(6) TYPE c,
- END OF ty_file.
- DATA:lv_raw TYPE truxs_t_text_data.
- DATA: lt_file TYPE STANDARD TABLE OF ty_file,
- ls_file TYPE ty_file.
- DATA: lt_temp TYPE STANDARD TABLE OF kcde_intern_struc,
- ls_temp TYPE kcde_intern_struc.
- FIELD-SYMBOLS: <fs_file>.
- DATA: lv_index TYPE i.
- CALL FUNCTION 'KCD_CSV_FILE_TO_INTERN_CONVERT'
- EXPORTING
- i_filename = lv_file
- i_separator = ','
- TABLES
- e_intern = lt_temp
- EXCEPTIONS
- upload_csv = 1
- upload_filetype = 2
- OTHERS = 3.
- IF sy-subrc = 0.
- CLEAR: ls_temp, ls_file.
- LOOP AT lt_temp INTO ls_temp.
- CLEAR: lv_index.
- lv_index = ls_temp-col.
- ASSIGN COMPONENT lv_index OF STRUCTURE ls_file TO <fs_file>.
- IF sy-subrc = 0.
- <fs_file> = ls_temp-value.
- ENDIF.
- AT END OF row.
- APPEND ls_file TO lt_file.
- CLEAR ls_file.
- ENDAT.
- CLEAR ls_temp.
- ENDLOOP.
- ENDIF.
- BREAK-POINT.
3. Upload的文件
4. 读取文件的结果:
5. 加上我后面那段逻辑处理, 就能得到最后的结果
6. 注意: 这个FM不能处理",
把文件改成如下图所示:
再运行程序, 程序就会dump, 想知道为什么, 自己debug一下吧
*.XLS
1. 常用FM: TEXT_CONVERT_XLS_TO_SAP
- TYPE-POOLS: truxs.
- DATA: lv_file TYPE rlgrap-filename VALUE 'D:\ZZERO3.XLS'.
- TYPES: BEGIN OF ty_data,
- key(3) TYPE c,
- year(4) TYPE c,
- month(2) TYPE c,
- test(4) TYPE c,
- END OF ty_data.
- DATA: lv_raw TYPE truxs_t_text_data.
- DATA: lw_data TYPE ty_data,
- lt_data TYPE STANDARD TABLE OF ty_data.
- CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
- EXPORTING
- * I_FIELD_SEPERATOR =
- * I_LINE_HEADER =
- i_tab_raw_data = lv_raw
- i_filename = lv_file
- TABLES
- i_tab_converted_data = lt_data
- EXCEPTIONS
- conversion_failed = 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.
2. Upload的文件
3. 读取文件的结果: