1. Export Excel tables from the database
Define a GridView1
protected void Button1_Click (object sender, EventArgs e)//pour out data
{
if (GridView1.Rows.Count = = 0)
{
Return
}
Else
{
gridview1.allowpaging = false;//data paging is canceled before all data can be exported
Bind ();
Export ("Application/ms-excel", "Filename.xls", GridView1);
gridview1.allowpaging = true;//data is bound before rebind
Bind ();
}
public void Export (string fileType, String fileName, GridView gd)//Data export function
{
Response.Clear ();
Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding ("utf-7");
HttpContext.Current.Response.ContentType = FileType;
HttpContext.Current.Response.AppendHeader ("Content-disposition", "attachment;filename=\" "+ System.Web.HttpUtility.UrlEncode (FileName, System.Text.Encoding.UTF7));
Gd. Page.enableviewstate = false;
System.IO.StringWriter tw = new StringWriter ();
HtmlTextWriter HW = new HtmlTextWriter (TW);
Gd. RenderControl (HW);
Response.Output.Write (TW. ToString ());
Response.Flush ();
Response.End ();
}
If the following method is not the error type "GridView" control "GridView1" must be placed in the form tag with Runat=server
public override void Verifyrenderinginserverform (Control control)
{
}
2. Import from Excel into the database
Import and export Excel tables from SQL Server 2005