日期:2014-05-20  浏览次数:20507 次

用asp.net从sql数据库中提取数据,并填充到相应的word模板文件
我现在有一个word模板文件,已经建立好书签,现在想使用asp.net从sql数据库中提取数据,并填充到相应的书签中,生成word文档,由于是第一次使用office   开发,希望可以提供一些源代码。

------解决方案--------------------
Object Nothing = System.Reflection.Missing.Value;

//取得Word文件保存路径
object filename = Server.MapPath( " ") +@ "\rsc.doc ";
//@ "c:\rsc.doc ";

//创建一个名为WordApp的组件对象
Word.Application WordApp = new Word.ApplicationClass();

// //打开WordDoc文档
Word.Document WordDoc = WordApp.Documents.Open(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);

//对标签 "adjure_class "进行填充
object bkmA = "adjure_class ";

if(WordApp.ActiveDocument.Bookmarks.Exists( "adjure_class ") == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmA).Select();
}
WordApp.Selection.TypeText( "111 ");//此处内容可从数据库中取,怎么取的代码就省了吧

filename = Server.MapPath( " ")+@ "\docs\test.doc ";

WordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);

//关闭WordDoc文档对象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);

//关闭WordApp组件对象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);