private void ToExcel(System.Web.UI.WebControls.Repeater ctl, IList <EyouSoft.Model.StatisticStructure.EarningsTypeStatistic> PerAreaList) { ctl.Visible = true; ctl.DataSource = PerAreaList; ctl.DataBind(); HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=ProAreaStaticFile.xls"); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/ms-excel"; ctl.Page.EnableViewState = false; StringWriter sw = new StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw); ctl.RenderControl(hw); ctl.Visible = false; HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); }
/// <summary> /// ����Repeater��������reportname��excel���� /// </summary> /// <param name="reportname">�������ִ�xls��</param> /// <param name="rp">Repeater�ؼ�</param> public static void ExportExcelReport(string reportname,Repeater rp) { System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.UTF8; System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(reportname)); System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel"; rp.RenderControl(new HtmlTextWriter(System.Web.HttpContext.Current.Response.Output)); System.Web.HttpContext.Current.Response.End(); }
public void rpExportExcel(ref System.Web.UI.WebControls.Repeater rp, string strFileName, String FileType) { // DataTable dt = (DataTable)this.Session["GridToExcel"]; // if (dt==null) return; strFileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8); //System.IO.StringWriter sw = new System.IO.StringWriter(); //System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw); //rp.RenderControl(hw); //System.Web.HttpContext.Current.Response.Clear(); //System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; //System.Web.HttpContext.Current.Response.Charset = ""; //rp.Page.EnableViewState = false; //System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls"); //System.Web.HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=GB2312\"><title>Copyright by SDU</title></head><body><center>"); //System.Web.HttpContext.Current.Response.Write(sw.ToString()); //System.Web.HttpContext.Current.Response.Write("</center></body></html>"); //System.Web.HttpContext.Current.Response.End(); System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.BufferOutput = true; //设定输出字符集 System.Web.HttpContext.Current.Response.Charset = "GB2312"; System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8)); //设置输出流HttpMiME类型(导出文件格式) System.Web.HttpContext.Current.Response.ContentType = FileType; //关闭ViewState rp.Page.EnableViewState = false; System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true); System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo); HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter); //rpt_pro为repeater控件的ID //数据源要有边框,否则导出数据也无边框 rp.RenderControl(textWriter); //把HTML写回游览器 System.Web.HttpContext.Current.Response.Write(stringWriter.ToString()); System.Web.HttpContext.Current.Response.End(); System.Web.HttpContext.Current.Response.Flush(); }
/// </summary> /// 导出Excel文件,自动返回可下载的文件流 /// </summary> public static void DataTable1Excel(Repeater repeater, System.Data.DataTable dtData) { HttpContext curContext = HttpContext.Current; StringWriter strWriter = null; HtmlTextWriter htmlWriter = null; if (dtData != null) { curContext.Response.AddHeader("content-disposition", "attachment;filename=" + "用户统计表-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); curContext.Response.Charset = "utf-8"; strWriter = new StringWriter(); htmlWriter = new HtmlTextWriter(strWriter); repeater.DataSource = dtData; repeater.DataBind(); repeater.RenderControl(htmlWriter); curContext.Response.Write(strWriter.ToString()); curContext.Response.End(); } }
public static String getHTML(Repeater r) { StringWriter sr = new StringWriter(); HtmlTextWriter writer = new HtmlTextWriter(sr); r.RenderControl(writer); writer.Dispose(); return sr.ToString(); }