narasiman rao

narasiman rao

  • NA
  • 519
  • 771.7k

How to save database data into different excel

Aug 14 2016 2:25 AM
In my table record as follows


ASSociateID Project ID ProjectName Area ShiftType Shiftdate

2456 1 A BLR IN 28 jun 2016
3424 2 B BLR IN 30 Jun 2016
3422 3 C BLR IN 1 Aug 2016
3342 4 D BLR IN 2 Aug 2016

5534 1 A KOC OUT 29 Jun 2016
3353 2 B KOC OUT 30 jun 2016
4343 3 C KOC OUT 1 Aug 2016
4535 4 D KOC OUT 2 Aug 2016


my console application code as follows

Displaying above database data into excel using console application

string connectionstring = "Server=(local);initial catalog=OneC;Trusted_Connection=True";
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand command= new SqlCommand();
SqlDataReader dr;
DataSet ds= new DataSet();
command.CommandText = "Select * from Employee";
command.CommandType = CommandType.Text;
command.Connection = con;
con.Open();
dr= cmd.ExecuteReader();
if (dr.HasRows)
{

using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\Details\Excel.xls"))
{
while (dr.Read())
{
for (int i = 0; i < dr.FieldCount; i++)
{
sw.Autoflush = true;
sw.write(dr[i].Tostring() +"\t");
}
sw,writeline("\n");
}
}
con.Close();
}
when i execute the above code in my sysem in c folder in one excel both IN and OUTdata are saved
in same excel.
 

i want the above data to be saved ShiftType (IN) to be saved in one excel

and i want the above data to be saved shiftType (Out) to be saved in another excel

For that in my above code changes i have to do to save IN and OUT in different excel 

Answers (1)