private
void
DisableControls(Control gv)

...
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)

...{
if (gv.Controls[i].GetType() == typeof(LinkButton))

...{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))

...{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())

...{
DisableControls(gv.Controls[i]);
}
}
}

在web应用程序中,我们是不是很发愁打印问题,您是不是有过为了打印写Activex的经历,我们有没有想过,Word和Excel的打印功能能被我们利用起来呢?只要我们将我们将数据导出到Excel或者Word中,打印岂不是小case了么。下面就谈谈如何让GridView自己支持导出Excel和Word 。
首先增加了两个属性,用于指示是否支持Excel导出和Word导出
protected
override
void
OnRowCommand(GridViewCommandEventArgs e)

...
{
base.OnRowCommand(e);
if (e.CommandName == "ExportToExcel")

...{
string[] ss = UnExportedColumnNames.Split(',');
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
foreach (string s in ss)

...{
if (s != ",")

...{
list.Add(s);
}
}
ShowToolBar = false;
this.AllowSorting = false;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename=" + ExcelFileName + ".xls");
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
HttpContext.Current.Response.ContentType = "application/ms-excel";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
bool showCheckAll = ShowCheckAll;
this.ShowCheckAll = false;
this.AllowPaging = false;
OnBind();
DisableControls(this);
foreach (DataControlField c in this.Columns)

...{
if (list.Contains(c.HeaderText) && !string.IsNullOrEmpty(c.HeaderText))

...{
c.Visible = false;
}
}
this.RenderControl(htmlWrite);
string content = System.Text.RegularExpressions.Regex.Replace(stringWrite.ToString(), "(<a[^>]+>)|(</a>)", "");
HttpContext.Current.Response.Write(content);
HttpContext.Current.Response.End();
this.AllowPaging = true;
this.AllowSorting = true;
ShowToolBar = true;
this.ShowCheckAll = showCheckAll;
OnBind();
}
else if (e.CommandName == "ExportToWord")

...{
string[] ss = UnExportedColumnNames.Split(',');
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
foreach (string s in ss)

...{
if (s != ",")

...{
list.Add(s);
}
}
ShowToolBar = false;
this.AllowSorting = false;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename=" + ExcelFileName + ".doc");
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
HttpContext.Current.Response.ContentType = "application/ms-word";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
bool showCheckAll = ShowCheckAll;
this.ShowCheckAll = false;
this.AllowPaging = false;
OnBind();
DisableControls(this);
foreach (DataControlField c in this.Columns)

...{
if (list.Contains(c.HeaderText) && !string.IsNullOrEmpty(c.HeaderText))

...{
c.Visible = false;
}
}
this.RenderControl(htmlWrite);
string content = System.Text.RegularExpressions.Regex.Replace(stringWrite.ToString(), "(<a[^>]+>)|(</a>)", "");
HttpContext.Current.Response.Write(content);
HttpContext.Current.Response.End();
this.AllowPaging = true;
this.AllowSorting = true;
ShowToolBar = true;
ShowCheckAll = showCheckAll;
OnBind();
}
}

使用的时候,只要指定ShowExportExcel=True,ShowExportWord=True就自动出现导出Word和导出Excel的按钮了,点击自动会将GridView中的数据导出到Word或者Excel中了,如果原GridView是多页的,那也会自动将全部数据(而不是当前页的数据)导出,而且会剔除原来数据中的一些超级链接。使用起来相当简单,效果页非常好。
在web应用程序中,我们是不是很发愁打印问题,您是不是有过为了打印写Activex的经历,我们有没有想过,Word和Excel的打印功能能被我们利用起来呢?只要我们将我们将数据导出到Excel或者Word中,打印岂不是小case了么。下面就谈谈如何让GridView自己支持导出Excel和Word 。
首先增加了两个属性,用于指示是否支持Excel导出和Word导出
使用的时候,只要指定ShowExportExcel=True,ShowExportWord=True就自动出现导出Word和导出Excel的按钮了,点击自动会将GridView中的数据导出到Word或者Excel中了,如果原GridView是多页的,那也会自动将全部数据(而不是当前页的数据)导出,而且会剔除原来数据中的一些超级链接。使用起来相当简单,效果页非常好。