我正在尝试查找和替换word文档中的文本,然后将所有文档合并为一个文档。到目前为止,我得到了找到并替换部分工作。如何将所有文档合并/附加到一个文档中。 如何在替换文本后停止打开文档?
static void Main(string[] args)
{
string[] documents = { @"C:\file1.docx",
@"C:\file2.docx",
@"C:\file3.docx"};
foreach (string fileName in documents)
{
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fileName, ReadOnly: false, Visible: true);
//aDoc.Activate();
FindAndReplace(wordApp, "{ Contractor_Name}", "John Doe");
FindAndReplace(wordApp, "{day}", "1");
FindAndReplace(wordApp, "{year}", "17");
FindAndReplace(wordApp, "{month}", "August");
}
}
private static void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText)
{
//options
object matchCase = false;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
//execute find and replace
doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}
答案 0 :(得分:0)
请为.NET使用GroupDocs.Merger。通过API,您可以将Word文档列表合并为一个文档。
string sourceFile1 = "source file";
string sourceFile2 = "source file";
string password = "SomePasswordString";
Stream openFile1 = new FileStream(sourceFile1, FileMode.Open);
Stream openFile2 = new FileStream(sourceFile2, FileMode.Open);
List<JoinItem> documentStreams = new List<JoinItem>();
JoinItem item1 = new JoinItem(openFile1, FileFormat.Docx, password);
documentStreams.Add(item1);
JoinItem item2 = new JoinItem(openFile2, FileFormat.Docx, password);
documentStreams.Add(item2);
DocumentResult result = new DocumentHandler().Join(documentStreams);
Stream documentStream = result.Stream;
var fileStream = File.Create("output path" + "OutPut." + FileFormat.Docx);
documentStream.CopyTo(fileStream);
documentStream.Close();
披露:我是GroupDocs的一名开发人员。