【发布时间】:2010-12-20 18:15:19
【问题描述】:
我需要将类似这样的文件读入数据表:
A02 BLANK031
B02 F357442
C02 F264977
D02 BLANK037
E02 F272521
F02 E121562
G02 F264972
H02 F332321
A03 E208240
B03 F313854
C03 E229786
D03 E229787
E03 F307584
F03 F357478
我有一个奇怪的分隔符和一些尾随空格。 我如何将其读入数据表,使第一列包含“A02”、“B02”……第二列包含“BLANK031”、“F357442”等。
目前我正在做:
DataTable dt = new DataTable();
using (TextReader tr = File.OpenText(batchesAddresses[index]))
{
string line;
while ((line = tr.ReadLine()) != null)
{
string[] items = Regex.Split(line, ' ');
if (dt.Columns.Count == 0)
{
// Create the data columns for the data table based on the number of items
// on the first line of the file
for (int i = 0; i < items.Length; i++)
dt.Columns.Add(new DataColumn("Column" + i, typeof(string)));
}
dt.Rows.Add(items);
}
}
但这不起作用,因为我在列之间有尾随空格和多个空格
【问题讨论】:
-
对于什么数据库?大多数都可以从平面文件中导入数据,而无需 C#/Java/等。
-
@omg 它们是文本文件,它不是来自数据库
-
我说你要重新发明轮子