ASP.NET模板法操作word文档

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; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值