Ejemplo n.º 1
0
 /// <summary>
 /// 下载控件内容到,Excel 文件
 /// </summary>
 /// <param name="response">Response 引用</param>
 /// <param name="con">父控件,接受子控件的引用(GridView , DataList , Repeater)</param>
 /// <param name="fileName">下载文件名</param>
 public static string DownLoadControlAsExcel(HttpResponse response, Control con, string fileName)
 {
     try
     {
         //设置文件名,及下载格式
         response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8) + ".xls");
         response.ContentType = "application/vnd.ms-excel";
         //response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
         //this.EnableViewState = false;
         System.IO.StringWriter       strWriter = new System.IO.StringWriter();
         System.Web.UI.HtmlTextWriter hw        = new System.Web.UI.HtmlTextWriter(strWriter);
         //设置到处内容控件(Anyone 父类Control即可)
         con.RenderControl(hw);
         response.Write(strWriter.ToString());
         //释放资源
         strWriter.Dispose();
         hw.Dispose();
         response.End();
         return(null);
     }
     catch
     {
         return("导出数据到Excel发生异常。");
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 导出GridView中的数据到Excel
        /// </summary>
        /// <param name="gvw"></param>
        /// <param name="DataList"></param>
        private static void ExportToExcel(GridView gvw, string title, string TableName)
        {
            //int coun = ExistsRegedit();
            //string fileName = string.Format("DataInfo{0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
            //if (coun >0)
            //{
            //    fileName = string.Format("DataInfo{0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
            //    //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
            //}
            //else
            //{
            //    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            //    //Page.RegisterStartupScript("mess", "<script>alert('该机器没有安装任何office软件');</script>");
            //    //return;
            //}

            for (int i = 0; i < gvw.Rows.Count; i++)
            {
                for (int j = 0; j < gvw.HeaderRow.Cells.Count; j++)
                {
                    //这里给指定的列编辑格式,将数字输出为文本,防止数字溢出
                    gvw.Rows[i].Cells[j].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                }
            }
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            //fileName = string.Format("DataInfo{0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + TableName + System.Web.HttpUtility.UrlEncode(title) + DateTime.Now.ToShortDateString() + ".xls");
            HttpContext.Current.Response.Charset         = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");


            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.Write("<html><head><style type='text/css'>h1 {font-family: Arial, sans-serif;font-size: 24px;margin: 5px;color: #369;text-align:center}h3 {text-align:center;color:#0099FF;}table{border-collapse: collapse;font-family: 'Lucida Sans Unicode','Lucida Grande',Sans-Serif;font-size: 12px;margin: 20px;text-align: left;width: 97%}td {padding: 8px;}th{text-align:center;}</style></head><body>");
            StringWriter   tw = new System.IO.StringWriter();
            HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

            gvw.RenderControl(hw);
            if (!string.IsNullOrEmpty(title))
            {
                //HttpContext.Current.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" + title + "</font></center></b>");
            }
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.Write("</body></html>");
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();
            HttpContext.Current.Response.End();
            gvw.Dispose();
            tw.Dispose();
            hw.Dispose();
            gvw = null;
            tw  = null;
            hw  = null;
        }
Ejemplo n.º 3
0
        public static void ExportToExcel(DataTable dataList, string filename, string context)
        {
            GridView gvw = new GridView();

            gvw.AutoGenerateColumns = true;
            gvw.DataSource          = dataList;
            // SetStype(gvw);
            gvw.DataBind();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            //fileName = string.Format("DataInfo{0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + System.Web.HttpUtility.UrlEncode(filename) + DateTime.Now.ToShortDateString() + ".xls");
            HttpContext.Current.Response.Charset         = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");


            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.Write("<html><head><style type='text/css'>h1 {font-family: Arial, sans-serif;font-size: 24px;margin: 5px;color: #369;text-align:center}h3 {text-align:center;color:#0099FF;}table{border-collapse: collapse;font-family: 'Lucida Sans Unicode','Lucida Grande',Sans-Serif;font-size: 12px;margin: 20px;text-align: left;width: 97%}td {padding: 8px;}th{text-align:center;}</style></head><body>");
            HttpContext.Current.Response.Write(context);
            StringWriter   tw = new System.IO.StringWriter();
            HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

            gvw.RenderControl(hw);

            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.Write("</tbody> </table>");
            HttpContext.Current.Response.Write("</body></html>");
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();
            HttpContext.Current.Response.End();
            gvw.Dispose();
            tw.Dispose();
            hw.Dispose();
            gvw = null;
            tw  = null;
            hw  = null;


            //var res = System.Web.HttpContext.Current.Response;
            //content = String.Format("<style type='text/css'>{0}</style>{1}", cssText, content);

            //res.Clear();
            //res.Buffer = true;
            //res.Charset = "UTF-8";
            //res.AddHeader("Content-Disposition", "attachment; filename=" + filename);
            //res.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            //res.ContentType = "application/vnd.ms-excel;charset=UTF-8";
            ////res.ContentType = "application/ms-excel;charset=UTF-8";
            //res.Write(content);
            //res.Flush();
            //res.End();
        }
Ejemplo n.º 4
0
 public void Compression(System.Web.UI.HtmlTextWriter writer)
 {
     StringWriter sr = new StringWriter();
     HtmlTextWriter tw = new HtmlTextWriter(sr);
     base.Render(tw);
     tw.Flush();
     tw.Dispose();
     StringBuilder sb = new StringBuilder(sr.ToString());
     string outhtml = Regex.Replace(sb.ToString(), "\\r+\\n+\\s+", string.Empty);
     writer.Write(outhtml);
     sr.Dispose();
 }
Ejemplo n.º 5
0
        //---------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Uses streams to render HTML controls and get the html code generated
        /// </summary>
        /// <param name="ControlToRender"></param>
        /// <returns></returns>
        public static string RenderControlToHtml(Control ControlToRender)
        {
            StringBuilder sb = new StringBuilder();

            try {
                System.IO.StringWriter       stWriter   = new System.IO.StringWriter(sb);
                System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(stWriter);
                ControlToRender.RenderControl(htmlWriter);

                stWriter.Dispose();
                htmlWriter.Dispose();
            } catch (Exception ex) {
                Logger.Log("Problem rendering a control to html in HTMLUtilities.RenderControlToHtml: " + ex.ToString());
            }

            return(sb.ToString());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 导出GridView中的数据到Excel
        /// </summary>
        /// <param name="gvw">GridView</param>
        /// <param name="title">表头</param>
        /// <param name="footer">表尾</param>
        private static void ExportToExcel(GridView gvw, string title, string[] footer)
        {
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.ContentType     = "application/vnd.xls";
            StringWriter   tw = new System.IO.StringWriter();
            HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

            gvw.RenderControl(hw);
            HttpContext.Current.Response.Write(style);
            if (!string.IsNullOrEmpty(title))
            {
                HttpContext.Current.Response.Write("<b><center><font size=3 face=Verdana>" + title + "</font></center></b>");
            }

            HttpContext.Current.Response.Write(tw.ToString());
            if (footer != null)
            {
                for (int i = 0; i < footer.Length; i++)
                {
                    HttpContext.Current.Response.Write("<br><font size=2>" + footer[i].ToString() + "</font>");
                }
            }

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();
            HttpContext.Current.Response.End();
            gvw.Dispose();
            tw.Dispose();
            hw.Dispose();
            gvw = null;
            tw  = null;
            hw  = null;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 导出GridView中的数据到Excel
        /// </summary>
        /// <param name="gvw"></param>
        private void ExportToExcel(GridView gvw)
        {
            var fileName = InitFileName();

            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Charset         = this.Charset;
            HttpContext.Current.Response.ContentEncoding = this.ContentEncoding;
            HttpContext.Current.Response.Buffer          = true;
            HttpContext.Current.Response.HeaderEncoding  = this.ContentEncoding;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            HttpContext.Current.Response.AppendHeader("Content-Type", string.Format("application/vnd.ms-excel;charset={0};", this.Charset));
            HttpContext.Current.Response.AppendHeader("Content-Language", "zh-CN");
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

            HttpContext.Current.Response.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
            HttpContext.Current.Response.Write(string.Format("<html>\n<head>\n<title>{0}</title>\n", this.Options.TitleText));
            HttpContext.Current.Response.Write(string.Format("<meta http-equiv=\"Content-Type\" content=\"text/html; charset={0}\">\n", this.Charset));
            HttpContext.Current.Response.Write("<style type=\"text/css\" title=\"\">\n<!--\n");
            HttpContext.Current.Response.Write("body {margin:0;padding:0;color:#000;background-color:#FFF;}\n");
            HttpContext.Current.Response.Write("body,p {font-size:12px;line-height:150%;font-family:Arial, Helvetica, sans-serif;}\n");
            HttpContext.Current.Response.Write("//-->\n</style>\n</head>\n");
            HttpContext.Current.Response.Write("<body>\n");

            System.Globalization.CultureInfo m_CI = new System.Globalization.CultureInfo("ZH-CN", true);//区域设置
            StringWriter   tw = new System.IO.StringWriter(m_CI);
            HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

            gvw.RenderControl(hw);

            int spancol = this.Columns.Count;

            if (gvw.HeaderRow != null)
            {
                spancol = gvw.HeaderRow.Cells.Count;
            }
            if (!string.IsNullOrEmpty(Options.TitleText))
            {
                string css = " style=\"{0}\" ";
                if (Options.TitleIsBold)
                {
                    css = string.Format(css, "font-weight:bolder;{0}");
                }
                if (Options.TitleIsItalic)
                {
                    css = string.Format(css, "font-style:italic;{0}");
                }

                if (!string.IsNullOrEmpty(Options.TitleFontName))
                {
                    css = string.Format(css, "font-family:" + Options.TitleFontName + ";{0}");
                }
                else
                {
                    css = string.Format(css, "font-family:Arial, Helvetica, sans-serif;{0}");
                }

                if (!Options.TitleFontSize.IsEmpty)
                {
                    css = string.Format(css, "font-size:" + Options.TitleFontSize.ToString() + ";{0}");
                }
                if (!Options.TitleBackColor.IsEmpty)
                {
                    css = string.Format(css, "background-color:" + Options.TitleBackColor.Name + ";{0}");
                }
                if (!Options.TitleForeColor.IsEmpty)
                {
                    css = string.Format(css, "color:" + Options.TitleForeColor.Name + ";{0}");
                }
                switch (Options.TitleHorizontalAlign)
                {
                case HorizontalAlign.Center:
                    css = string.Format(css, "text-align:center;{0}");
                    break;

                case HorizontalAlign.Justify:
                    css = string.Format(css, "text-align:justify;{0}");
                    break;

                case HorizontalAlign.Right:
                    css = string.Format(css, "text-align:right;{0}");
                    break;

                case HorizontalAlign.Left:
                case HorizontalAlign.NotSet:
                default:
                    break;
                }
                if (!Options.TitleHeight.IsEmpty)
                {
                    css = string.Format(css, "height:" + Options.TitleHeight.Value.ToString("f0") + "px;{0}");
                }
                css = css.Replace("{0}", "");
                HttpContext.Current.Response.Write(string.Format("<table><tr><td {0} colspan=\"{2}\">{1}</td></tr></table>", css, Utils.EncodeHtml(Options.TitleText), spancol));
            }
            if (!string.IsNullOrEmpty(Options.TitleInfoHtml))
            {
                HttpContext.Current.Response.Write(string.Format("<table><tr><td  colspan=\"{0}\">{1}</td></tr></table>", spancol, Options.TitleInfoHtml));
            }

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

            if (!string.IsNullOrWhiteSpace(Options.InfoHtml))
            {
                HttpContext.Current.Response.Write(string.Format("<table><tr><td colspan=\"{1}\"></td></tr><tr><td colspan=\"{1}\">{0}</td></tr><tr><td colspan=\"{1}\"></td></tr></table>", Options.InfoHtml, spancol));
            }

            _watch.Stop();

            HttpContext.Current.Response.Write(string.Format("<div align=\"right\" style=\"color:#AAAAAA;font-family:Verdana\">导出时间:{0}, 记录数量(s): {2}, 耗时: {1}ms</div>", DateTime.Now, _watch.ElapsedMilliseconds, gvw.Rows.Count));
            HttpContext.Current.Response.Write("\n</body>\n</html>");
            //HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

            gvw.Dispose();
            tw.Dispose();
            hw.Dispose();

            gvw = null;
            tw  = null;
            hw  = null;
        }
Ejemplo n.º 8
0
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder renderedOutput = null;
            StringWriter strWriter = null;
            HtmlTextWriter tWriter = null;

            try
            {
                renderedOutput = new StringBuilder();
                strWriter = new StringWriter( renderedOutput );
                tWriter = new HtmlTextWriter( strWriter );

                view.RenderControl( tWriter );

                tWriter.Flush();
                strWriter.Flush();

                Response.Write( renderedOutput.ToString() );
                Response.Flush();
            }
            catch( Exception ex )
            {
                SendJson( new Framework.API.ReturnObject {
                    Error = true,
                    Message = "There was an error loading the requested page.",
                    Result = new {
                        Message = ex.Message,
                        Type = ex.GetType().FullName,
                        StackTrace = ex.StackTrace
                    }
                } );
            }
            finally
            {
                if( tWriter != null )
                {
                    tWriter.Close();
                    tWriter.Dispose();
                    tWriter = null;
                }

                if( strWriter != null )
                {
                    strWriter.Close();
                    strWriter.Dispose();
                    strWriter = null;
                }
            }

            Response.End();
        }
Ejemplo n.º 9
0
 public static String getHTML(WebControl aControl)
 {
     StringWriter sr = new StringWriter();
       HtmlTextWriter writer = new HtmlTextWriter(sr);
       aControl.RenderControl(writer);
       writer.Dispose();
       return sr.ToString();
 }
Ejemplo n.º 10
0
        /// <summary>
        ///     导出GridView中的数据到Excel
        /// </summary>
        /// <param name="gvw"></param>
        /// <param name="title"></param>
        /// <param name="TableName"></param>
        private static void ExportToExcel(GridView gvw, string title, string TableName) {
            //int coun = ExistsRegedit();
            //string fileName = string.Format("DataInfo{0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
            //if (coun >0)
            //{
            //    fileName = string.Format("DataInfo{0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
            //    //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
            //}
            //else
            //{
            //    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            //    //Page.RegisterStartupScript("mess", "<script>alert('该机器没有安装任何office软件');</script>");
            //    //return;
            //}

            for (var i = 0; i < gvw.Rows.Count; i++) {
                for (var j = 0; j < gvw.HeaderRow.Cells.Count; j++) {
                    //这里给指定的列编辑格式,将数字输出为文本,防止数字溢出  
                    gvw.Rows[i].Cells[j].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                    //nowrap="nowrap" 不换行
                    gvw.Rows[i].Cells[j].Attributes.Add("nowrap", "nowrap");
                }
            }
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            //fileName = string.Format("DataInfo{0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
            HttpContext.Current.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(TableName) + HttpUtility.UrlEncode(title) + ".xls");
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");


            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            var tw = new StringWriter();

            var hw = new HtmlTextWriter(tw);

            gvw.RenderControl(hw);
            if (!string.IsNullOrEmpty(title)) {
                HttpContext.Current.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" + title +
                                                   "</font></center></b>");
            }
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.Flush();
            //HttpContext.Current.Response.Close();
            HttpContext.Current.Response.End();
            gvw.Dispose();
            tw.Dispose();
            hw.Dispose();
        }
Ejemplo n.º 11
0
        // -----------------------------------------------------------------------------------------------
        protected override void Render(HtmlTextWriter writer)
        {
            System.IO.MemoryStream mem = new System.IO.MemoryStream();
            System.IO.StreamWriter twr = new System.IO.StreamWriter(mem);
            System.Web.UI.HtmlTextWriter myWriter = new HtmlTextWriter(twr);
            base.Render(myWriter);
            myWriter.Flush();
            myWriter.Dispose();

            // Write our final output string
            writer.Write(AJAXhtmloutput);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Executa el Render del GridView para generar el Codigo HTML
        /// </summary>
        /// <param name="grid">Grid</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string GetHTMLGridView(GridView grid)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter textwriter = new StringWriter(sb);
            HtmlTextWriter htmlwriter = new HtmlTextWriter(textwriter);
            grid.RenderControl(htmlwriter);
            htmlwriter.Flush();
            textwriter.Flush();
            htmlwriter.Dispose();
            textwriter.Dispose();

            return sb.ToString();
        }