Upload a Comma Delimited CSV file that contains commas in data

source:http://wiki.sdn.sap.com/wiki/display/Snippets/Upload+a+Comma+Delimited+CSV+file+that+contains+commas+in+data

Some Comma Delimited CSV files will contain commas in the data fields and this causes problems in loading the file into our system. I don't think there is a standard function module to do this but I will share a small code snippet which will save someone to re-invent the wheel.

Here is a sample of the comma delimited CSV text file, you can copy below text then paste in notepad and save as c:\Temp\Book1.csv file.

10,Shafiq,"Sacramento, CA"
11,Willie,"New York, NY"
12,Conner,"Seattle, WA"

Here is the sample program which will read this file and store in a table that will preserve the commas in the city field:
[b]Upload from Local PC:[/b]

TYPES: BEGIN OF kcde_intern_struc.
INCLUDE STRUCTURE kcde_cells.
TYPES: END OF kcde_intern_struc.


DATA: l_intern TYPE TABLE OF kcde_intern_struc WITH HEADER LINE.

DATA: BEGIN OF it_output OCCURS 0,
id(10) TYPE c,
name(20) TYPE c,
location(20) TYPE c,
END OF it_output.
DATA: wa_output LIKE LINE OF it_output.
DATA: v_index TYPE i.

FIELD-SYMBOLS : <fs>.


CALL FUNCTION 'KCD_CSV_FILE_TO_INTERN_CONVERT'
EXPORTING
i_filename = 'C:\Temp\Book1.csv'
i_separator = ','
TABLES
e_intern = l_intern
EXCEPTIONS
upload_csv = 1
upload_filetype = 2.

LOOP AT l_intern.
MOVE : l_intern-col TO v_index.
ASSIGN COMPONENT v_index OF STRUCTURE wa_output TO <fs>.
MOVE : l_intern-value TO <fs>.
AT END OF row.
APPEND wa_output TO it_output.
CLEAR wa_output.
ENDAT.
ENDLOOP.

[b]Upload from Unix Server[/b]
DATA: BEGIN OF itab_upload OCCURS 0,
str(4096),
END OF itab_upload.
DATA: l_intern TYPE TABLE OF kcde_intern_struc WITH HEADER LINE.

OPEN DATASET p_path FOR INPUT ENCODING UTF-8 IN TEXT MODE.
DO.
READ DATASET p_path INTO itab_upload.
IF sy-subrc NE 0.
EXIT.
ENDIF.
APPEND itab_upload.
ENDDO.
CLOSE DATASET p_path.
PERFORM separated_to_intern_convert IN PROGRAM saplkcde
TABLES itab_upload l_intern USING ','.
LOOP AT l_intern.
MOVE : l_intern-col TO v_index.
ASSIGN COMPONENT v_index OF STRUCTURE wa_output TO <fs>.
MOVE : l_intern-value TO <fs>.
AT END OF row.
APPEND wa_output TO it_output.
CLEAR wa_output.
ENDAT.
ENDLOOP.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值