我想知道是否有人知道一个高效的C#函数用于读取制表符分隔的文件转换成一个DataTable?
谢谢
我想知道是否有人知道一个高效的C#函数用于读取制表符分隔的文件转换成一个DataTable?
谢谢
这目前使用LINQ方法.First()
和.Skip()
都是容易重建,如果你需要使用此上的.Net 2.0
//even cooler as an extension method
static IEnumerable<string> ReadAsLines(string filename)
{
using (var reader = new StreamReader(filename))
while (!reader.EndOfStream)
yield return reader.ReadLine();
}
static void Main()
{
var filename = "tabfile.txt";
var reader = ReadAsLines(filename);
var data = new DataTable();
//this assume the first record is filled with the column names
var headers = reader.First().Split('\t');
foreach (var header in headers)
data.Columns.Add(header);
var records = reader.Skip(1);
foreach (var record in records)
data.Rows.Add(record.Split('\t'));
}
public System.Data.DataTable GetDataTable(string strFileName)
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + ";Extended Properties = \"Text;HDR=YES;FMT=TabDelimited\"");
conn.Open();
string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet("CSV File");
adapter.Fill(ds);
conn.Close();
return ds.Tables[0];
}
下面是做这件事?
var dt = new DataTable();
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
var lines = File.ReadAllLines(@"c:\tabfile.txt");
foreach( string line in lines )
dt.Rows.Add(line.Split('\t'));