如何将包含逗号的值写入csv文件?

时间:2014-03-03 10:25:12

标签: javascript csv comma

我有一个javascript程序可以将值写入csv文件。 我有一个包含逗号的值。 这该怎么做? 我是javascript的新手。 你能帮帮我吗?

我试过双引号

content += record.number +","+ record.category +","+ record.status +","+ record.approval_status +","+ record.requested_by +","+ record.assigned_to +","+record.assign_dept +","+ record.coordinator +","+ record.coord_phone +","+ record.planned_start +","+ record.planned_end +","+record.reason +","+ record.risk_assessment +","+ record.logical_name +","+ "record.assets" +","+ record.request_date +","+ record.subcategory +","+ record.priority_code +","+ record.impact +","+  record.sched_outage_start +","+ record.sched_outage_end  + "\n";

record.assets是将在其中包含逗号的字段。

谢谢, Chithra

2 个答案:

答案 0 :(得分:4)

用引号关闭它们并转义双引号?

http://en.wikipedia.org/wiki/Comma-separated_values

1999年,Chevy,“Venture”“Extended Edition”“”,4900.00

在你的情况下:

"," + "record.assets" + ","

应该是

"," + '"' + record.assets.replace(/"/g,'""') + '"' + ","

答案 1 :(得分:0)

您可以将文字放在引号之间。
您没有义务使用逗号,您可以使用“;” (荷兰语/德语格式,但效果很好)
- > http://en.wikipedia.org/wiki/Comma-separated_values
    1997;福特; E350; 2,34