C#实现WORD文档的内容复制和替换

最近一个项目的需求是要根据一个Word文档的模板,用记录集的具体内容替换掉里面的标识字符的内容,生成不同的文档。
分两步:
第一:复制模板的内容到一个Document对象里
从源DOC文档复制内容返回一个Document类 #region从源DOC文档复制内容返回一个Document类
/// <summary>
///从源DOC文档复制内容返回一个Document类
/// </summary>
/// <param name="sorceDocPath">源DOC文档路径</param>
/// <returns>Document</returns>
protectedDocument copyWordDoc( objectsorceDocPath)
{
objectobjDocType = WdDocumentType.wdTypeDocument;
objecttype = WdBreakType.wdSectionBreakContinuous;

//Word应用程序变量
Application wordApp;
//Word文档变量
Document newWordDoc;

objectreadOnly = false;
objectisVisible = false;

//初始化
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
wordApp = newApplicationClass();

Object Nothing = System.Reflection.Missing.Value;

//wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
newWordDoc = wordApp.Documents.Add( refNothing, refNothing, refNothing, refNothing);

Document openWord;
openWord = wordApp.Documents.Open( refsorceDocPath, refNothing, refreadOnly, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refisVisible, refNothing, refNothing, refNothing, refNothing);
openWord.Select();
openWord.Sections[1].Range.Copy();

objectstart = 0;
Range newRang = newWordDoc.Range( refstart, refstart);

//插入换行符
//newWordDoc.Sections[1].Range.InsertBreak(ref type);
newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);
openWord.Close( refNothing, refNothing, refNothing);
returnnewWordDoc;
}
#endregion
第二:替换复制好内容的Document的标识字符
替换指定Document的内容,并保存到指定的路径 #region替换指定Document的内容,并保存到指定的路径
/// <summary>
///替换指定Document的内容,并保存到指定的路径
/// </summary>
/// <param name="docObject">Document</param>
/// <param name="savePath">保存到指定的路径</param>
protected voidReplaceWordDocAndSave(Document docObject, objectsavePath)
{
objectformat = WdSaveFormat.wdFormatDocument;
objectreadOnly = false;
objectisVisible = false;

stringstrOldText = "{WORD}";
stringstrNewText = "{替换后的文本}";

List< string> IListOldStr = newList< string>();
IListOldStr.Add( "{WORD1}");
IListOldStr.Add( "{WORD2}");

Object Nothing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Application wordApp = newApplicationClass();
//Microsoft.Office.Interop.Word.Document oDoc = wordApp.Documents.Open(ref obj, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
Microsoft.Office.Interop.Word.Document oDoc = docObject;

objectFindText, ReplaceWith, Replace;
objectMissingValue = Type.Missing;

foreach( stringstr inIListOldStr)
{

oDoc.Content.Find.Text = str;
//要查找的文本
FindText = str;
//替换文本
ReplaceWith = strNewText;

//wdReplaceAll - 替换找到的所有项。
//wdReplaceNone - 不替换找到的任何项。
//wdReplaceOne - 替换找到的第一项。
Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

//移除Find的搜索文本和段落格式设置
oDoc.Content.Find.ClearFormatting();

if(oDoc.Content.Find.Execute( refFindText, refMissingValue, refMissingValue, refMissingValue, refMissingValue, refMissingValue, refMissingValue, refMissingValue, refMissingValue, refReplaceWith, refReplace, refMissingValue, refMissingValue, refMissingValue, refMissingValue))
{
Response.Write( "替换成功!");
Response.Write( "<br>");
}
else
{
Response.Write( "没有相关要替换的:("+ str + ")字符");
Response.Write( "<br>");
}
}

oDoc.SaveAs( refsavePath, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing);

//关闭wordDoc文档对象
oDoc.Close( refNothing, refNothing, refNothing);
//关闭wordApp组件对象
wordApp.Quit( refNothing, refNothing, refNothing);
}
#endregion
用到了C#操作WORD的复制,替换,和创建WORD文档的知识。

本文出自 “摇滚互联网” 博客,请务必保留此出处http://tobetobe.blog.51cto.com/1392243/354420

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值