Exemple #1
0
        /**//// <summary>
        /// 将Web控件或页面信息导出(不带文件名参数)
        /// </summary>
        /// <param name="source">控件实例</param>
        /// <param name="DocumentType">导出类型:Excel或Word</param>
        public void ExportControl(System.Web.UI.Control source, string DocumentType)
        {
            //设置Http的头信息,编码格式
            if (DocumentType == "Excel")
            {
                //Excel
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下载文件.xls", System.Text.Encoding.UTF8));
                HttpContext.Current.Response.ContentType = "application/ms-excel";
            }

            else if (DocumentType == "Word")
            {
                //Word
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下载文件.doc", System.Text.Encoding.UTF8));
                HttpContext.Current.Response.ContentType = "application/ms-word";
            }

            HttpContext.Current.Response.Charset         = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

            //关闭控件的视图状态
            source.Page.EnableViewState = false;

            //初始化HtmlWriter
            System.IO.StringWriter       writer     = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
            source.RenderControl(htmlWriter);

            //输出
            HttpContext.Current.Response.Write(writer.ToString());
        }

        /**//// <summary>
        /// 将Web控件或页面信息导出(带文件名参数)
        /// </summary>
        /// <param name="source">控件实例</param>
        /// <param name="DocumentType">导出类型:Excel或Word</param>
        /// <param name="filename">保存文件名</param>
        public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename)
        {
            //设置Http的头信息,编码格式
            if (DocumentType == "Excel")
            {
                //Excel
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
                HttpContext.Current.Response.ContentType = "application/ms-excel";
            }

            else if (DocumentType == "Word")
            {
                //Word
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".doc", System.Text.Encoding.UTF8));
                HttpContext.Current.Response.ContentType = "application/ms-word";
            }

            HttpContext.Current.Response.Charset         = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

            //关闭控件的视图状态
            source.Page.EnableViewState = false;

            //初始化HtmlWriter
            System.IO.StringWriter       writer     = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
            source.RenderControl(htmlWriter);

            //输出
            HttpContext.Current.Response.Write(writer.ToString());
        }
Exemple #2
0
        /// <summary>
        /// 针对WEB控件导出word
        /// </summary>
        /// <param name="control"></param>
        /// <param name="ExcelFileName"></param>
        public static void ExportToWord(System.Web.UI.Control control, string WordFileName)
        {
            HtmlForm form = new HtmlForm();

            form.Controls.Add(control);

            HttpContext context = HttpContext.Current;

            context.Response.Clear();
            context.Response.Buffer  = true;
            context.Response.Charset = "UTF8";

            context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(WordFileName, System.Text.Encoding.UTF8) + ".doc");

            context.Response.ContentEncoding = System.Text.Encoding.UTF8; //设置输出流为简体中文
            context.Response.ContentType     = "application/ms-Word";     //设置输出文件类型为excel文件。

            System.Globalization.CultureInfo myCItrad        = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter           oStringWriter   = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter     oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            control.RenderControl(oHtmlTextWriter);
            string charset = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\">";

            context.Response.Write(charset + oStringWriter.ToString());
            context.Response.End();
        }
Exemple #3
0
 public static string RenderControlToHtml(System.Web.UI.Control ControlToRender)
 {
     System.Text.StringBuilder    sb         = new System.Text.StringBuilder();
     System.IO.StringWriter       stWriter   = new System.IO.StringWriter(sb);
     System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(stWriter);
     ControlToRender.RenderControl(htmlWriter);
     return(sb.ToString());
 }
Exemple #4
0
        public static string RenderControl(System.Web.UI.Control control)
        {
            StringBuilder html = new StringBuilder();

            System.IO.StringWriter       stringWriter = new System.IO.StringWriter(html);
            System.Web.UI.HtmlTextWriter htmlWriter   = new System.Web.UI.HtmlTextWriter(stringWriter);
            control.RenderControl(htmlWriter);
            return(html.ToString());
        }
Exemple #5
0
        /// <summary>
        /// 获取 服务器控件生成的html
        /// </summary>
        /// <param name="pCtrl"></param>
        /// <returns></returns>
        public static string GetControlHtml(System.Web.UI.Control pCtrl)
        {
            string strHtml = string.Empty;

            using (System.IO.StringWriter sw = new System.IO.StringWriter())
            {
                using (System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw))
                {
                    pCtrl.RenderControl(htw);
                    strHtml = sw.ToString();
                }
            }



            return(strHtml);
        }
Exemple #6
0
        /// <summary>
        /// 将控件内容,导出EXCEL
        /// </summary>
        /// <param name="ctr"></param>
        public void ExportToExcel(System.Web.UI.Control ctr)
        {
            string style = @"<style> .text { mso-number-format:\@; } </script> ";

            Response.ClearContent();
            Response.Write("<meta   http-equiv=Content-Type   content=text/html;charset=utf-8>");
            Response.AddHeader("content-disposition", "attachment; filename=filename.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType     = "application/excel";
            System.IO.StringWriter       sw  = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            ctr.RenderControl(htw);

            // Style is added dynamically
            Response.Write(style);
            Response.Write(sw.ToString());
            Response.End();
        }
        private string getHtmlFromControlRender(System.Web.UI.Control control)
        {
            StringBuilder sb;

            System.IO.StringWriter       sw;
            System.Web.UI.HtmlTextWriter writer;

            sb = new StringBuilder();
            using (sw = new System.IO.StringWriter(sb))
            {
                using (writer = new System.Web.UI.HtmlTextWriter(sw))
                {
                    control.RenderControl(writer);
                }
            }

            return(sb.ToString());
        }
Exemple #8
0
        /// <summary>
        /// 将 DataGrid、DataList 等数据控件中的数据导出为 Execl 文档并下载
        /// </summary>
        /// <param name="ctl">控件</param>
        /// <param name="FileName">指定保存的文件名</param>
        public static void DataControlToExcelAndDownload(System.Web.UI.Control ctl, string FileName)
        {
            if (String.IsNullOrEmpty(FileName))
            {
                FileName = "Excel.xls";
            }

            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            HttpContext.Current.Response.Charset         = System.Text.Encoding.Default.EncodingName;
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
            HttpContext.Current.Response.ContentType     = "application/ms-excel";

            ctl.Page.EnableViewState = false;
            System.IO.StringWriter       sw  = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            ctl.RenderControl(htw);

            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
Exemple #9
0
        /// <summary>
        /// 将WebControl数据列表导出至Excel
        /// </summary>
        /// <param name="objControl"></param>
        /// <param name="strFileName"></param>
        public static void Export2Excel(System.Web.UI.Control objControl, string strFileName)
        {
            strFileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);

            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer  = true;
            System.Web.HttpContext.Current.Response.Charset = "GB2312";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + strFileName + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
            System.Web.HttpContext.Current.Response.ContentType     = "application/ms-excel";

            System.Globalization.CultureInfo myCItrad        = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter           oStringWriter   = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter     oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            objControl.RenderControl(oHtmlTextWriter);

            objControl = null;
            System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
            System.Web.HttpContext.Current.Response.Buffer = false;
            System.Web.HttpContext.Current.Response.End();
        }