用于 C# 的 CSV 解析器/阅读器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/906841/
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
CSV parser/reader for C#?
提问by Bartosz Radaczyński
采纳答案by Keltex
回答by Anton Gogolev
There's a nice implementation on CodeProject:
CodeProject上有一个很好的实现:
To give more down to earth numbers, with a 45 MB CSV file containing 145 fields and 50,000 records, the reader was processing about 30 MB/sec. So all in all, it took 1.5 seconds! The machine specs were P4 3.0 GHz, 1024 MB.
为了提供更多实际数据,使用包含 145 个字段和 50,000 条记录的 45 MB CSV 文件,读取器的处理速度约为 30 MB/秒。总而言之,花了 1.5 秒!机器规格为 P4 3.0 GHz,1024 MB。
回答by no_one
try filehelpersWork amazingly well. I am using it to parse a 100 MB file every day.
试试filehelpers工作得非常好。我每天都用它来解析一个 100 MB 的文件。
回答by JaredPar
Have you tried the FileHelpers library? It's free, open source and can be used to parse CSV files.
您是否尝试过 FileHelpers 库?它是免费的、开源的,可用于解析 CSV 文件。
回答by zhao
I've started using CSV Parser that is part of the CommonLibrary.NET.
我已经开始使用 CSV 解析器,它是CommonLibrary.NET 的一部分。
It uses .NET 3.5, has an easy API, and convenient overloads/methods & lamda's for iterations.
它使用 .NET 3.5,具有简单的 API,以及方便的重载/方法和用于迭代的 lamda。
I don't have any benchmarks for this one like above, but nice thing about this is that it's just one component of a library similar to Java Commons. So I also get a Command-line parser, Repository implementation among other things.
我没有像上面那样的任何基准测试,但好处是它只是类似于 Java Commons 的库的一个组件。所以我还得到了一个命令行解析器、存储库实现等。
回答by Maxim
You can load a CSV file to DataTable.
您可以将 CSV 文件加载到 DataTable。
Sample code -
示例代码 -
static DataTable CsvToDataTable(string strFileName)
{
DataTable dataTable = new DataTable("DataTable Name");
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + Directory.GetCurrentDirectory() + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\""))
{
conn.Open();
string strQuery = "SELECT * FROM [" + strFileName + "]";
OleDbDataAdapter adapter =
new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
adapter.Fill(dataTable);
}
return dataTable;
}
Make sure you compile your project to x86 processor. It doesn't work for x64.
确保将项目编译为 x86 处理器。它不适用于 x64。