public override void VerifyRenderingInServerForm(Control control) //不写这个重载函数容易报错,错误内容为:类型"GridView"的控件"GridView1"必须放在具有 runat=server 的窗体标记内。
{
// Confirms that an HtmlForm control is rendered for
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=SampleInspectation.xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
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();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//需要操作的列则不能使用此方法,只需在长数字那一列用此方法如是第二列,则代码为:
e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");//这里是第二列
}