Word复制和替换实例

        public string Path
{
get
{
DirectoryInfo info = new DirectoryInfo(Application.StartupPath);
return info.Parent.Parent.FullName + "\\WordData\\Table.docx";
}
}
//【C# 在word文档中复制表格并粘帖到下一页中】
private void button1_Click(object sender, EventArgs e)
{
object missing = Missing.Value;
Word.Application app = new Word.Application();
app.Visible = true;
//导入模板
object filename = Path;
Word.Document doc = app.Documents.Add(ref filename, missing, missing, missing); //复制第一个表格
doc.Tables[].Select();
app.Selection.Copy();
//在这里操作表格的文本
Word.Cell cellOne = doc.Tables[].Cell(, );
cellOne.Range.Text = "这是第一个表格";
cellOne.Range.Bold = ;
cellOne.Range.Font.ColorIndex = Word.WdColorIndex.wdRed; //下一页
object myunit = Word.WdUnits.wdStory;
app.Selection.EndKey(ref myunit, ref missing);
object pBreak = (int)Word.WdBreakType.wdPageBreak;
app.Selection.InsertBreak(ref pBreak); //粘贴第一个表格
app.Selection.Paste(); //操作第二个表格单元格
Word.Cell cellTwo = doc.Tables[].Cell(, );
cellTwo.Range.Text = "这是第二个表格";
cellTwo.Range.Underline = Word.WdUnderline.wdUnderlineDash;
} //【C#实现WORD文档的内容复制和替换】
private void button2_Click(object sender, EventArgs e)
{
LocalPathHelper pathHelper = new LocalPathHelper();
string sourceWord = WordPath.GetWordDataFullFileName("copy.docx");
string targetWord = pathHelper.DesktopPath() + "\\target.docx";
//复制文件
Word.Document doc = copyWord(sourceWord);
//查找替换 ReplaceAndSave(doc, targetWord);
} //复制word内容到Document对象
public Word.Document copyWord(object sourcePath)
{
object objDocType = Word.WdDocumentType.wdTypeDocument;
object type = Word.WdBreakType.wdSectionBreakContinuous;
object missing = Missing.Value; Word.Application app = new Word.Application();
Word.Document doc; object readOnly = false;
object isVisible = false; doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Word.Document opendWord = app.Documents.Open(ref sourcePath, 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, ref missing);
opendWord.Select();
opendWord.Sections[].Range.Copy(); object start = ;
Word.Range newRange = doc.Range(ref start, ref start); //插入换行符
//newWordDoc.Sections[1].Range.InsertBreak(ref type);
doc.Sections[].Range.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
opendWord.Close(ref missing, ref missing, ref missing);
return doc;
} //替换复制好的内容
public void ReplaceAndSave(Word.Document doc, object savePath)
{
object format = Word.WdSaveFormat.wdFormatDocument;
object missing = Missing.Value;
object readOnly = false;
object isVisible = false; string strOldText = "{Word}";
string strNewText = "{提花后的文本}";
List<string> listStr = new List<string>();
listStr.Add("{Word1}");
listStr.Add("{Word2}"); Word.Application app = new Word.Application();
//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);
Word.Document newDoc = doc; object FindText, ReplaceText, ReplaceAll;
foreach (string str in listStr)
{
newDoc.Content.Find.Text = str;
//要找的文本
FindText = str;
//替换文本
ReplaceText = strNewText;
//wdReplaceAll - 替换找到的所有项。
//wdReplaceNone - 不替换找到的任何项。
//wdReplaceOne - 替换找到的第一项。
ReplaceAll = Word.WdReplace.wdReplaceAll;
//移除Find的搜索文本和段落格式设置
newDoc.Content.Find.ClearFormatting(); if (newDoc.Content.Find.Execute(ref FindText, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref ReplaceText, ref ReplaceAll, ref missing, ref missing, ref missing, ref missing))
{
MessageBox.Show("替换成功");
}
else
{
MessageBox.Show("没有相关替换的:'" + str + "'字符");
}
} newDoc.SaveAs2(ref savePath);
//关闭文档对象,关闭组件对象
newDoc.Close(ref missing, ref missing, ref missing);
app.Quit(ref missing, ref missing, ref missing);
}
上一篇:C#从http上拿返回JSON数据


下一篇:C#如何拿到从http上返回JSON数据?