ExtAspNet之Grid导出Excel

前台代码:

<ext:Button ID="btnExport" runat="server" Icon=ApplicationGo Text="导出Excel文件" ToolTip="将当前章的清单导出到Excel文件" ToolTipType=Title OnClick="btnExport_Click" EnableAjax="false">
</ext:Button>

 

后台代码:

 /// <summary>
        /// 导出Excel文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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("<table cellspacing=/"0/" rules=/"all/" border=/"1/" style=/"border-collapse:collapse;/">");
            sb.Append("<tr>");
            foreach (GridColumn column in grid.Columns)
            {
                sb.AppendFormat("<td>{0}</td>", column.HeaderText);
            }
            sb.Append("</tr>");
            foreach (GridRow row in grid.Rows)
            {
                sb.Append("<tr>");
                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("<img"))
                    //{
                    //    html = html.Replace("src=/"/extaspnet/", "src=/"http://localhost/extaspnet/");
                    //}
                    #endregion
                    sb.AppendFormat("<td>{0}</td>", html);
                }
                sb.Append("</tr>");
            }
            sb.Append("</table>");
            return sb.ToString();
        }

//===========以下隐藏不显示的列========================
 private string GetGridTableHtml2(ExtAspNet.Grid grid)
        {
            StringBuilder sb = new StringBuilder();
            Boolean[] blag = new Boolean[20];
            int k = 0;
            sb.Append("<table cellspacing=/"0/" rules=/"all/" border=/"1/" style=/"border-collapse:collapse;/">");
            sb.Append("<tr>");
            foreach (GridColumn column in grid.Columns)
            {
                if (!column.Hidden)
                {
                    blag[k] = true;
                    k++;
                    sb.AppendFormat("<td>{0}</td>", column.HeaderText);
                }
                else
                {
                    blag[k] = false;
                    k++;
                }
            }
            sb.Append("</tr>");
            for (int i = 0; i < grid.Rows.Count; i++) //遍历行
            {
                sb.Append("<tr>");
                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("<img"))
                        //{
                        //    html = html.Replace("src=/"/extaspnet/", "src=/"http://localhost/extaspnet/");
                        //}
                        #endregion
                        sb.AppendFormat("<td>{0}</td>", html);
                    }
                }
                sb.Append("</tr>");
            }
            sb.Append("</table>");
            return sb.ToString();
        }

你可能感兴趣的:(html,object,String,Excel,border,button)