导出excel,数据全是代码。 我检查过了,里面的代码跟源文件(页面右击-->查看源文件)的代码一模一样。
请问有谁知道这个问题怎么解决?

解决方案 »

  1.   

    protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.GridView3.Rows.Count == 0)
            {
                Response.Write("<script>alert('没有查找到数据,无法导出!')</script>");
            }
            else
            {
                //this.GridView3.AllowPaging = false; // 将有分页的GridView中的数据全部导出到Excel
                //GridView3Bind();
                export("application/ms-excel", "工作人员.xls");
                // 换成 export("application/ms-word", "工作人员.doc"); 那么导出的就是Word格式的了.
                //this.GridView3.AllowPaging = true;
                //GridView3Bind();
            }
        }
        public void export(string FileType, string FileName)
        {
            string style = "<style>td{mso-number-format:\"\\@\";}</style>";//导入到excel时,保存表里数字列中前面存在的 0 .
            for (int i = 0; i < GridView3.Columns.Count; i++)
            {
                if (GridView3.Columns[i].GetType() == typeof(CommandField))
                    GridView3.Columns[i].Visible = false;
            }
            PrepareGridViewForExport(GridView3);
            Response.Clear();
            Response.Charset = "GB2312";
            Response.ContentEncoding = Encoding.UTF7;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            this.GridView3.AllowPaging = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            this.GridView3.RenderControl(htw);
            Response.Write(style);
            Response.Write(sw.ToString());
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //在后台中重载VerifyRenderingInServerForm()方法,否则报错为“类型"GridView"的控件"GridView1"必须放在具有 runat=server 的窗体标记内“
        }
        private void PrepareGridViewForExport(Control gv)//模式化特殊元素 flashcong
        {
            Literal l = new Literal();
            for (int i = 0; i < gv.Controls.Count; i++)
            {
                if (gv.Controls[i].GetType() == typeof(HyperLink))
                {
                    l.Text = (gv.Controls[i] as HyperLink).Text;
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                if (gv.Controls[i].GetType() == typeof(LinkButton))
                {
                    l.Text = (gv.Controls[i] as LinkButton).Text;
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                else if (gv.Controls[i].GetType() == typeof(DropDownList))
                {
                    l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                else if (gv.Controls[i].GetType() == typeof(CheckBox))
                {
                    l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                if (gv.Controls[i].HasControls())
                {
                    PrepareGridViewForExport(gv.Controls[i]);
                }
            }
        }
    这方法能用 我昨天还用的 
      

  2.   

    http://topic.csdn.net/u/20080728/10/DD7D9AF8-0751-474B-A34C-147D71E28777.html
    参考