C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法
本文實(shí)例展示了C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法,是非常實(shí)用的一個(gè)功能,分享給大家供大家參考。具體如下:
主要功能代碼如下:
// 把GridView輸出到Excel文件 private void ExportExcel(GridView gridView, string title, string title2, string fileName) { int nHideCols = 0; //如果不想輸出出某列,將Visible設(shè)為false即可 for (int i = 0; i < gridView.Columns.Count; i++) { if (gridView.Columns[i].HeaderText == "設(shè)備狀態(tài)") { gridView.Columns[i].Visible = false; gridView.Columns[i].ControlStyle.Width = 0; nHideCols = 1; break; } } //設(shè)定顯示字符集 Response.Charset = "utf-8"; //設(shè)定內(nèi)容字符集 Response.ContentEncoding = Encoding.GetEncoding("utf-8"); //設(shè)定文件名 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace('+', '_').Replace('-', '_')); //設(shè)定文件類(lèi)型 也可以是application/ms-word,也可以是text/html(字符集設(shè)為gb2312) Response.ContentType = "application/ms-excel"; this.EnableViewState = false; using (StringWriter tw = new StringWriter()) { using (HtmlTextWriter hell = new HtmlTextWriter(tw)) { gridView.AllowPaging = false; gridView.RenderControl(hell); string s = tw.ToString(); s = s.Replace("\r\n", ""); int index = s.IndexOf("<tr"); //可以自定義Excel文件的標(biāo)題 string head = "<tr><td colspan=\"" + (gridView.Columns.Count - nHideCols).ToString() + "\" style=\"text-align: center; height: 42px; font-size: 24px; font-weight: bolder; color: #000000;\">" + title + "</td></tr>" + "<tr><td colspan=\"" + (gridView.Columns.Count - nHideCols).ToString() + "\" style=\"text-align: center; height: 24px; font-size: 12px; color: #000000;\">" + title2 + "</td></tr>"; //使用Index來(lái)判斷是否存在數(shù)據(jù),當(dāng)然也可以用gridView.Rows.Count來(lái)判斷 if (index != -1) { //有數(shù)據(jù)的 s = s.Insert(index, head); } else { //沒(méi)有數(shù)據(jù)的時(shí)候 s = "<table cellspacing=\"0\" cellpadding=\"3\" rules=\"rows\" border=\"1\" id=\"" + gridView.ID + "\" style=\"background-color:White;border-color:#E7E7FF;border-width:1px;border-style:None;border-collapse:collapse;\">" + head + "</table>"; } Response.Write(s); Response.End(); } } } //同時(shí)vs2005,vs2003會(huì)報(bào)錯(cuò)“類(lèi)型“ExGridView”的控件“GridViewMaster”必須放在具有 runat=server 的窗體標(biāo)記內(nèi) //需要添加下面取消對(duì)GridViewMaster 控件驗(yàn)證的方法 public override void VerifyRenderingInServerForm(Control control) { if (!control.GetType().Equals(gridView.GetType())) { base.VerifyRenderingInServerForm(control); } }
本文實(shí)例代碼備有較為詳盡的注釋?zhuān)瑧?yīng)該不難理解。希望本文實(shí)例對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#中使用ArrayPool和MemoryPool實(shí)例
對(duì)資源的可復(fù)用是提升應(yīng)用程序性能的一個(gè)非常重要的手段,比如本篇要分享的 ArrayPool 和 MemoryPool,它們就有效的減少了內(nèi)存使用和對(duì)GC的壓力,從而提升應(yīng)用程序性能。感興趣的可以了解一下2021-05-05insert語(yǔ)句太長(zhǎng)用StringBuilder優(yōu)化一下
insert語(yǔ)句太長(zhǎng)用StringBuilder優(yōu)化一下,下面是示例代碼,需要的朋友可以研究研究2014-07-07C#中幾個(gè)未知的Visual Studio編碼技巧分享
用了多年的Visual Studio,今天才發(fā)現(xiàn)這個(gè)編碼技巧,真是慚愧,分享出來(lái),算是拋磚引玉吧,需要的朋友可以參考下2012-11-11Unity實(shí)現(xiàn)弧形移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)弧形移動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06C#使用Sleep(Int32)方法實(shí)現(xiàn)動(dòng)態(tài)顯示時(shí)間
這篇文章主要為大家詳細(xì)介紹了C#如何使用Sleep(Int32)方法實(shí)現(xiàn)動(dòng)態(tài)顯示時(shí)間,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2024-01-01