C#(asp.net)實現數據導出Excel表詳細代碼
C#(asp.net)實現數據導出Excel表詳細代碼2010-6-25 22:57:32 來源:25億CMS系統
本例介紹的用C#(asp.net)實現數據導出Excel詳細代碼的方法,會輸出標準的Excel格式文件,非常穩定,不會死鎖Excel進程,支持中文文件名,支持表頭導出,支持大多數數據庫導入。
C#(asp.net)數據導出Excel實現算法:利用Excel組件將DataGrid控件內容生成Excel臨時文件,并存放在服務器上,然后用Response方法將生成的Excel文件下載到客戶端然后再將生成的臨時文件刪除。
25億CMS系統v2.9實現此功能,大家可以25億CMS系統后臺查看并研究。
C#(asp.net)數據導出Excel具體步驟:
1,在項目中引用Excel組件(Interop.Excel.dll),如需此組件,可以到技術社區(http://bbs.25yi.com)索取,25億官方技術部會提供。
2,aspx頁面代碼:
3,cs代碼:
public void DataSourse()
{
//GridView數據綁定
GridView1.DataSource = bllintable.GetModds(9, Remark, this.pbComment.CurrentPage - 1, this.pbComment.PageSize, Language);
GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hell = new HtmlTextWriter(tw);
GridView1.AllowPaging = false;
DataSourse();
GridView1.RenderControl(hell);
Response.Write(tw.ToString());
Response.End();
GridView1.AllowPaging = true;
DataSourse();
}
直接把上述代碼復制到頁面中,修改GridView綁定參數既可以運行,如有疑問,可以官方技術社區(http://bbs.25yi.com)提問。
作者: 網站設計@ CMS系統
原載: 25億企業網站管理系統
版權所有。轉載時必須以鏈接形式注明作者和原始出處及本聲明。
posted on 2011-01-10 10:47 aiaiwoo 閱讀(650) 評論(0) 編輯 收藏 所屬分類: ASP.NET