首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >引用其他文件的内容

引用其他文件的内容
EN

Stack Overflow用户
提问于 2012-09-12 13:41:37
回答 1查看 164关注 0票数 0

我们需要从其他两个文档的内容中创建一个矩阵。例如:

  1. doc的字段如下: 4.2需求-- Blah
  2. doc的字段如下: 2.1分析一个Blah

我们希望创建另一个文档(称为可跟踪矩阵),它类似于:

代码语言:javascript
复制
Col1    Col2    Col3
4.2     2.1     Blah Blah Blah

应该在doc3中动态更新4.2和2.1。

我们检查使用超链接,交叉引用,但似乎没有什么是有用的组合不同的文件。有这样的事吗?

编辑:下面是一个例子:

代码语言:javascript
复制
Technical Specification Num         Requirement Num     Requirement
4.2                                 2.1                 A sentence that explains the relationship btw 2 cols: Technical Specification and Requirement Num
EN

回答 1

Stack Overflow用户

发布于 2012-10-23 18:44:39

我现在已经创建了一个如何使用MS、Interop和C#实现这一点的工作示例。

代码包含注释,应该解释最有趣的部分。

该示例被实现为一个C#控制台应用程序,使用:

  • .NET 4.5
  • Microsoft对象库版本15.0,以及
  • Microsoft Word对象库版本15.0

..。也就是说,MSOffice2013预览版附带的MS Word Interop API。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;

namespace WordDocStats
{
    internal class Program
    {
        private static void Main()
        {
            // Open word
            var wordApplication = new Application() { Visible = true };

            // Open document A, get its headings, and close it again
            var documentA = wordApplication.Documents.Open(@"C:\Users\MyUserName\Documents\documentA.docx", Visible: true);
            var headingsA = GetHeadingsInDocument(documentA);
            documentA.Close();

            // Same procedure for document B
            var documentB = wordApplication.Documents.Open(@"C:\Users\MyUserName\Documents\documentB.docx", Visible: true);
            var headingsB = GetHeadingsInDocument(documentB);
            documentB.Close();

            // Open the target document (document C)
            var documentC = wordApplication.Documents.Open(@"C:\Users\MyUserName\Documents\documentC.docx", Visible: true);

            // Add a table to it (the traceability matrix)
            // The number of rows is the number of headings + one row reserved for a table header
            documentC.Tables.Add(documentC.Range(0, 0), headingsA.Count+1, 3);

            // Get the traceability matrix
            var traceabilityMatrix = documentC.Tables[1];

            // Add a table header and border
            AddTableHeaderAndBorder(traceabilityMatrix, "Headings from document A", "Headings from document B", "My Description");

            // Insert headings from doc A and doc B into doc C's traceability matrix
            for (var i = 0; i < headingsA.Count; i++)
            {
                // Insert headings from doc A
                var insertRangeColOne = traceabilityMatrix.Cell(i + 2, 1).Range;
                insertRangeColOne.Text = headingsA[i].Trim();

                // Insert headings from doc B
                var insertRangeColTwo = traceabilityMatrix.Cell(i + 2, 2).Range;
                insertRangeColTwo.Text = headingsB[i].Trim();
            }

            documentC.Save();
            documentC.Close();

            wordApplication.Quit();
        }

        // Based on:
        // -> http://csharpfeeds.com/post/5048/Csharp_and_Word_Interop_Part_4_-_Tables.aspx
        // -> http://stackoverflow.com/a/1817041/700926
        private static void AddTableHeaderAndBorder(Table table, params string[] columnTitles)
        {
            const int headerRowIndex = 1;

            for (var i = 0; i < columnTitles.Length; i++)
            {
                var tableHeaderRange = table.Cell(headerRowIndex, i+1).Range;
                tableHeaderRange.Text = columnTitles[i];
                tableHeaderRange.Font.Bold = 1;
                tableHeaderRange.Font.Italic = 1;
            }

            // Repeat header on each page
            table.Rows[headerRowIndex].HeadingFormat = -1;

            // Enable borders
            table.Borders.Enable = 1;
        }

        // Based on:
        // -> http://stackoverflow.com/q/7084270/700926
        // -> http://stackoverflow.com/a/7084442/700926
        private static List<string> GetHeadingsInDocument(Document document)
        {
            object headingsAtmp = document.GetCrossReferenceItems(WdReferenceType.wdRefTypeHeading);
            return ((Array)(headingsAtmp)).Cast<string>().ToList();
        }
    }
}

基本上,代码首先加载两个给定文档中的所有标题,并将它们存储在内存中。然后打开目标文档,创建可跟踪矩阵并对其进行样式化,最后将标题插入到矩阵中。

守则所依据的假设是:

  • 存在目标文档(documentC.docx)。
  • 两个输入文档(documentA.docx和documentB.docx)中的标题数包含相同数量的标题--这个假设是基于您关于不想要笛卡尔产品的评论做出的。

我希望这符合你的要求:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12389569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档