1

我不知道如何将 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);
        }
    }
4

1 回答 1

0

我觉得你可以用这个。。

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

于 2013-06-20T13:10:47.453 回答