Repeater控件的使用 (原样导出和动态显示/隐藏Repeater中的列)
一、Repeater数据原样导出
DataTable dt = ViewState["DtDatat"] as DataTable; //Repeater绑定的数据源
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
DisplayDetailCol(false);
//使用流方式导出Excel
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "gb2312";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + Context.Server.UrlPathEncode("excel名称.xls")); //解决中文乱码问题
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Repeater1.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
二、 动态显示/隐藏Repeater中的列
foreach (RepeaterItem item in this.Repeater1.Controls)
{
if (item.ItemType == ListItemType.Header)
{
item.FindControl("panelDetail").Visible = b;
}
if (item.ItemType == ListItemType.Item)
{
item.FindControl("lbtnDetail").Visible = b;
}
}
三、下载的excel显示成html格式
需要将<table>标签添加到HeaderTemplate和FooterTemplate中
四、导出的Excel数据为空或则数据都缩到一列
不要设置列的宽度为百分比