ASP.NET模板法操作word文档类:
using System; using System.Web; using System.Web.UI; using System.IO; namespace Common { /// <summary> /// WordOperation 的摘要说明。 /// 原理:在WORD模板中插入书签,然后用程序查找到书签,再插入数据 /// 要引用office程序集 /// </summary> public class WordOperation { private Word.ApplicationClass word = null; private Word.Document doc = null; private object obj = System.Reflection.Missing.Value; /// <summary> /// 打开WORD模板,可以是word2003、word2007 /// </summary> /// <param name="wordTemplateName">模板路径</param> /// <returns>bool</returns> public bool OpenTemplate(object wordTemplateName) { try { word = new Word.ApplicationClass(); doc = word.Documents.Open(ref wordTemplateName,ref obj,ref obj,ref obj,ref obj,ref obj,ref obj,ref obj, ref obj,ref obj,ref obj,ref obj); return true; } catch(Exception ex) { string msg = ex.ToString(); CloseWord(); return false; } } /// <summary> /// 查找到书签然后插入文本数据 /// </summary> /// <param name="index">书签名</param> /// <param name="markValue">要插入的值</param> /// <returns>bool</returns> public bool SetValue(object index,string markValue) { try { doc.Bookmarks.Item(ref index).Range.Text = markValue; return true; } catch(Exception ex) { string msg = ex.ToString(); // CloseWord(); return false; } } /// <summary> /// 查找到书签然后插入图片数据 /// </summary> /// <param name="index">书签名</param> /// <param name="picPath">图片路径</param> /// <returns></returns> public bool FillMapInWord(object index,string picPath) { try { doc.Bookmarks.Item(ref index).Range.InlineShapes.AddPicture(picPath, ref obj, ref obj, ref obj); return true; } catch { CloseWord(); return false; } } /// <summary> /// 保存WORD文档(只能保存为word2003格式) /// </summary> /// <param name="fileName">保存路径</param> /// <returns>bool</returns> public bool SaveAs(object fileName) { try { doc.SaveAs(ref fileName,ref obj,ref obj,ref obj,ref obj,ref obj,ref obj,ref obj,ref obj,ref obj,ref obj); return true; } catch { CloseWord(); return false; } } /// <summary> /// 关闭文档 /// </summary> public void CloseWord() { if ( doc != null ) { doc.Close(ref obj,ref obj,ref obj); System.Runtime.InteropServices.Marshal.ReleaseComObject( doc ); doc = null; } if ( word != null ) { word.Quit(ref obj,ref obj,ref obj); System.Runtime.InteropServices.Marshal.ReleaseComObject( word ); word = null; } } } }
调用示例:
private WordOperation wopt = null; private string uid = string.Empty; protected void Page_Load(object sender, System.EventArgs e) { if (WriteWord()) { Response.Redirect("../Reports/test.doc"); } } public bool WriteWord() { string path = string.Format("../../Reports/"); if (!Directory.Exists(HttpContext.Current.Server.MapPath(path))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path)); } object templateName = HttpContext.Current.Server.MapPath("../WordTemplate/test.docx"); object reportPath = HttpContext.Current.Server.MapPath("../Reports/test.doc"); wopt = new WordOperation(); wopt.OpenTemplate(templateName); wopt.SaveAs(reportPath); wopt.CloseWord(); if (wopt.OpenTemplate(reportPath)) { wopt.SetValue("aaa", "abcdefg"); wopt.SaveAs(reportPath); wopt.CloseWord(); return true; } return false; }