【问题标题】:How to read tables from a particular place in a document?如何从文档中的特定位置读取表格?
【发布时间】:2016-11-22 06:07:52
【问题描述】:

当我使用以下行时,它会读取该特定文档的所有表格:

  foreach (Microsoft.Office.Interop.Word.Table tableContent in document.Tables)

但我想读取特定内容的表格,例如从一个标识符到另一个标识符。

标识符可以是 [SRS oraganisation_123] 到另一个标识符 [SRS Oraganisation_456]

的形式

我只想读取上述标识符之间的表格。

假设第 34 页包含我的标识符,所以我想从该点读取所有表,直到遇到第二个标识符。我不想阅读剩余的表格。

如有任何疑问,请询问我。

【问题讨论】:

    标签: c# ms-word


    【解决方案1】:

    说开始和结束标识符存储在名为myStartIdentifiermyEndIdentifier的变量中-

        Range myRange = doc.Range();
        int iTagStartIdx = 0;
        int iTagEndIdx = 0;
    
        if (myRange.Find.Execute(myStartIdentifier))
            iTagStartIdx = myRange.Start;
    
        myRange = doc.Range();    
        if (myRange.Find.Execute(myEndIdentifier))
            iTagEndIdx = myRange.Start;
    
        foreach (Table tbl in doc.Range(iTagStartIdx,iTagEndIdx).Tables)
        {
           // Your code goes here
        }
    

    【讨论】:

      【解决方案2】:

      不确定您的程序的结构...但如果您可以访问 tableContent 中的标识符,那么您应该能够编写 LINQ 查询。

      var identifiers = new List<string>();
      identifiers.Add("myIdentifier");
      
      var tablesWithOnlyTheIdentifiersIWant = document.Tables.Select(tableContent => identifiers.Contains(tableContent.Identifier)
      
      foreach(var tableContent in tablesWithOnlyTheIdentifiersIWant)
      {
           //Do something
      }
      

      【讨论】:

      • 我的标识符不会出现在表格内容中... :(
      • 数据存储在哪里?
      • 假设第 34 页包含我的标识符,所以我想从该点读取所有表,直到遇到第二个标识符。我不想阅读剩余的表格。
      • 标识符是不是只有标识符才有的字符串?
      • 是的,它是一个字符串,它是唯一的(我可以将这些标识符读入一个列表)
      【解决方案3】:

      看看下面的代码,如果对你有帮助的话。

      System.Data.DataTable dt = new System.Data.DataTable();
                 foreach (Microsoft.Office.Interop.Word.Cell c in r.Cells)
                  {
                      if(c.Range.Text=="Content you want to compare")
                        dt.Columns.Add(c.Range.Text);
                  }
                  foreach (Microsoft.Office.Interop.Word.Row row in newTable.Rows)
                  {
                      System.Data.DataRow dr = dt.NewRow();
                      int i = 0;
                      foreach (Cell cell in row.Cells)
                      {
                          if (!string.IsNullOrEmpty(cell.Range.Text)&&(cell.Range.Text=="Text you want to compare with"))
                          {
                              dr[i] = cell.Range.Text; 
                          }
                      }
                      dt.Rows.Add(dr);
                      i++;
                  }
      

      通过以下链接的第三个数字答案。

      Replace bookmark text in Word file using Open XML SDK

      【讨论】:

      • 我要比较的内容不会在表格中,我想要的是从一个书签读取表格到另一个书签..!!
      • @CharanGourishetty 浏览我提供的链接
      猜你喜欢
      • 2020-04-14
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 2017-02-12
      • 2018-06-09
      • 2016-02-28
      • 1970-01-01
      相关资源
      最近更新 更多