Exporting Any Data Into Excel File Using Console Application.

It is easy to imporrt the data from a gridview to an excel sheet. But when the data comes from a database or any datasource it becomes difficult for us to import them into the excel sheet. I have tried the following code in Console application to export the data from the database. Its not too difficult to understand and utiilize the code.

For importing the data from the database we have to keep the following things in our mind.

1. What is the data needed to be exported to excel sheet.

2. What is the current file structure to save the excel sheet / create the excel sheet.

3. What should be the format of the data.

PROGRAM TO EXPORT DATA INTO EXCEL FORMAT:

static void Main(string[] args)
        {
               SqlConnection myConnection = null;
               SqlDataReader rdr = null;
               try
                     {
                            myConnection = new SqlConnection(connection string);  //The connectionstring will be assigned here.
                            myConnection.Open();
                            SqlCommand cmd = new SqlCommand(storedprocName, myConnection);//Stored procedure name is assigned                                                                                                                                                                           //here
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.Add(Parameters);
                            rdr = cmd.ExecuteReader();

                              while (rdr.Read())
                                       {
                                             program.CreateXLSFile(rdr, fileName); //Here the program is the class name that calls the CreateXLSFile().
                                       }
                     }
               catch (Exception)
                           {
                                  throw;
                           }
               finally
                           {
                                  if (myConnection != null)
                                  myConnection.Close();
                            }
          }

The above code calls the function CreateXLSFile() to save the file into the excel format. The codes required for the file to be created is as follows:
CODE FOR THE FILE TO SAVE IN THE XLS FORMAT:

public void CreateXLSFile(SqlDataReader dt, string strFileName)
       {

           #region Export Grid to XLS

           string date = string.Format("{0:dd-MM-yyyy}", DateTime.Now);
           string path = strFileName + "_" + date + ".xls";
           if (!File.Exists(path))
           {
               // Create a file to write to.
               Directory.SetCurrentDirectory("C:\\Users\\sisirp\\Desktop\\Console Application");// Changed the default location of the storage of the file.
               StreamWriter sw = File.AppendText(Path.Combine(strFileName + "_" + date + ".xls")); //Append the file name with the file path.
               //StreamWriter sw = File.CreateText(path);

               while (dt.Read())
               {
                   for (int i = 0; i < dt.FieldCount; i++)
                   {
                       sw.AutoFlush = true;
                       sw.Write(dt[i].ToString() + "\t");
                   }
                   sw.Write("\n");
               }
           }

           else
           {
               //StreamWriter sw = File.CreateText(path);
               Directory.SetCurrentDirectory("C:\\Users\\sisirp\\Desktop\\Console Application");// Changed the default location of the storage of the file.
               StreamWriter sw = File.AppendText(Path.Combine(strFileName + "_" + date + ".xls")); //Append the file name to the file path.
               while (dt.Read())
               {
                   for (int i = 0; i < dt.FieldCount; i++)
                   {
                       sw.AutoFlush = true;
                       sw.Write(dt[i].ToString() + "\t"); //Add the data to the file one by one row wise.
                   }
                   sw.Write("\n");
               }
           }

           #endregion

       }

This code works fine in the console application.  This is the simplest code that I found to be implemented in the console application. This code does not take more dll files and simple in nature.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!