C# 中是否有任何 CSV 读取器/写入器库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1941392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:03:23  来源:igfitidea点击:

Are there any CSV readers/writer libraries in C#?

c#csv

提问by mrblah

Are there any CSVreaders/writer libraries in C#?

C# 中是否有任何CSV读取器/写入器库?

回答by Murph

Yes - though I assume you're actually asking for specifics?

是的 - 虽然我假设你实际上是在询问细节?

Try FileHelpers

尝试文件助手

回答by Alex

There are dozens.

有几十个。

http://www.filehelpers.net/is one of the most common.

http://www.filehelpers.net/是最常见的一种。

I should say that I find Filehelpers restrictive in some scenarios, and instead use The Fast CSV Reader. In my experience, if you don't know the format of your CSV file or import mapping until runtime - this is the better library to use.

我应该说我发现 Filehelpers 在某些情况下受到限制,而是使用The Fast CSV Reader。根据我的经验,如果您直到运行时才知道 CSV 文件的格式或导入映射 - 这是更好的库。

回答by Alex

Sebastien Lorion has a great CSVreader on CodeProject called A Fast CSV Reader. Probably one of the best for C# and it's free.

Sebastien LorionCSV在 CodeProject 上有一个很棒的阅读器,叫做A Fast CSV Reader。可能是 C# 中最好的之一,而且它是免费的。

As far as writing, just use StreamWriter.

至于写作,只需使用StreamWriter.

Here is some boilerplate code for writing a DataGridViewto a file:

这是一些用于将 a 写入DataGridView文件的样板代码:

private void exportDGVToCSV(string filename)
{
    if (dataGridView1.Columns.Count != 0) 
    {    
        using (Stream stream = File.OpenWrite(filename))
        {
            stream.SetLength(0);
            using (StreamWriter writer = new StreamWriter(stream))
            {
                // loop through each row of our DataGridView
                foreach (DataGridViewRow row in dataGridView1.Rows) 
                {
                    string line = string.Join(",", row.Cells.Select(x => $"{x}"));
                    writer.WriteLine(line);
                }

                writer.Flush();
            }
        };
    }
}

回答by Reed Copsey

There are a couple of options, right in the framework itself.

有几个选项,就在框架本身中。

One of the easiest is to Reference Microsoft.VisualBasic, then use TextFieldParser. It is a fully functional CSV reader in the core framework.

最简单的方法之一是引用 Microsoft.VisualBasic,然后使用TextFieldParser。它是核心框架中功能齐全的 CSV 阅读器。

Another good alternative is to use datasets to read CSV files.

另一个不错的选择是使用数据集来读取 CSV 文件

回答by Josh Close

Try CsvHelper. It's as easy to use as FastCsvReader and does writing also. I've been very happy with FastCsvReader in the past, but I needed something that does writing also, and wasn't happy with FileHelpers.

试试CsvHelper。它与 FastCsvReader 一样易于使用,也可以编写。过去我对 FastCsvReader 非常满意,但我也需要一些可以写作的东西,并且对 FileHelpers 不满意。

Reading:

读:

var csv = new CsvReader( stream );
var myCustomTypeList = csv.GetRecords<MyCustomType>();

Writing:

写作:

var csv = new CsvWriter( stream );
csv.WriteRecords( myCustomTypeList );

Full Disclosure: I am the author of this library.

完全披露:我是这个库的作者。