使用c#合并文件夹中的Word文档

cgr*_*720 5 c# merge ms-word

我目前有一个窗口形式,当按下按钮时,会将 3 个单独的单词 docx 合并到一个组合文件中。

 private void button1_Click(object sender, EventArgs e)
    {

            string document1 = @"C:\Test\Test1.docx";
            string document2 = @"C:\Test\Test2.docx";
            string document3 = @"C:\Test\Test3.docx";

            string[] documentsToMerge = { document1, document2, document3 };

            string outputFileName = String.Format(@"C:\Test\Merge\Combined.docx", Guid.NewGuid());

            MsWord.Merge(documentsToMerge, outputFileName, true);}
Run Code Online (Sandbox Code Playgroud)

但是,我想选择包含文件夹(“C:\Test”)而不是每个单独的文件。这将允许我组合更多文件,而无需将它们单独编码到程序中,这将使其在使用时更加实用。

是否有任何建议如何实现这一目标?

public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
    {
        object defaultTemplate = documentTemplate;
        object missing = System.Type.Missing;
        object pageBreak = Word.WdBreakType.wdSectionBreakNextPage;
        object outputFile = outputFilename;

        // Create a new Word application
        Word._Application wordApplication = new Word.Application();

        try
        {
            // Create a new file based on our template
            Word.Document wordDocument = wordApplication.Documents.Add(
                                          ref missing
                                        , ref missing
                                        , ref missing
                                        , ref missing);

            // Make a Word selection object.
            Word.Selection selection = wordApplication.Selection;

            //Count the number of documents to insert;
            int documentCount = filesToMerge.Length;

            //A counter that signals that we shoudn't insert a page break at the end of document.
            int breakStop = 0;

            // Loop thru each of the Word documents
            foreach (string file in filesToMerge)
            {
                breakStop++;
                // Insert the files to our template
                selection.InsertFile(
                                            file
                                        , ref missing
                                        , ref missing
                                        , ref missing
                                        , ref missing);

                //Do we want page breaks added after each documents?
                if (insertPageBreaks && breakStop != documentCount)
                {
                    selection.InsertBreak(ref pageBreak);
                }
            }

            // Save the document to it's output file.
            wordDocument.SaveAs(
                            ref outputFile
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing);

            // Clean up!
            wordDocument = null;
        }
        catch (Exception ex)
        {
            //I didn't include a default error handler so i'm just throwing the error
            throw ex;
        }
        finally
        {
            // Finally, Close our Word application
            wordApplication.Quit(ref missing, ref missing, ref missing);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是第一段代码中引用的 MsWord.merge。我曾尝试使用 ' lnkResult.NavigateUrl = ' 但是我没有成功。

Fel*_* D. 2

由于GetFiles()将为您提供所有文件,因此第二次重载更适合。要获取所有单词文档 ( *.doc& *.docx),请调用:

//Add *.doc
string[] allWordDocuments = Directory.GetFiles("YourDirectory", "*.doc", SearchOptions.AllDirectorys); //Or if you want only SearchOptions.TopDirectoryOnly
Run Code Online (Sandbox Code Playgroud)

正如NineBerry在他的评论中所说,这也将包括*.docx

这将为您提供所有文件类型*.doc & *.docx忽略所有其他文件类型。这将避免错误,因为GetFiles("directoryName")将获取所有可能导致错误的文件,MsWord.Merge()如果您移交文件,例如*.exe

所以一个简单的方法是:

string outputPath = @"C:\Test\Merge\Combined.docx";

MsWord.Merge(allWordDocuments, outputPath, true); 
Run Code Online (Sandbox Code Playgroud)