Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

What I have tried:

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
Posted
Updated 12-Aug-16 22:11pm

1 solution

I'm not quite sure what asp.net has to do with a console application ... if you wish to write your output to a file that can be read by excel, then, something like this

C#
using (System.IO.StreamWriter fileOutput = 
   new System.IO.StreamWriter(@"C:\Some Path\sudhakarthikeyan.csv"))
{
  while (reader.Read()
  {
    fileOutput.WriteLine("{0},{1},{2},{3},{4}", 
                      reader.GetInt32(0),
                      reader.GetString(1),
                      reader.GetString(2),
                      reader.GetString(3),
                      reader.GetString(4);
  }
}


Now, if you're running from ASP.Net instead of 'a console application', you'll need to replace

C:\Some Path

With a 'mapped path', ie a valid path on your web site. This http://www.dotnetperls.com/mappath[^] may be a reference for how to start

There are also other tools that might be useful - FileHelpers Library[^] for instance
 
Share this answer
 
v3

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