Analysis on the use of the Repeater control (export as is and dynamically display/hide columns in Repeater), analysis of repeater
1. Original Repeater data export
DataTable dt = ViewState ["DtDatat"] as DataTable; // data source bound to Repeater this. repeater1.DataSource = dt; this. repeater1.DataBind (); DisplayDetailCol (false); // use stream to export 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"); // solve the Chinese garbled problem StringWriter sw = new StringWriter (); HtmlTextWriter htw = new HtmlTextWriter (sw); Digest (htw); HttpContext. current. response. write (sw. toString (); HttpContext. current. response. end ();
Ii. dynamically display/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 file is displayed in html format.
You need to add the <table> label to HeaderTemplate and FooterTemplate.
4. If the exported Excel Data is empty or the data is reduced to one column
Do not set the column width to percentage
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!