You must Sign In to post a response.
  • Category: ASP.NET

    How to save console output file in to excel

    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
  • #767545
    Hi,
    Add the reference "Microsoft Excel 11.0 Object Library" or "Microsoft Excel 5.0 Object Library" to your consolde application.
    Use ExcelFileWriterClass.WriteDateToExcel() method to write your data to excel.
    Or refer multiple methods to write data to excel file using c# over here:
    http://techbrij.com/export-excel-xls-xlsx-asp-net-npoi-epplus

  • #767565
    Hi,

    You just create one object like gridview/datagrid/html table etc.. and make it as visible false, then bind your data to that control and export using that object, this is the way you can achieve it easily.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments