读取文本文件时出现问题将其保存到C#中的csv文件中?
我的文本文件格式如下。我将保存到c#中的csv文件中。
//需要忽略人员清关对行,文本文件中的凭证行。
Access.text文件:
名称:,4楼,IT
人员清关对
清关名称:,iT
凭证
卡号:,1234
姓名:,Graham,John
人员清关对
清仓名称:,Temps
凭证
卡号:,23489
我正在循环文本文件中下面
Hi ,
I have text file format like below. I am saving into csv file in c#.
//need to ignore Personnel Clearance Pair line,Credential line in the text file.
Access.text file:
Name:,4th Floor, IT
Personnel Clearance Pair
Clearance name:,iT
Credential
Card number:,1234
Name:,Graham, John
Personnel Clearance Pair
Clearance name:,Temps
Credential
Card number:,23489
I am looping throught the text file like below
try
{
System.IO.TextWriter Summarytw = System.IO.File.CreateText(sOutputFilePath + sOutFileName_DST);
//Here I am adding column names to csv file(output file).
string line1 = string.Empty;
string strColumnname = string.Empty;//{"Name","Clearance name","Card number"};
strColumnname = "Name,Clearance name,Card number";
string[] arr = strColumnname.Split(',');
int a = arr.Length;
for (int y = 0; y < arr.Length; y++)
{
line += arr[y].ToString() + ",";
}
Summarytw.WriteLine(line);
//here I am reading the text file(input file)
using (System.IO.StreamReader sr = new System.IO.StreamReader(sInputPath + sInFile))
{
while ((line = sr.ReadLine()) != null)
{
//I need to save only name, clearance name, card number values to the csv file.
if ((line.Contains("Name")) || (line.Contains("Clearance name")) || (line.Contains("Card number")))
{
string s = line.ToString();
string pattern = ":,";
string strdelimiter = ",";
string[] substrings = Regex.Split(line, pattern);
for (int i = 1; i < substrings.Length; i++)
{
string value = substrings[1].ToString();
if (i == substrings.Length)
{
strconcat += " " + value + " ";
}
else
{
strconcat += "\" " + value + "\"";
strconcat += strdelimiter;
}
}
}
if (line.StartsWith("Card number"))
{
strconcat += Environment.NewLine;
}
}
Summarytw.WriteLine(strconcat);
}
}
for Access.txt代码中的上述行它工作正常。
但如果数据如下所示
for the above line in Access.txt code it was working fine.
but if the data is like below
Name:,lori, Julie
Personnel Clearance Pair
Clearance name:,Access Full Time Employees //No card number row
Credential
Name:,Badge 43511, Visitor
Personnel Clearance Pair//No clearance name row
Credential
Card number:,43511
如果有没有卡号或清关名称csv文件如下所示。
ACCESS.CSV:
名称清关名片号码
4楼, IT iT 1234
格雷厄姆,John Temps 23489
lori,Julie Access(如果没有卡号,则附有下一条记录详情)
徽章56892 56892(如果清关名称为空,则卡号将保存到csv文件中的清除名称字段中。)
文本文件数据是逐行的。
如何阅读并保存到csv文件
我尝试过的事情:
尝试了很多但失败了。
搜索在谷歌,但没有找到正确的答案。
请帮帮我。
谢谢inadvance。
if there is no card number or clearance name the csv file is looking like below.
ACCESS.CSV:
Name Clearance name card number
4th Floor, IT iT 1234
Graham, John Temps 23489
lori, Julie Access (if no card number it is placing with next record details)
Badge 56892 56892 (if clearance name is empty then card number Is saving into clearance name field in csv file.)
The text file data is line by line .
How to read and save it to csv file
What I have tried:
Tried many things but failed.
searched in google but didn't find proper answer.
Please help me.
Thanks inadvance.
但是如果总有一个名称,您可以检查以确定您正在检查的当前行是否是名称。 。 。如果它是concat Environment.Newline之前添加名称和以下字段以及逗号分隔行。这样,在那之后没有不存在的纪律时,你永远不必关心。如果它很重要,您可以检查以确定它是否是初始列表,并且可能不会在第一次出现之前添加一个全新的行。
But in case there is always a name you might check to determine whether the present line you are checking at is a name. . .if it is concat Environment.Newline previous to adding name and following fields alongside comma delimited line. That way you never have to care when there isn't a nonexistent discipline after that. In case it matters you can check to determine whether its the initial listing and perhaps not add a brand new line until the name the very first time around.