Asp.net2.0 the GridView to an Excel file

Source: Internet
Author: User

The following code implementation exports the GridView to an Excel file.

It is worth noting that the Verifyrenderinginserverform overload method:

Description of the Verifyrenderinginserverform method on MSDN:

A control that must be in the <form runat=server> tag can call this method before rendering to display an error message when the control is placed outside the tag. A control that sends back or relies on a registered script block should call this method in an override of the Control.render method. Pages that render a server form element can override this method to throw an exception under different conditions.

server controls that are postback or that use client script are not included in the HtmlForm server control (<form runat= "Server" >) tags, and they will not work correctly. These controls can call this method at render time to provide a clear error message when they are not included in the HtmlForm control.

When you develop a custom server control, you typically call this method when you override the Render method for any type of input token. This is especially important when the input control invokes GetPostBackEventReference or emits client script. The composite server control does not need to make this call.

Without this method, the program will complain.

C # code

<%...@ Page language= "C #" enableeventvalidation= "false"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<script runat= "Server" ....
ICollection CreateDataSource ()
... {
System.Data.DataTable dt = new System.Data.DataTable ();
System.Data.DataRow Dr;
Dt. Columns.Add (New System.Data.DataColumn ("id", typeof (Int32));
Dt. Columns.Add (New System.Data.DataColumn ("Pkid", typeof (String));
Dt. Columns.Add (New System.Data.DataColumn ("Title", typeof (String));
for (int i = 0; i < 6; i++)
... {
Dr = dt. NewRow ();
Dr[0] = i;
DR[1] = "123456789123456789123456789";
DR[2] = "<a href= ' http://dotnet.aspx.cc/' > Welcome" Mengxian The Wonderful World "</a>";
Dt. Rows.Add (DR);
}
System.Data.DataView dv = new System.Data.DataView (DT);
return DV;
}

protected void Page_Load (object sender, EventArgs e)
... {
if (! IsPostBack)
... {
Gridview1.borderwidth = Unit.pixel (2);
Gridview1.bordercolor = System.Drawing.Color.DarkOrange;
Gridview1.datasource = CreateDataSource ();
Gridview1.databind ();
}
}

protected void Button1_Click (object sender, System.EventArgs e)
... {
Response.Clear ();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.appendheader ("Content-disposition", "Attachment;filename=filename.xls");
If set to GetEncoding ("GB2312"), the exported file will appear garbled!!!
response.contentencoding = System.Text.Encoding.UTF7;
Response.ContentType = "Application/ms-excel";//Set Output file type to excel file.
System.IO.StringWriter ostringwriter = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter ohtmltextwriter = new System.Web.UI.HtmlTextWriter (ostringwriter);
This. Gridview1.rendercontrol (Ohtmltextwriter);
Response.Output.Write (Ostringwriter.tostring ());
Response.Flush ();
Response.End ();

}
public override void Verifyrenderinginserverform (Control control)
... { }
protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
... {
if (E.row.rowtype = = Datacontrolrowtype.datarow)
... {
E.ROW.CELLS[1]. Attributes.Add ("Style", "vnd.ms-excel.numberformat:@;");
}
}
</script>

<title> Export the GridView to an Excel file </title>

<body>
<form id= "Form1" runat= "Server" >
<asp:gridview id= "GridView1" runat= "Server" onrowdatabound= "GridView1_RowDataBound"
Autogeneratecolumns= "false" >
<Columns>
<asp:boundfield headertext= "ordinal" datafield= "id"/>
<asp:boundfield headertext= "ID number" datafield= "Pkid"/>
<asp:boundfield headertext= "url" datafield= "Title" readonly= "true" htmlencode= "false"/>
</Columns>
</asp:GridView>
<asp:literal id= "hiddenout" runat= "Server"/>
<asp:button id= "Button1" runat= "server" text= "Export" onclick= "Button1_Click"/>
</form>
</body>



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.