ExtAspNet之Grid导出Excel

前台代码:


 

后台代码:

 ///


        /// 导出Excel文件
        ///

        ///
        ///
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string namestr = "合同段" + htdselect.SelectedText ;
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=/"" + context.Server.UrlEncode(String.Format("{0}.xls", namestr)) + "/"");
            Response.ContentType = "application/excel";
            Response.Write(GetGridTableHtml(Grid1));
            Response.End();

            btnExport.Enabled = true;
        }
        private string GetGridTableHtml(ExtAspNet.Grid grid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("

");
            sb.Append("");
            foreach (GridColumn column in grid.Columns)
            {
                sb.AppendFormat("", column.HeaderText);
            }
            sb.Append("");
            foreach (GridRow row in grid.Rows)
            {
                sb.Append("");
                foreach (object value in row.Values)
                {
                    string html = value.ToString();
                    #region 处理CheckBox和图片
                    处理CheckBox
                    //if (html.Contains("box-grid-static-checkbox"))
                    //{
                    //    if (html.Contains("box-grid-static-checkbox-uncheck"))
                    //    {
                    //        html = "×";
                    //    }
                    //    else
                    //    {
                    //        html = "√";
                    //    }
                    //}
                    处理图片
                    //if (html.Contains("                    //{
                    //    html = html.Replace("src=/"/extaspnet/", "src=/"http://localhost/extaspnet/");
                    //}
                    #endregion
                    sb.AppendFormat("
", html);
                }
                sb.Append("");
            }
            sb.Append("
{0}
{0}
");
            return sb.ToString();
        }

//===========以下隐藏不显示的列========================
 private string GetGridTableHtml2(ExtAspNet.Grid grid)
        {
            StringBuilder sb = new StringBuilder();
            Boolean[] blag = new Boolean[20];
            int k = 0;
            sb.Append("

");
            sb.Append("");
            foreach (GridColumn column in grid.Columns)
            {
                if (!column.Hidden)
                {
                    blag[k] = true;
                    k++;
                    sb.AppendFormat("", column.HeaderText);
                }
                else
                {
                    blag[k] = false;
                    k++;
                }
            }
            sb.Append("");
            for (int i = 0; i < grid.Rows.Count; i++) //遍历行
            {
                sb.Append("");
                for (int j = 0; j < grid.Columns.Count; j++)   //遍历列
                {
                    string html = "";
                    if (blag[j])
                    {
                        html = grid.Rows[i].Values[j].ToString();
                        if (html.Contains("编辑"))
                        {
                            html = "";
                        }
                        if (html.Contains("删除"))
                        {
                            html = "";
                        }
                        #region 处理CheckBox和图片
                        // 处理CheckBox
                        //if (html.Contains("box-grid-static-checkbox"))
                        //{
                        //    if (html.Contains("box-grid-static-checkbox-uncheck"))
                        //    {
                        //        html = "×";
                        //    }
                        //    else
                        //    {
                        //        html = "√";
                        //    }
                        //}
                        // 处理图片
                        //if (html.Contains("                        //{
                        //    html = html.Replace("src=/"/extaspnet/", "src=/"http://localhost/extaspnet/");
                        //}
                        #endregion
                        sb.AppendFormat("
", html);
                    }
                }
                sb.Append("");
            }
            sb.Append("
{0}
{0}
");
            return sb.ToString();
        }

你可能感兴趣的:(ExtAspNet之Grid导出Excel)