Hi,
Thanks for your reply ,I have used below code
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);
}
}
if the data format is like below it is working fine
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
suppose the data is like below it is not working fine.
//if there is no card number line then next record is placing in that column
Name:,lori, Julie
Personnel Clearance Pair
Clearance name:,Access Full Time Employees //No card number row
Credential
//if clearance name line is missing then card number is saving into the clearance name field.
Name:,Badge 43511, Visitor
Personnel Clearance Pair//No clearance name row
Credential
Card number:,43511
what is wrong in my code , can you please let me know.
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.)