.net数据绑定控件中的数据导出到Excel
在做審計局內部系統時,數據需要導出到Excel,在網上找了些代碼,成功的完成了此功能
在html代碼第一行中添加EnableEventValidation="false"
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="JinduDetail.aspx.cs" Inherits="JinduDetail" %>
?
在aspx.cs文件中添加以下代碼:
// 綁定gridview
??? /**/
??? /// <summary>
??? /// 重載VerifyRenderingInServerForm方法
??? /// 確認在運行時為指定的 ASP.NET 服務器控件呈現 HtmlForm 控件。
??? /// </summary>
??? /// <param name="control">ASP.NET 服務器控件,它必須位于 HtmlForm 控件中</param>
??? public override void VerifyRenderingInServerForm(Control control)
??? {
??????? //base.VerifyRenderingInServerForm(control);
??? }
導出按鈕需要的代碼:
?protected void btnXls_Click(object sender, ImageClickEventArgs e)
??? {
??????? Response.Clear();
??????? Response.Buffer = true;
??????? Response.Charset = "GB2312";
??????? Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
??????? // 經測試如果設置為 GetEncoding("GB2312"),導出的文件將會出現亂碼。
??????? Response.ContentEncoding = System.Text.Encoding.UTF7;
??????? //設置輸出文件類型為excel文件。
??????? Response.ContentType = "application/ms-excel";
??????? System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
??????? System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
??????? this.GridView2.RenderControl(oHtmlTextWriter);//注:GridView也可以換成其它數據綁定控件
??????? Response.Output.Write(oStringWriter.ToString());
??????? Response.Flush();
??????? Response.End();
??? }
轉載于:https://www.cnblogs.com/sea520168/archive/2008/09/03/1282922.html
總結
以上是生活随笔為你收集整理的.net数据绑定控件中的数据导出到Excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓自定义时间选择器_微信小程序拾色器(
- 下一篇: idea 无法打开项目_Premiere