读取CSV文件和TextFieldParser会跳过标题行.
任何想法如何确定第一行被跳过.
String[] Col3Value = new string[40];
TextFieldParser textFieldParser = new TextFieldParser(File1);
textFieldParser.TextFieldType = FieldType.Delimited;
textFieldParser.SetDelimiters(",");
{
{
string FileName = this.Variables.FileName.ToString();
{
while (!textFieldParser.EndOfData)
{
File1OutputBuffer.AddRow();
string[] values = textFieldParser.ReadFields();
for (int i = 0; i <= values.Length - 1; i++)
{
Col3Value[i] = values[i];
File1OutputBuffer.Column1 = Col3Value[0];
File1OutputBuffer.Column2 = Col3Value[1];
}
}
}
}
textFieldParser.Close();
}
Run Code Online (Sandbox Code Playgroud)
以未解析的方式显式读取第一行
if (!parser.EndOfData) {
parser.ReadLine();
}
while (!parser.EndOfData)
{
var fields = parser.ReadFields();
...
}
Run Code Online (Sandbox Code Playgroud)
您必须手动跳过第一行.
using (TextFieldParser parser = new TextFieldParser(path))
{
// set the parser variables
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
bool firstLine = true;
while (!parser.EndOfData)
{
//Processing row
string[] fields = parser.ReadFields();
// get the column headers
if (firstLine)
{
firstLine = false;
continue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
parser.LineNumber 从 1 开始,因此在第一次读取后继续。
while (!parser.EndOfData)
{
var fields = parser.ReadFields();
if (parser.LineNumber <= 2) continue;
...
}
Run Code Online (Sandbox Code Playgroud)