GridView leads out of Excel

Source: Internet
Author: User

1.aspx page needs to be added: enableeventvalidation= "false"

Example: <%@ page language= "C #" autoeventwireup= "true" enableeventvalidation= "false" codefile= " DefCheckDate.aspx.cs "
inherits= "Webadmin_customregister_shortoverflow_defcheckdate"%>

2. Post Code (example):

public void Btn_excelclick (object sender, EventArgs e)
{
if (Gdv_Sof.Rows.Count <= 0)
{
Clientscript.registerclientscriptblock (this. GetType (), "", "<script language= ' JavaScript ' >alert (' no numbers! ') </script> ");
Return
}
Response.clearcontent ();
Response.AddHeader ("Content-disposition", "attachment; Filename=checkdate.xls ");
Response.contenttype= "Application/excel";
Response.Charset = "Big5";
response.contentencoding = System.Text.Encoding.GetEncoding ("Big5");
StringWriter SW = new StringWriter ();
HtmlTextWriter HTW = new HtmlTextWriter (SW);
Gdv_sof.columns[0]. Visible = false;
GDV_SOF.COLUMNS[1]. Visible = false;
Gdv_Sof.HeaderRow.Controls.Clear ();
Gdv_sof.allowpaging = false;
Gdv_sof.allowsorting = false;
Bindgridview ();
Gdv_sof.rendercontrol (HTW);
Response.Write (SW);
Response.End ();
Gdv_sof.allowpaging = true;
Gdv_sof.allowsorting = true;
Bindgridview ();


}

This method must have Verifyrenderinginserverform
public override void Verifyrenderinginserverform (Control control)
{
}

3. The digital format is set up: For example, the page is 005, but the reason is 5, you need to add the following type vnd.ms-excel.numberformat:@ ");

Example:

protected void Gdv_sof_rowdatebound (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
E.ROW.CELLS[3]. Attributes.Add ("Style", "vnd.ms-excel.numberformat:@");
E.ROW.CELLS[4]. Attributes.Add ("Style", "vnd.ms-excel.numberformat:@");
}

}

GridView leads out of Excel

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.