最近做项目,需要多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. Book of Evil

    Codeforces Round #196 (Div. 2) D:http://codeforces.com/contest/337/status/D 题意:给你一个树,然后树中有一m个点,求到这m个 ...

  2. 【 UVALive - 4287】Proving Equivalences (SCC缩点)

    题意: 给出N个命题,要求你证明这N个命题的等价性 比如有4个命题a,b,c,d,我们证明a<->b, b<->c,c<->d,每次证明都是双向的,因此一共用了6次 ...

  3. net.sf.json在处理json对象转换为普通java实体对象时的问题和解决方案

    我使用的net.sf.json是json-lib-2.4-jdk15.jar,把json对象转换为普通java实体对象时候有个问题,josn对象转换为java对象之后,json串里面的那几个小数点的值 ...

  4. delphi 连接 c++ builder 生成obj文件

    delphi 连接 c++ builder 生成obj文件 delphi 可以连接c++ builder 生成OMF格式的obj文件,会报一个错.[DCC Error] E2065 Unsatisfi ...

  5. GCC 命令行详解 -L 指定库的路径 -l 指定需连接的库名(转载)

    转载自:http://www.cnblogs.com/cy163/archive/2009/03/12/1409434.html 1.gcc包含的c/c++编译器gcc,cc,c++,g++,gcc和 ...

  6. HDU-4089 Activation

    http://acm.hdu.edu.cn/showproblem.php?pid=4089 Activation Time Limit: 20000/10000 MS (Java/Others)   ...

  7. 选择排序(SelectSorted)

    //简单的选择排序public class SelectSorted { public static void main(String[] args) { int[] array={12,24,9,7 ...

  8. 《University Calculus》-chape4-极坐标与圆锥曲线-极坐标系下的面积与弧长

    极坐标系下的面积: 在直角坐标系下一样,这里在极坐标系下,我们面临一个同样的问题:如何求解一个曲线围成的面积?虽然两种情况本质上是一样的,但是还是存在一些细小的区别. 在直角坐标系下中,我们是讨论一条 ...

  9. DB2 insert into 三种写法

    db2的insert into 支持三种格式,即:一次插入一行,一次插入多行和从SELECT语句中插入. 以表为例: create table “user" ( "name&quo ...

  10. 在Mapper中进行循环判断

    1.在Bo中将值保存在list中