最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中的文件在下次使用的时候能正常使用。

代码:

     /// <summary>
/// 读取出word文档中的内容后对word文档进行修改并保存在本地
/// </summary>
    /// <param name="jtmodel">实体类(在数据库中读取出来的数据)</param>
/// <param name="pathfile">替换后的字符</param>
private void ReadWord(Jtmodle jtmodel, String pathfile)
{
Microsoft.Office.Interop.Word.Document oWordDoc = null;
Microsoft.Office.Interop.Word.ApplicationClass oWordApp = null;
object missing = System.Reflection.Missing.Value;
try
{
object fileName = System.Web.HttpContext.Current.Server.MapPath(pathfile).ToString();
object readOnly = false;
object isVisible = true;
oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing);
#region 字符替换
oWordApp = Replce("{htmc}", jtmodel.htmc.ToString(), oWordApp);
oWordApp = Replce("{htbh}", jtmodel.htbh.ToString(), oWordApp);
oWordApp = Replce("{qydd}", jtmodel.qydd.ToString(), oWordApp);
oWordApp = Replce("{qyrq}", jtmodel.qyrq.ToString(), oWordApp);
oWordApp = Replce("{sxrq}", jtmodel.sxrq.ToString(), oWordApp);
oWordApp = Replce("{fkqx}", jtmodel.fkqx.ToString(), oWordApp);
oWordApp = Replce("{jfqx}", jtmodel.jfqx.ToString(), oWordApp);#endregion #region 保存文件到客户端
string filePath = @"d:\\WordDown\\";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
if (!System.IO.File.Exists(@"d:\\WordDown\\" + jtmodel.name.Trim() + ".doc"))
{
oWordDoc.SaveAs(@"d:\\WordDown\\" + jtmodel.name.Trim() + ".doc");
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", @"<script>alert('合同文档生成成功,已保存在本地磁盘D:\\WordDown\\" + jtmodel.name.Trim() + ".doc!');</script>");
}
else
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", @"<script>alert('该合同文档已经存在本地磁盘D:\\WordDown\\" + jtmodel.name.Trim() + ".doc,生成失败!');</script>");
}
oWordApp.NormalTemplate.Saved = true;
oWordDoc.Close(ref missing, ref missing, ref missing);
oWordApp.Application.Quit(ref missing, ref missing, ref missing);
#endregion
}
catch
{
oWordDoc.Close(ref missing, ref missing, ref missing);
oWordApp.Application.Quit(ref missing, ref missing, ref missing);
}
}
/// <summary>
/// 替换word中的字符串
/// </summary>
/// <param name="oldstr">被替换的符</param>
/// <param name="newstr">替换后的字符</param>
/// <param name="oWordApp"></param>
private Microsoft.Office.Interop.Word.ApplicationClass Replce(string oldstr, string newstr, Microsoft.Office.Interop.Word.ApplicationClass oWordApp)
{
object missing = System.Reflection.Missing.Value;
oWordApp.Selection.Find.ClearFormatting();
oWordApp.Selection.Find.Replacement.ClearFormatting();
oWordApp.Selection.Find.Text = oldstr;
oWordApp.Selection.Find.Replacement.Text = newstr;
object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
oWordApp.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref objReplace, ref missing, ref missing, ref missing, ref missing);
return oWordApp;
}

原本不打算用替换字符串的方式编辑word,在网上看到还有可以在word中添加标签的然后对word文档进行编辑,但是苦于不知道怎么给word文档插入标签,只好适用替换字符串的方法来实现功能了

asp.net对word文档进行修改 对于使用word文档做模板编辑比较适用的更多相关文章

  1. ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)

    ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)      上述博文里提到的在线浏览pdf的方案不错,但word转pdf的那个dll只支持doc不支持docx,附上最新的下载链 ...

  2. [No00005B] word快速插入当前时间&怎样一次性删除文档中的全部链接

    按Alt+Shift+D键来插入系统日期 按Alt+Shift+T键则插入系统当前时间 同时,在插入的时间上右键->编辑域 一次性删除文档中的全部链接: 方法1:一劳永逸法(推荐) 因为链接大多 ...

  3. 用Word收集网页中的内容,用文档结构图整理

    如何用Word保存网页中的内容 网页中的内容,用什么保存好? 用笔记类软件是个不错的选择,还可以用 Word 保存,这样方便用“文档结构图”来整理网页. 如图:网页收集后用文档结构图进行整理. (图一 ...

  4. 用Swashbuckle给ASP.NET Core的项目自动生成Swagger的API帮助文档

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用Swashbuckle给ASP.NET Core的项目自动生成Swagger的API帮助文档.

  5. VSTO 得到Office文档的选中内容(Word、Excel、PPT、Outlook)

    原文:VSTO 得到Office文档的选中内容(Word.Excel.PPT.Outlook) 目的:得到在Word.Excel.PPT.Outlook中选中的一段内容. Word: private ...

  6. Word打开默认显示缩略图,而不是文档结构图

    So easy! 1.打开Word文档,点击缩略图右侧的"X",关闭缩略图: 2.打开菜单[视图],勾选"文档结构图": 3.关闭当前Word文档: 4.再次打 ...

  7. 打开word文档时提示“Microsoft Office Word已停止工作”

    我的电脑(Win10)有Office 2003和2013两个版本,可能由于之前超长待机等原因导致word 2003的文件(.doc)不能正常打开,没次都会提示“Microsoft Office Wor ...

  8. PowerDesigner导出word,PowerDesigner把表导出到word,PDM导出word文档

    PowerDesigner导出word,PowerDesigner把表导出到word,PDM导出word文档 >>>>>>>>>>>& ...

  9. 根据Attribute值条件对XML文档进行修改

    现手上有一个XML文档, 需要把"直接工序"改为"间接工序0". 你可以使用<对XML文档进行修改> http://www.cnblogs.com/ ...

随机推荐

  1. 【Tools】maven安装

    安装Maven插件老是报以下的错误,好像少了一个叫guava库的东西,但是在其他机器安装不报这个错误. Cannot complete the install because one or more  ...

  2. Connect them

    zoj3204: 最小生成树,要求最小字典序的解. 用kruscal算法,先排序,输出的时候也要排序. /* zoj3204 解题思路: 赤裸裸的最小生成树.只是要求输出字典序最小的连接方案. 所以在 ...

  3. Condition 的使用

    Condition 将 Object 监视器方法(wait.notify 和 notifyAll)分解成截然不同的对象,以便通过将这些对象与任意 Lock 实现组合使用,为每个对象提供多个等待 set ...

  4. keil多文件组织方法

    方法一 首先新建一个main.c的文件,加入到项目中,该文件中主要写main函数,然后,新建文件,如delay.c,编写内容之后,不要加入到项目,而是在main.c文件的开始写上#include“de ...

  5. 利用ROWID 快速更新单表记录

    -----对于普通表 实现: UPDATE T_PM_DEPOSIT_HIS b SET flag = SUBSTR( flag, 1, 8 )||'4'|| CASE WHEN term <= ...

  6. suse linux编译安装GCC报错

    gcc编译安装过程 1.先安装三个库 gmp mprc mpc 这三个库的源码要到官网去下载 1)安装gmp:首先建立源码同级目录 gmp-build,输入命令,第一次编译不通过,发现缺少一个叫m4的 ...

  7. poj 2960 S-Nim(SG函数)

    S-Nim Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3694   Accepted: 1936 Description ...

  8. 1‘b0 什么意思

    在看datasheet 中有类似表达式如下: 3'b000, 1'b1, 1'b0; 3'b000这个表示:b代表二進制.3代表位元數. 1'b1:宣告為一位元二進制之值為1,一般除了可以宣告b外,也 ...

  9. hdoj 2141 Can you find it?【二分查找+暴力】

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  10. Windows - 子系统(subsystem)错误

    Windows - 子系统(subsystem)错误 本文地址: http://blog.csdn.net/caroline_wendy VS2012生成错误: "error LNK2019 ...