Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to generate csv file from server in matrix form from the tables like table1(A,B,C) AND table(D,E,F) which are created in sql 2005.
It should be created like

DATE A B C
D
E
F

date column is selected by csv generator.
Posted
Updated 10-Apr-12 23:09pm
v7
Comments
[no name] 27-Mar-12 9:03am    
Is there a question mixed in there somewhere?
Herman<T>.Instance 27-Mar-12 16:49pm    
how you succeed doing that?

Line by line!

Read records/rows from table then write the data out to file
write row1 - col1,col2,.....,coln
write row2 - col1,col2,.....,coln

It is a very simple process what do you not understand?
What have you tried?
Where is your problem?
 
Share this answer
 
protected void Button2_Click(object sender, EventArgs e)
{
try
{
con.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select * from Attendence", con);
da.Fill(dt);
ExportToText(dt, "Attendence");

}
catch (SqlException ex)
{
}
finally
{
con.Close();
}
}
protected void ExportToText(DataTable dataTable, string fileName)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in dataTable.Columns)
{
context.Response.Write(column.ColumnName + ",");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in dataTable.Rows)
{
for (int i = 0; i < dataTable.Columns.Count; i++)
{
context.Response.Write(row[i].ToString() + ",");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
context.Response.End();
}
 
Share this answer
 

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