如何使用C#将控制台输出数据保存到控制台应用程序中 [英] How to save console ouput data into console application using C#

查看:167
本文介绍了如何使用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.CommandType = CommandType.Text;

cmd。 Connection = sqlConnection;

sqlConnection.Open();

reader = cmd.ExecuteReader();

if(reader.HasRows)

{

while(reader.Read())

{

Console.WriteLine(ID | Name | Address |城市|电话\ n {0} | {1} | {2} | {3} | {4},reader.GetInt32(0),

reader.GetString(1), reader.GetString(2),reader.GetString(3),reader.GetString(4));

}

}

sqlConnection。关闭();





当我执行时在命令提示符输出中使用控制台应用程序中的上述代码将显示如下





ID名称地址城市电话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想要将上面的输出保存在我的excel中下载文件夹。



为什么我在asp.net中使用c#在控制台应用程序中做什么



我尝试过:



我的控制台应用程序代码如下



string connectionstring =Server =(local); initial catalog = Te st; 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。文字;

cmd.Connection = sqlConnection;

sqlConnection.Open();

reader = cmd.ExecuteReader();

if(reader.HasRows)

{

while(reader.Read())

{

Console.WriteLine(ID |名称|地址|城市|电话\ n {0} | {1} | {2} | {3} | {4},reader.GetInt32(0),

reader.GetString(1),reader.GetString(2),reader.GetString(3),reader.GetString(4)); < br $>
}

}

sqlConnection.Close();





当我在控制台应用程序中执行上述代码时,命令提示输出将显示如下





ID名称地址城市电话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想要以上输出在我的下载文件夹中保存在excel中。



我该怎么办? n asp.net在控制台应用程序中使用c#

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

推荐答案

我不太确定asp.net与控制台应用程序有什么关系...如果你想将你的输出写入一个可以被excel读取的文件,然后,类似这样的东西



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

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);
  }
}





现在,如果您从ASP.Net而不是'控制台应用程序运行',你需要更换



C:\ Some Path



带'映射path',即您网站上的有效路径。 http://www.dotnetperls.com/mappath [ ^ ]可能是如何开始的参考



还有其他工具可以可能很有用 - FileHelpers Library [ ^ ]例如



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


这篇关于如何使用C#将控制台输出数据保存到控制台应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆