private void btnExportToExcel_Click(object sender, EventArgs e)
{
//Export to Excel code
try
{
string attachment = "attachment; filename=Contact.xls";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
string sTab = "";
foreach (DataColumn dc in resultTable.Columns)
{
HttpContext.Current.Response.Write(sTab + dc.ColumnName);
sTab = "\t";
}
HttpContext.Current.Response.Write("\n");
int i;
foreach (DataRow dr in resultTable.Rows)
{
sTab = "";
for (i = 0; i < resultTable.Columns.Count; i++)
{
HttpContext.Current.Response.Write(sTab + dr[i].ToString());
sTab = "\t";
}
HttpContext.Current.Response.Write("\n");
}
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
ErrorLabel = new Label();
ErrorLabel.Text = ErrorMessage;
this.Controls.Add(ErrorLabel);
}
}
{
//Export to Excel code
try
{
string attachment = "attachment; filename=Contact.xls";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
string sTab = "";
foreach (DataColumn dc in resultTable.Columns)
{
HttpContext.Current.Response.Write(sTab + dc.ColumnName);
sTab = "\t";
}
HttpContext.Current.Response.Write("\n");
int i;
foreach (DataRow dr in resultTable.Rows)
{
sTab = "";
for (i = 0; i < resultTable.Columns.Count; i++)
{
HttpContext.Current.Response.Write(sTab + dr[i].ToString());
sTab = "\t";
}
HttpContext.Current.Response.Write("\n");
}
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
ErrorLabel = new Label();
ErrorLabel.Text = ErrorMessage;
this.Controls.Add(ErrorLabel);
}
}
No comments:
Post a Comment