narasiman rao

narasiman rao

  • NA
  • 519
  • 746.5k

how to add header name in console application in excel

Aug 26 2016 10:10 AM
 My code as follows
 
string connectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
SqlConnection sqlConnection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "select * from customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
 
if (reader.HasRows)
{
using (System.IO.StreamWriter fileOutput = new System.IO.StreamWriter(@"C:\Users\551386\Desktop\DataDump\ "Excel.xls"))
 
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
fileOutput.AutoFlush = true;
fileOutput.Write(reader[i].ToString() + "\t");
}
}
fileOutput.Write("\n");
}
}
sqlConnection.Close();
}
when i write the above and run  in excel data will be download as follows
 
    1        suresh   988789899    chennai    IN    1
    2        rajeshr   988788799   chennai     IN    2
    3        mootrhi  988787699    chennai    Out  3
 
 select * from customers records as follows
 
 id         Name       Mobile          Place     Type    Cityid
1          suresh    988789899    chennai      IN     1
2         rajeshr     988788799    chennai      IN     2
3        mootrhi     988787699    chennai     Out    3
 
 
but in excel the above column name is not coming in excel when i download.
 
id         Name      Mobile     Place        Type     Cityid
 
the above column name is not showing in excel when i download. 
what is the mistake in my above code.
 
 
 

Answers (1)