Import and export Excel tables from SQL Server 2005

Source: Internet
Author: User
Tags httpcontext

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.