G
Guest
I used the code below to export a table in a dataset to Excel.
It can export only on table at a time.
Can export more than on table in a dataset to Excel
public static void Convert(System.Data.DataSet ds, int TableIndex,
System.Web.HttpResponse response)
{
if (TableIndex > ds.Tables.Count - 1)
{
Convert(ds, response, ds.Tables[0].TableName);
}
response.Clear();
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new
System.Web.UI.HtmlTextWriter(stringWrite);
System.Web.UI.WebControls.DataGrid dg = new
System.Web.UI.WebControls.DataGrid();
dg.DataSource = ds.Tables[TableIndex];
dg.DataBind();
dg.RenderControl(htmlWrite);
response.Write(stringWrite.ToString());
response.End();
}
It can export only on table at a time.
Can export more than on table in a dataset to Excel
public static void Convert(System.Data.DataSet ds, int TableIndex,
System.Web.HttpResponse response)
{
if (TableIndex > ds.Tables.Count - 1)
{
Convert(ds, response, ds.Tables[0].TableName);
}
response.Clear();
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new
System.Web.UI.HtmlTextWriter(stringWrite);
System.Web.UI.WebControls.DataGrid dg = new
System.Web.UI.WebControls.DataGrid();
dg.DataSource = ds.Tables[TableIndex];
dg.DataBind();
dg.RenderControl(htmlWrite);
response.Write(stringWrite.ToString());
response.End();
}