有可能在word中使用c#合并两个文档吗?

本文关键字:两个 文档 合并 word 有可能 | 更新日期: 2023-09-27 18:19:16

我不知道如何使Table-object在word文档WrapText样式。我有两份文件。首先包含一个包含一些数据的表。第二个包含文本。现在,当我尝试合并这两个文件时,我添加的第一个文档是-包含表格的文档,后面是只包含文本的文档。

问题是——在输出文件中,表在第一页,文本在第二页。但是,我希望文本在table旁边换行。这样,我就可以在同一个页面上显示表格和文本。

如果有其他的处理方法,请告诉我。下面是我的代码:-

    public static void MergeWithColumns(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
    {
        object defaultTemplate = documentTemplate;
        object missing = System.Type.Missing;
        object pageBreak = Word.WdBreakType.wdPageBreak;
        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 defaultTemplate
                                        , ref missing
                                        , ref missing
                                        , ref missing);
            // Make a Word selection object.
            Word.Selection selection = wordApplication.Selection;
            // Loop thru each of the Word documents
            foreach (string file in filesToMerge)
            {
                // 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)
                {
                    selection.InsertBreak(ref pageBreak);
                }
            }
            foreach (Word.Table tbl in wordDocument.Tables)
            {
                //tbl.AllowAutoFit = false;
                tbl.AllowAutoFit = true;
                tbl.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
            }
            // 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);
        }
    }

有可能在word中使用c#合并两个文档吗?

我想你可以用这个…

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.merge (v = office.14) . aspx