环境:word 2010
vs 2010
sql server 2008
1.新建word模板,后缀名名dotx,然后再相应的需要输入数据的地方插入书签,如图
2.在项目中添加引用,Microsoft.Office.Interop.Word
3.代码,我使用的是ajax
前台代码:
//导出word $("#exportToWord").live("click", function () { var bugID = $(this).attr("name"); $.ajax({ type: "post", url: "../Office/ExportByWord.aspx/ExportBugByID", data: "{'id':'" + bugID + "'}", dataType: "json", contentType: "application/json; charset=utf-8", success: function (data) { alert(data.d); } }); });
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Services; using System.Web.Script.Services; using Microsoft.Office; using Microsoft.Office.Interop; using Microsoft.Office.Interop.Word; public partial class Office_ExportByWord : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e){} /// <summary> /// 根据bugID将单个bug导出到word文件中 /// </summary> /// <param name="id"></param> [WebMethod] public static void ExportBugByID(string id) { WriteIntoWord writeIntoWord = new WriteIntoWord(); string filePath = System.Web.HttpContext.Current.Server.MapPath("../Upload/System/bug.dot"); //模板路径 List<BugDetails> bugList = new List<BugDetails>(); BugDetails bugDetails = new BugDetails(); BugDB bugDB = new BugDB(); bugList = bugDB.GetBugByID(int.Parse(id)); bugDetails = bugList[0]; string saveDocPath = System.Web.HttpContext.Current.Server.MapPath("../Upload/System/File/" + bugDetails.Title + ".doc"); writeIntoWord.OpenDocument(filePath); writeIntoWord.WriteIntoDocument("Title", bugDetails.Title); writeIntoWord.WriteIntoDocument("Versions", bugDetails.ProduceName + bugDetails.Versions); writeIntoWord.WriteIntoDocument("Creator", bugDetails.Creator); writeIntoWord.WriteIntoDocument("AssignTo", bugDetails.AssignTo); writeIntoWord.WriteIntoDocument("BugType", bugDetails.BugType); writeIntoWord.WriteIntoDocument("Severity", bugDetails.Severity.ToString()); writeIntoWord.WriteIntoDocument("Priority", bugDetails.Priority.ToString()); writeIntoWord.WriteIntoDocument("Status", bugDetails.Status); writeIntoWord.WriteIntoDocument("ProblemDescription", bugDetails.ProblemDescription); writeIntoWord.WriteIntoDocument("SolveDescription", bugDetails.SolveDescription); writeIntoWord.WriteIntoDocument("CloseDescription", bugDetails.CloseDescription); writeIntoWord.Save_CloseDocument(saveDocPath); } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.Office; using Microsoft.Office.Interop; using Microsoft.Office.Interop.Word; using System.Text; /// <summary> ///WriteIntoWord 的摘要说明:使用C#将数据库中内容写入word中并导出 /// </summary> public class WriteIntoWord { ApplicationClass app = null; //定义应用程序对象 Document doc = null; //定义word文档对象 object missing = System.Reflection.Missing.Value; //定义空变量 object isReadOnly = false; // public WriteIntoWord() {} /// <summary> /// 向word中写入数据 /// </summary> /// <param name="FilePath"></param> public void OpenDocument(string FilePath) { object filePath = FilePath; app = new ApplicationClass(); //打开文档 doc = app.Documents.Open(ref filePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.Activate(); //激活文档 } /// <summary> /// 打开word,将对应数据写入word 中对应的书签中 /// </summary> /// <param name="BookmarkName">域标签(即word中定义的书签)</param> /// <param name="FillName">写入域中的内容</param> public void WriteIntoDocument(string BookmarkName,string FillName) { object bookmarkName = BookmarkName; Bookmark bm = doc.Bookmarks.get_Item(ref bookmarkName); //返回书签 bm.Range.Text = FillName; //设置书签域的内容 } /// <summary> /// 保存并关闭 /// </summary> /// <param name="SaveDocPath">文档另存为的路径</param> public void Save_CloseDocument(string SaveDocPath) { object savePath = SaveDocPath; object saveChanges = app.Options.BackgroundSave; //文档另存为 doc.SaveAs(ref savePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.Close(ref missing, ref missing, ref missing); //关闭文档 app.Quit(ref missing, ref missing, ref missing); //关闭应用程序 } }