Analysis on the use of Repeater control of export as it is and dynamically display and hide columns in Repeater
- 2021-08-31 07:41:22
- OfStack
1. Repeater data is exported as is
DataTable dt = ViewState["DtDatat"] as DataTable; //Repeater Bound data source
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
DisplayDetailCol(false);
// Export using stream mode 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 Name .xls")); // Solve the problem of Chinese garbled codes
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Repeater1.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
2. Dynamically show/hide columns in 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;
}
}
3. The downloaded excel is displayed in html format
Need to put < table > Tag added to HeaderTemplate and FooterTemplate
4. Exported Excel data is null or shrink to 1 column
Do not set the width of a column to a percentage