Creating csv file by reading text file by placing columns and data separately

The below code will read the text file and create csv file with columns and data. let us say access.txt file have data as like below

Name:,4th Floor, IT
Clearance name:,iT
Card number:,1234
Name:,Graham, John
Personnel Clearance Pair
Clearance name:,Temps
Card number:,23489
:

:
etc

so, use below code to read the text file and create csv file

try 
{
StreamReader sr = new StreamReader("C:\\Access.txt");
string csv = string.Empty;
string csv1 = string.Empty

line = sr.ReadLine();
line = line .split(":,");

if(line.length>1){
csv += line[0] + ','; // Adding header columns into string
csv1 += line[1]; // Adding the values against each header
}

while (line != null) //Continue to read until you reach end of file
{
line = sr.ReadLine();
}
csv += "\r\n"; //Add new line.
csv + = csv + csv1 + "\r\n";
//close the file
sr.Close();
Console.ReadLine();

//Download the CSV file.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=AccessCSVFile.csv");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(csv);
Response.Flush();
Response.End();

}
catch(Exception e){}
finally {}

Comments

Popular posts from this blog

Email Sending through O365 using OAuth Protocol

IoT Technology

What is reverse proxy?