当前位置:编程学习 > C#/ASP.NET >>

在ASP.NET里面怎么把数据导出为WORD的形式!!!

在ASP.NET里面怎么把数据导出为WORD的形式!!!
数据是绑定DATAGRID的~~~
谢谢~~ --------------------编程问答-------------------- string style = @"<style> .text { mso-number-format:\@; } </script> ";
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.doc");
            //Response.ContentType = "application/excel";
            Response.ContentType = "application/word";
            Response.ContentType = "application/vnd.ms-word";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView1.RenderControl(htw);
            // Style is added dynamically
            Response.Write(style);
            Response.Write(sw.ToString());
            Response.End(); --------------------编程问答-------------------- 关注 --------------------编程问答-------------------- 期待答案。。。。。 --------------------编程问答-------------------- 期待! --------------------编程问答-------------------- mark --------------------编程问答-------------------- 顶上去 --------------------编程问答-------------------- 取出存数据的table(HTML)里的,然后写在新的HTML页中,把这个页存为WORK格式doc就行了 

偷懒的方法 --------------------编程问答-------------------- public void ToWord(System.Web.UI.Control ctl)//定义由所有asp、net服务器控件共享的属性、方法和事件
{
Response.Clear();//清除缓冲区流中的所有内容输出

Response.Buffer = true;//获取或设置一个值,指示该值是否缓冲输出,并在完成处理整个响应之后将其发送

Response.Charset="GB2312";//获取或设置输出流的http字符集

Response.AppendHeader("Content-Disposition","attachment;filename=word.doc");//将http头添加到输出流,Content-Disposition是头的名称

Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文

Response.ContentType = "application/ms-word";//设置输出文件类型为word文件

this.EnableViewState = false; 
          
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true); 
  
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);//实现一个用于将信息写入字符串的System.IO.TextWriter,该信息存储在基础System.Text.Builder中
   
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);//oStringWriter是要呈现html内容的System.IO.TextWriter对象 
  
ctl.RenderControl(oHtmlTextWriter);//oHtmlTextWriter是接收控件内容的HtmlTextWriter对象
     
Response.Write(oStringWriter.ToString());

this.dgrdMemorabilia.ShowHeader = false;

this.dgrdMemorabilia.ShowFooter = false;

Response.Flush();//向客户端发送当前所有缓冲的输出

Response.Close();//关闭到客户端的套接字连接

Response.End();

}

private void btnAdd_Click(object sender, System.EventArgs e)
{
DgrdWord();
ToWord(this.dgrdWord);
}
--------------------编程问答-------------------- 我刚好有个现成的,给你吧 --------------------编程问答-------------------- MARK
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,