narasiman rao

narasiman rao

  • NA
  • 519
  • 767.1k

how to save console output data into excel file using c#

Aug 13 2016 3:12 AM
 
 My console application code as follows
 
string connectionstring = "Server=(local);initial catalog=Test;Trusted_Connection=True";
SqlConnection sqlConnection = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
DataSet ds = new DataSet();
cmd.CommandText = "select * from Empdetails";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
    Console.WriteLine("ID | Name | Address | City | Phone \n {0} | {1} | {2} | {3} | {4}",        reader.GetInt32(0),
reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4));
}
}
sqlConnection.Close();
 
 
when i execute the above code in console application in command prompt output will displayed as follows
 
 
ID      Name      Address               City                Phone        Actvie
1        Ajay        LakeViewRaod Chennai          24851524        A
2        Suresh     GandhiNagar    Chennai          24745734        A
3        Vimal       BoagRoad        Chennai         24754874         A
4        Rahul     Alwarthirunagar Chennai         24758471         A
5       Karthik  GNTCheetyRoad Chennai         24845454         A
 
 
i want the above output to be save in excel in my download folder.
 
for that how can i do in asp.net using c# in console application
 
 
 
 

Answers (2)