Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows

C#
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.CommandText += " where shifttype  = @par "
            cmd.Parameters.Add("@par", SqlDbType.Int).Value = j;
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection;
            sqlConnection.Open();
            reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                System.IO.StreamWriter sw_In = new System.IO.StreamWriter("C:\Users\God\Desktop\DataDump\" + j + "Excel.xls");
                while (reader.Read())
                {
                    for (int i = 0; i< reader.FieldCount; i++)
                    {
                        sw_In.AutoFlush = true;
                        sw_In.Write(reader[i].ToString() +"\t");
                    }
                        sw_In.Write("\n");
                }

            }
            sqlConnection.Close();
            reader.Close();



When i run the above code in excel record as follows

1    Suresh    9867689098    IN   Chennai
2    Rajesh    9840512112    IN   Chennai
3    Vikash    9844521213    IN   Chennai
4    Ramesh    9955454213    IN   Chennai
5    Kumar     9840521210    IN   Chennai



But in table
SQL
select * from Empdetails

record as follows with column name

ID    Name       Mobile      Shift    Place
1    Suresh    9867689098    IN     Chennai
2    Rajesh    9840512112    IN     Chennai
3    Vikash    9844521213    IN     Chennai
4    Ramesh    9955454213    IN     Chennai
5    Kumar     9840521210    IN     Chennai


In excel the above column to be there. for that how can i do in asp,net using C#.

in excel with column name records to be there.

What I have tried:

how to add column name in excel using C#
Posted
Updated 28-Aug-16 1:29am
v2
Comments
Richard MacCutchan 28-Aug-16 2:55am    
We have explained to you a number of times already, that you are not creating an Excel file, you are creating a simple tab separated text file. If you still do not understand that then there is little point in repeatedly posting the same question.

And of course, in the above code you do not write the header column names to the file before all the data.

You already know how to add rows of data coming from SQL reader.
Inserting a row of columns names is no more complicated.

1) build a list with columns names.
2) write that list just like you do with a row of data, but there you use the list.
3) write your rows of data as usual.
 
Share this answer
 
Comments
Maciej Los 28-Aug-16 7:53am    
+5
Patrice T 28-Aug-16 7:55am    
Thank you
 
Share this answer
 
v3
Comments
Patrice T 28-Aug-16 7:46am    
I think the code that give the columns names would be better before the loop that process the data rather than inside the inner loop.
Maciej Los 28-Aug-16 7:52am    
It depends on OP requirements, but on principle, i agree.
Patrice T 28-Aug-16 7:56am    
Sure, it is possible, but it complicate things.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900