protected void btnExport_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add();
dt.Columns.Add();
dt.Rows.Add(TextBox1.Text, TextBox2.Text);
ExportDataTable(dt);
}
public void ExportDataTable(DataTable dt)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=e1.xls");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
string sTab = "";
foreach (DataColumn dc in dt.Columns)
{
HttpContext.Current.Response.Write(sTab + dc.ColumnName);
sTab = "\t";
}
HttpContext.Current.Response.Write("\n");
int i;
foreach (DataRow dr in dt.Rows)
{
sTab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
HttpContext.Current.Response.Write(sTab + dr[i].ToString());
sTab = "\t";
}
HttpContext.Current.Response.Write("\n");
}
HttpContext.Current.Response.End();
}