Ejemplo n.º 1
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();
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 获得控件的HTML代码
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public string GetControlHTML(System.Web.UI.Control control)
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            control.RenderControl(hw);

            return sw.ToString();
        }
Ejemplo n.º 3
0
		private void RenderControlTest(System.Web.UI.Control toTest)
		{
			StringBuilder sb = new StringBuilder();
			StringWriter sw = new StringWriter(sb);
			System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
			toTest.RenderControl(htw);
			GHTSubTestAddResult(sb.ToString(), true);
		}
 public void ToExcel(System.Web.UI.Control ctl, String fileName)
 {
     Response.Clear();
     Response.Buffer = false;
     Response.Charset = "GB2312";
     HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
     Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
     Response.ContentType = "application/ms-excel";
     Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
     this.EnableViewState = false;
     System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
     HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
     ctl.RenderControl(oHtmlTextWriter);
     Response.Write(oStringWriter.ToString());
     Response.End();
 }
Ejemplo n.º 5
0
 public void ToExcel(System.Web.UI.WebControls.DataGrid DG,string FileName)
 {
     HttpContext.Current.Response.Charset = "utf-8";
     HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
     //Response.ContentType指定文件类型   可以为application/ms-excel   ||   application/ms-word   ||   application/ms-txt   ||   application/ms-html   ||   或其他浏览器可直接支持文档   
     HttpContext.Current.Response.ContentType = "application/ms-excel";
     HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
     //关闭   ViewState   
     DG.Page.EnableViewState = false;
     //将信息写入字符串   
     System.IO.StringWriter tw = new System.IO.StringWriter();
     //在WEB窗体页上写出一系列连续的HTML特定字符和文本。   
     System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
     //此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能   
     //获取control的HTML
     DG.RenderControl(hw);
     //   把HTML写回浏览器   
     HttpContext.Current.Response.Write(tw.ToString());
     HttpContext.Current.Response.End();
 }        
Ejemplo n.º 6
0
 /// <summary>
 /// 直接由GridView导出Excel
 /// </summary>
 /// <param name="ctl">控件(一般是GridView)</param>
 /// <param name="FileName">导出的文件名</param>
 /// <param name="removeIndexs">要移除的列的索引数组(因为有时我们并不希望把GridView中的所有列全部导出)</param>
 /// <param name="pages"></param>
 public void controlToExcel(System.Web.UI.WebControls.GridView ctl, string FileName, string[] removeIndexs, System.Web.UI.Page pages)
 {
     if (removeIndexs != null)
     {
         foreach (string index in removeIndexs)
         {
             ctl.Columns[int.Parse(index)].Visible = false;
         }
     }
     pages.Response.Charset = "UTF-8";
     pages.Response.ContentEncoding = System.Text.Encoding.UTF7;
     pages.Response.ContentType = "application/ms-excel";
     FileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);
     pages.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
     ctl.Page.EnableViewState = false;
     System.IO.StringWriter tw = new System.IO.StringWriter();
     System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
     ctl.RenderControl(hw);
     pages.Response.Write(tw.ToString());
     HttpContext.Current.ApplicationInstance.CompleteRequest();
 }
Ejemplo n.º 7
0
    public static string RenderToMessage(System.Web.UI.UserControl ctrControl)
    {
        System.Text.StringBuilder sbuBuilder;
        System.IO.StringWriter wriWriter;
        System.Web.UI.HtmlTextWriter htwWriter;

        sbuBuilder = new System.Text.StringBuilder();

        sbuBuilder.Append("<html>");
        sbuBuilder.Append("<body>");

        wriWriter = new System.IO.StringWriter(sbuBuilder);
        htwWriter = new System.Web.UI.HtmlTextWriter(wriWriter);
        ctrControl.RenderControl(htwWriter);

        htwWriter.Close();
        wriWriter.Close();

        sbuBuilder.Append("</body>");
        sbuBuilder.Append("</html>");

        return sbuBuilder.ToString();
    }
Ejemplo n.º 8
0
        private void ExportToExcel(System.Web.UI.WebControls.DataGrid dg)
        {
            //export to excel

            Debug.WriteLine("trying to export");

            try
            {
                Response.Clear();
                Response.Buffer= true;
                Response.ContentType = "application/vnd.ms-excel";
                Response.Charset = "";
                this.EnableViewState = false;

                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

                dg.RenderControl(oHtmlTextWriter);

                Response.Write(oStringWriter.ToString());

                Response.End();
            }
            catch(Exception e)
            {
                Debug.WriteLine("ERROR: "+e.ToString());
            }

            //export to excel

            /*
            Response.Clear();
            Response.Buffer= true;
            //Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            this.EnableViewState = false;

            System.IO.StringWriter oStringWriter1 = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter1 = new System.Web.UI.HtmlTextWriter(oStringWriter1);

            //System.IO.StringWriter oStringWriter2 = new System.IO.StringWriter();
            //System.Web.UI.HtmlTextWriter oHtmlTextWriter2 = new System.Web.UI.HtmlTextWriter(oStringWriter2);

            //this.ClearControls(dgTemperatureLoggerSites);
            //oStringWriter1.GetStringBuilder().Append("<meta name='Excel Workbook Frameset'><meta http-equiv=Content-Type content='text/html; charset=windows-1252'><meta name=ProgId content=Excel.Sheet><meta name=Generator content='Microsoft Excel 9'><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>NewSheet1</x:Name>");

            dgTemperatureLoggerSites.RenderControl(oHtmlTextWriter1);
            //oStringWriter1.GetStringBuilder().Append("</x:ExcelWorksheet><x:ExcelWorksheet><x:Name>NewSheet2</x:Name>");
            dgTemperatureLoggerDetails.RenderControl(oHtmlTextWriter1);
            //oStringWriter1.GetStringBuilder().Append("</x:ExcelWorkSheet></x:ExcelWorksheets></x:ExcelWorkbook></xml>");

            //Debug.WriteLine(oStringWriter1.ToString());
            Response.Write(oStringWriter1.ToString());
            //Response.Write(oStringWriter1.ToString());
            Response.End();
            */
        }
Ejemplo n.º 9
0
    /**/
    /// <summary>
    /// 将Web控件或页面信息导出(不带文件名参数)
    /// </summary>
    /// <param name="source">控件实例</param>        
    /// <param name="DocumentType">导出类型:Excel或Word</param>
    public static 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());
        HttpContext.Current.Response.End();
    }

    /**/
    /// <summary>
    /// 将Web控件或页面信息导出(带文件名参数)
    /// </summary>
    /// <param name="source">控件实例</param>        
    /// <param name="DocumentType">导出类型:Excel或Word</param>
    /// <param name="filename">保存文件名</param>
    public static 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());
        HttpContext.Current.Response.End();
    }
Ejemplo n.º 10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string GetHtmlFromControl(System.Web.UI.Control c)
        {
            if (c == null)
                return null;
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            System.IO.StreamWriter writer = new System.IO.StreamWriter(stream,
                ResponseEncoding);

            writer.AutoFlush = true;
            System.Web.UI.HtmlTextWriter htmlwr = new System.Web.UI.HtmlTextWriter(writer);
            c.RenderControl(htmlwr);
            return ResponseEncoding.GetString(stream.ToArray());
        }
Ejemplo n.º 11
0
        //add by zachary 2009-04-07
        public static void ToExcelG(System.Web.UI.WebControls.DataGrid DataGrid2Excel, string FileName, string Title, string Head, string Foot)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);

            ToExcelFrontDecorator(hw);
            if (Title != "")
                hw.Write(Title + "<br>");
            if (Head != "")
                hw.Write(Head);

            DataGrid2Excel.EnableViewState = false;
            DataGrid2Excel.RenderControl(hw);

            if (Foot != "")
                hw.Write(Foot + "<br>");

            ToExelRearDecorator(hw);

            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.Clear();
            response.Buffer = true;
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName) + ".xls");
            response.Charset = "UTF-8";
            response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            response.Write(sw.ToString());
            response.End();
        }
Ejemplo n.º 12
0
        public static void ToExcelF(System.Web.UI.HtmlControls.HtmlTable Table1, string FileName, string Title, string Head, string foot)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls");  //filename=Report.xls
            HttpContext.Current.Response.Charset = "UTF-8"; //UTF-8
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            //msword;image/JPEG;text/HTML;image/GIF;vnd.ms-excel

            //HttpContext.Current.Application.Page.EnableViewState = false; //Turn off the view state.

            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

            hw.WriteLine(@"<HTML>");
            hw.WriteLine(@"<BODY>");
            hw.WriteLine("<b>" + Title + "</b>");

            //string Head = @"<table border=1><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table>";
            if (Head != "")
                hw.WriteLine(Head);
            Table1.RenderControl(hw);

            if (foot != "")
                hw.WriteLine(foot);
            hw.WriteLine(@"</BODY>");
            hw.WriteLine(@"</HTML>");
            hw.Flush();
            hw.Close();
            HttpContext.Current.Response.Write(tw.ToString()); //Write the HTML back to the browser.
            HttpContext.Current.Response.End();
        }
Ejemplo n.º 13
0
        public static void ToExcelE(System.Web.UI.WebControls.DataGrid DataGrid2Excel, string FileName)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls");  //filename=Report.xls
            HttpContext.Current.Response.Charset = "GB2312"; //UTF-8
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

            DataGrid2Excel.RenderControl(hw); //Get the HTML for the control.
            hw.Flush();
            hw.Close();
            HttpContext.Current.Response.Write(tw.ToString()); //Write the HTML back to the browser.
            HttpContext.Current.Response.End();
        }
        private string GetControlHtml(System.Web.UI.Control ctrl)
        {
            StringBuilder strB = new StringBuilder(1024);

            StringWriter sw = new StringWriter(strB);

            try
            {
                HtmlTextWriter writer = new HtmlTextWriter(sw);
                ctrl.RenderControl(writer);

                return strB.ToString();
            }
            finally
            {
                sw.Close();
            }
        }
Ejemplo n.º 15
0
    public void ToExcel(System.Web.UI.Control ctl, String filename)
    {

        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");

        HttpContext.Current.Response.Charset = "UTF-8";

        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;

        HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword

        ctl.Page.EnableViewState = false;

        System.IO.StringWriter tw = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

        ctl.RenderControl(hw);

        HttpContext.Current.Response.Write(tw.ToString());

        HttpContext.Current.Response.End();

    }
Ejemplo n.º 16
0
 public void ToWord(System.Web.UI.Control ctl)
 {
     HttpContext.Current.Response.Clear();
       HttpContext.Current.Response.Charset = "";
       HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=DocLibrary.doc");
       HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
       //更改ContentType的值为ms-word即可实现导出到Word
       HttpContext.Current.Response.ContentType = "application/ms-word";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
       ctl.Page.EnableViewState = false;
       System.IO.StringWriter tw = new System.IO.StringWriter();
       System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
       ctl.RenderControl(hw);
       string html = checkStr(tw.ToString());//过滤字符串
       HttpContext.Current.Response.Write(html);
       //HttpContext.Current.Response.Write(tw);
       HttpContext.Current.Response.End();
 }
Ejemplo n.º 17
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=GB2312>");
     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();
 }