是否可以使用C#在word中合并两个文档

时间:2013-06-20 13:04:06

标签: c# ms-word

我无法如何将word文档中的Table-object变为WrapText样式。 我有两份文件。首先包含一个包含一些数据的表。 第二个包含文字。 现在,当我尝试合并这两个文件时,我添加的第一个文档是 - 包含表的文档,后跟仅包含文本的文档。

问题是 - 在输出文件中,表位于第一页,文本位于第二页。 但是,我希望将文本包装在表格旁边。所以,我可以在同一页面上显示表格和文字。

如果还有其他一些方法可以解决这个问题,请告诉我。 以下是我的代码: -

    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);
        }
    }

1 个答案:

答案 0 :(得分:0)