將gridview數據導出到word

            Response.ClearContent();
            Response.Buffer = false;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//防止亂碼
            string style = @"<style> .text { mso-number-format:\@; } </style> ";//控制文本格式,防止0丟失
            Response.AddHeader("content-disposition", "attachment; filename=MyWord.doc");
            Response.ContentType = "application/word";

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            gridView.AllowPaging = false;
            gridView.AllowSorting = false;
            BindData1();//再綁定一下數據

            gridView.RenderControl(htw);
            Response.Write(style);

            Response.Write(sw.ToString());
            Response.End();
            gridView.AllowPaging = true;
            gridView.AllowSorting = true;


        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }

 將頁面EnableViewState="false"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章