我是用一个GridView的控件绑定SqlDataSource读出来的数据的,然后用http://blog.csdn.net/lz00728/article/details/7204818这里说的方法导出带分页数据的GridView到Excel,可是,还没运行就语法错误了...是不是我不能用这个函数?如果绑定的是一个SqlDataSource如何做到“为GridView重新绑定数据源 ”?就是说这句怎么改才适合我这种绑定的代码?

解决方案 »

  1.   

    找到一个代码,用了下,发现没有网格线,而且设定了写字符串用户名神马前面的“0”还是没了上代码        protected void ButtonExcel_Click(object sender, EventArgs e)
            {
                Export("全校教师工作量汇总表.xls", this.GridView1);
            }        public static void Export(string fileName, GridView gv)
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");            using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                    {
                        Table table = new Table();
                        string style = @"<style> .text { mso-number-format:\@; } </script> ";                     if (gv.HeaderRow != null)
                        {
                            PrepareControlForExport(gv.HeaderRow);
                            table.Rows.Add(gv.HeaderRow);
                        }                    foreach (GridViewRow row in gv.Rows)
                        {
                            PrepareControlForExport(row);
                            table.Rows.Add(row);
                        }                    if (gv.FooterRow != null)
                        {
                            PrepareControlForExport(gv.FooterRow);
                            table.Rows.Add(gv.FooterRow);
                        }                    table.RenderControl(htw);                    HttpContext.Current.Response.Write(style);
                        HttpContext.Current.Response.Write(sw.ToString());
                        HttpContext.Current.Response.End();
                    }
                }
            }        private static void PrepareControlForExport(Control control)
            {
                for (int i = 0; i < control.Controls.Count; i++)
                {
                    Control current = control.Controls[i];
                    if (current is LinkButton)
                    {
                        control.Controls.Remove(current);
                        control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
                    }
                    else if (current is ImageButton)
                    {
                        control.Controls.Remove(current);
                        control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
                    }
                    else if (current is HyperLink)
                    {
                        control.Controls.Remove(current);
                        control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
                    }
                    else if (current is DropDownList)
                    {
                        control.Controls.Remove(current);
                        control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
                    }
                    else if (current is CheckBox)
                    {
                        control.Controls.Remove(current);
                        control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
                    }                if (current.HasControls())
                    {
                        PrepareControlForExport(current);
                    }
                }
            }