DataList套GridView 动态页面内容 导出为word文件的方法或代码

来源:百度知道 编辑:UC知道 时间:2024/05/13 03:27:32

1、在有页面中添加一个GridView1,添加一个button2、在页面加载事件中为GridView1填充数据3、button 控件的Click事件中添加如下代码: string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode("word.doc"));
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
DataList1.RenderControl(htmlWrite);
Response.Write(style);
Response.Write(stringWrite.ToString());
Response.End();这样就可以把GridView中的数据导出为 Word文档了