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

ASP.NET如何将查询出的数据存在Word文档?

比如我新建了一个数据源 sqldatasource! 然后执行了select操作返回了查询出的结果! 我想把这个查询结果导出 存为到word文档。
求详细代码!
补充:因为从数据库读出来的数据生成word文档,然后已二进制的形式在写入数据库。
但不知道如何生成word文档。。。求解决!!!
答案:不管是导出为excel还是word,都有十分简单的方法可以实现。
//由gridviw导出为Excel
public static void ToExcel(System.Web.UI.Control ctl)
{

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");

HttpContext.Current.Response.Charset = "UTF-8";

HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;

HttpContext.Current.Response.ContentType = "application/msword";

ctl.Page.EnableViewState = false;

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

ctl.RenderControl(hw);

HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();
}
你可以看下这篇文章: http://www.sosuo8.com/article/show.asp?id=2415
是导出EXCEL,但是只要修改这句:
HttpContext.Current.Response.ContentType = "application/msword";就可以了

上一个:ASP中怎样在点击按钮后出发事件?
下一个:asp.net中GridView内的触发事件

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,