C#实现WORD文档的内容复制和替换
2010-07-21 15:52
645 查看
最近一个项目的需求是要根据一个Word文档的模板,用记录集的具体内容替换掉里面的标识字符的内容,生成不同的文档。
分两步:
[align=left]第一:复制模板的内容到一个Document对象里[/align]
从源DOC文档复制内容返回一个Document类#region 从源DOC文档复制内容返回一个Document类
/// <summary>
/// 从源DOC文档复制内容返回一个Document类
/// </summary>
/// <param name="sorceDocPath">源DOC文档路径</param>
/// <returns>Document</returns>
protected Document copyWordDoc(object sorceDocPath)
{
object objDocType = WdDocumentType.wdTypeDocument;
object type = WdBreakType.wdSectionBreakContinuous;
//Word应用程序变量
Application wordApp;
//Word文档变量
Document newWordDoc;
object readOnly = false;
object isVisible = false;
//初始化
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
wordApp = new ApplicationClass();
Object Nothing = System.Reflection.Missing.Value;
//wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
newWordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
Document openWord;
openWord = wordApp.Documents.Open(ref sorceDocPath, 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);
openWord.Select();
openWord.Sections[1].Range.Copy();
object start = 0;
Range newRang = newWordDoc.Range(ref start, ref start);
//插入换行符
//newWordDoc.Sections[1].Range.InsertBreak(ref type);
newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);
openWord.Close(ref Nothing, ref Nothing, ref Nothing);
return newWordDoc;
}
#endregion
第二:替换复制好内容的Document的标识字符
替换指定Document的内容,并保存到指定的路径#region 替换指定Document的内容,并保存到指定的路径
/// <summary>
/// 替换指定Document的内容,并保存到指定的路径
/// </summary>
/// <param name="docObject">Document</param>
/// <param name="savePath">保存到指定的路径</param>
protected void ReplaceWordDocAndSave(Document docObject, object savePath)
{
object format = WdSaveFormat.wdFormatDocument;
object readOnly = false;
object isVisible = false;
string strOldText = "{WORD}";
string strNewText = "{替换后的文本}";
List<string> IListOldStr = new List<string>();
IListOldStr.Add("{WORD1}");
IListOldStr.Add("{WORD2}");
Object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass();
//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;
object FindText, ReplaceWith, Replace;
object MissingValue = Type.Missing;
foreach (string str in IListOldStr)
{
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(ref FindText, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref ReplaceWith, ref Replace, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue))
{
Response.Write("替换成功!");
Response.Write("<br>");
}
else
{
Response.Write("没有相关要替换的:(" + str + ")字符");
Response.Write("<br>");
}
}
oDoc.SaveAs(ref savePath, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭wordDoc文档对象
oDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
#endregion
用到了C#操作WORD的复制,替换,和创建WORD文档的知识。
分两步:
[align=left]第一:复制模板的内容到一个Document对象里[/align]
从源DOC文档复制内容返回一个Document类#region 从源DOC文档复制内容返回一个Document类
/// <summary>
/// 从源DOC文档复制内容返回一个Document类
/// </summary>
/// <param name="sorceDocPath">源DOC文档路径</param>
/// <returns>Document</returns>
protected Document copyWordDoc(object sorceDocPath)
{
object objDocType = WdDocumentType.wdTypeDocument;
object type = WdBreakType.wdSectionBreakContinuous;
//Word应用程序变量
Application wordApp;
//Word文档变量
Document newWordDoc;
object readOnly = false;
object isVisible = false;
//初始化
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
wordApp = new ApplicationClass();
Object Nothing = System.Reflection.Missing.Value;
//wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
newWordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
Document openWord;
openWord = wordApp.Documents.Open(ref sorceDocPath, 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);
openWord.Select();
openWord.Sections[1].Range.Copy();
object start = 0;
Range newRang = newWordDoc.Range(ref start, ref start);
//插入换行符
//newWordDoc.Sections[1].Range.InsertBreak(ref type);
newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);
openWord.Close(ref Nothing, ref Nothing, ref Nothing);
return newWordDoc;
}
#endregion
第二:替换复制好内容的Document的标识字符
替换指定Document的内容,并保存到指定的路径#region 替换指定Document的内容,并保存到指定的路径
/// <summary>
/// 替换指定Document的内容,并保存到指定的路径
/// </summary>
/// <param name="docObject">Document</param>
/// <param name="savePath">保存到指定的路径</param>
protected void ReplaceWordDocAndSave(Document docObject, object savePath)
{
object format = WdSaveFormat.wdFormatDocument;
object readOnly = false;
object isVisible = false;
string strOldText = "{WORD}";
string strNewText = "{替换后的文本}";
List<string> IListOldStr = new List<string>();
IListOldStr.Add("{WORD1}");
IListOldStr.Add("{WORD2}");
Object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass();
//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;
object FindText, ReplaceWith, Replace;
object MissingValue = Type.Missing;
foreach (string str in IListOldStr)
{
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(ref FindText, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref ReplaceWith, ref Replace, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue))
{
Response.Write("替换成功!");
Response.Write("<br>");
}
else
{
Response.Write("没有相关要替换的:(" + str + ")字符");
Response.Write("<br>");
}
}
oDoc.SaveAs(ref savePath, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭wordDoc文档对象
oDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
#endregion
用到了C#操作WORD的复制,替换,和创建WORD文档的知识。
相关文章推荐
- 黄聪:C#实现WORD文档的内容复制和替换
- C#实现WORD文档的内容复制和替换
- 能够按页号提取word文档文本内容的小程序,由C#实现
- 能够按页号提取word文档文本内容的小程序,由C#实现
- C# 复制一个Word文档的部分或全部内容到另一个Word文档
- c#实现word,winWordControl 文档不允许复制、粘贴、隐藏工具栏、快捷保存
- ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- [转]ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- 用C#操作office组件库实现word文档的创建及文字 表格 图片的插入
- FckEditor中从word文档粘贴过来的内容在点击确定清除格式后直接清除而不是再弹出从MS WORD粘贴对话框的实现
- C#打开WORD文档内容并显示(未完善表格图片等排版显示的处理)
- (转)C#编程实现动态生成Word文档
- 如何用C#编程实现动态生成Word文档并填充数据?
- 用C#实现在Word文档中搜索文本
- Word替换实现中英文混排文档中删除空格
- 将网页内容转换成word或excel文档的方法(c#.net)
- C#编程实现Excel文档中搜索文本内容的方法及思路
- C# Word文档转PDF的实现
- C#获取Word文档中所有表格的实现代码分享
- C#打开WORD文档内容并显示(未完善表格图片等排版显示的处理)