【问题标题】:How to enumerate word document using office interop API?如何使用 office interop API 枚举 word 文档?
【发布时间】:2011-03-31 12:44:25
【问题描述】:

我想逐个遍历word文档的所有元素,并根据元素的类型(标题、句子、表格、图像、文本框、形状等)我想处理那个元素。我试图在办公室互操作 API 中搜索任何可以表示文档元素的枚举器或对象,但没有找到。 API 提供句子、段落、形状集合,但不提供可以指向下一个元素的通用对象。 例如:

<header of document>
<plain text sentences>
<table with many rows,columns>
<text box>
<image>
<footer>

(请把它想象成一个word文档)


所以,现在我想要一些枚举器,它首先给我&lt;header of document&gt;,然后在下一次迭代中给我&lt;plain text sentences&gt;,然后是&lt;table with many rows,columns&gt;,依此类推。 有谁知道我们如何实现这一目标?有可能吗?

我正在使用 C#、Visual Studio 2005 和 Word 2003。

非常感谢

【问题讨论】:

    标签: c# interop ms-word


    【解决方案1】:

    例如:

            // open the file
            Word.ApplicationClass app = new Word.ApplicationClass();
            object path = @"c:\Users\name\Desktop\Весь набор.docx";
            object missing = System.Reflection.Missing.Value;
    
            Word.Document doc = null;
            try
            {
                doc = app.Documents.Open(ref path,
                    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);
    
                // index
                foreach ( Word.Section section in doc.Sections)
                {
                    Debug.WriteLine("Section index:" + section.Index);
                    Debug.WriteLine("section start: " + section.Range.Start + ", section end: " + section.Range.End);
    
                }
    
    
                bool processNextTable = false;
                foreach (Word.Paragraph paragraph in doc.Paragraphs)
                {
                    string toWrite = paragraph.Range.Text;
                    System.Diagnostics.Debug.WriteLine(toWrite);
                }
    
                foreach (Word.Table table in doc.Tables)
                {
                    foreach (Word.Row wRow in table.Rows)
                        foreach (Word.Cell cell in wRow.Cells)
                        {
                        }
                }
    
            }
            finally
            {
                if (doc != null)
                {
                    bool saveChanges = false; // temporary not save any changes
                    app.Quit(ref saveChanges, ref missing, ref missing);
                }
            }
    

    【讨论】:

      【解决方案2】:

      您没有简单迭代器的原因是 Word 文档可能比您问题中概述的简单结构复杂得多。

      例如,一个文档的首页可能有多个页眉和页脚以及偶数页和奇数页,包含多个具有不同页眉和页脚设置的部分,包含脚注、cmets 和修订,以及表格等对象, 文本框、图像和形状可能与文本内嵌或浮动显示。简而言之,没有固定的元素序列。

      您必须检查输入文档的复杂程度,并根据分析结果决定如何迭代段落以及附加的图像和形状等。

      【讨论】:

      • 非常感谢您的解释。所以基本上我们不能通过文档元素来枚举。我问这个问题是因为我在处理单词表和不同形状时遇到了问题。有时 word API 会跳过文档中的几个单词,因此我的程序会失败。此外,API 给出了错误的句子,例如如果句子是“作为高级程序员工作”,那么我会得到“作为高级程序员工作”。作为一个句子和“程序员”作为第二个。它应该是一个句子。我想避免这类问题。此外,互操作 API 还有很多问题。
      • @shekhar:当然可以迭代内容,但不能以简单的方式。 Word 使您可以完全访问所有对象。关于句子分割,您需要考虑到这是自然语言处理中一个不那么微不足道的研究课题。
      • 是否可以迭代内容?该怎么做?
      猜你喜欢
      • 2013-07-04
      • 2011-05-09
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多