一、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數據為空或則數據都縮到一列
不要設置列的寬度為百分比
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持幫客之家!