引号内处理逗号导出CSV文件C#4.Any建议时, [英] Handling commas within quotes when exporting a CSV file C#4.Any suggestions

查看:435
本文介绍了引号内处理逗号导出CSV文件C#4.Any建议时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我读一个CSV文件,当有场里面一个逗号我已经中遇到问题

Hi all I am reading a csv file and I have encounted a problem when there is a comma inside the field

这所有的作品,我拿逗号
中的所有领域都会有内的一个逗号。周围

It all works till I get a comma within a comma all the fields will have "" around it.

             string delimiter=",";
         using (var sr = new StreamReader(fileName))
        {
                sr.ReadLine(); //skip headers
                while (!sr.EndOfStream)
                {
                   var readline = sr.ReadLine();
                   if (readline == null) continue;
                   var fields = readline.Split  (delimiter.ToCharArray());  
                 }
            }if I CANNOT split because of commas within quotation . How can I recode it?



我不能使用开源或thirdy党库。

I cannot use open source or thirdy party libraries.

有什么建议?

推荐答案

使用的 TextFieldParser 类。

Use the TextFieldParser class.

在MSDN上的描述是:

The description on MSDN is:

提供用于解析结构化的方法和属性文本文件。

Provides methods and properties for parsing structured text files.

它生活在 Microsoft.VisualBasic.FileIO 命名空间,所以没有第三方,也不开源。

It lives in the Microsoft.VisualBasic.FileIO namespace, so not third party nor open source.

这是一个管理类,所以你可以简单地添加一个引用,导入命名空间和使用。

It is a managed class, so you can simply add a reference, import the namespace and use.

实例:

using(var fileReader = New TextFieldParser("C:\ParserText.txt"))
{
    fileReader.TextFieldType = FieldType.Delimited;
    fileReader.SetDelimiters(",");
    ...
}

这篇关于引号内处理逗号导出CSV文件C#4.Any建议时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆