Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从文档中的特定位置读取表?_C#_Ms Word - Fatal编程技术网

C# 如何从文档中的特定位置读取表?

C# 如何从文档中的特定位置读取表?,c#,ms-word,C#,Ms Word,当我使用下一行时,它读取该特定文档的所有表格: foreach (Microsoft.Office.Interop.Word.Table tableContent in document.Tables) 但我想读取特定内容的表,例如从一个标识符到另一个标识符 标识符的形式可以是另一个标识符的形式 我只想读取上述标识符之间的表 假设第34页包含我的标识符,所以我希望从该点读取所有表,直到遇到第二个标识符。我不想看剩下的表格 请询问我对问题的任何澄清。如果有帮助,请阅读以下代码 System.

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

  foreach (Microsoft.Office.Interop.Word.Table tableContent in document.Tables)
但我想读取特定内容的表,例如从一个标识符到另一个标识符

标识符的形式可以是另一个标识符的形式

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


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


请询问我对问题的任何澄清。

如果有帮助,请阅读以下代码

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++;
            }
完成以下链接的第三个数字答案


不确定您的程序是如何构造的。。。但是如果您可以访问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
}
var标识符=新列表();
标识符。添加(“myIdentifier”);
var TABLESWITHONLYTEIDENTIFIERSIWANT=document.Tables.Select(tableContent=>identifiers.Contains)(tableContent.Identifier)
foreach(表中的var tableContent,仅包含EIdentifierSiwant)
{
//做点什么
}

假设开始和结束标识符存储在名为
myStartIdentifier
myEndIdentifier
-

    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
    }

我要比较的内容将不在表中,我要做的是将表从一个书签读取到另一个书签。!!@CharanGourishetty通过我提供的链接我的标识符将不在表内容中…:(数据存储在哪里?假设第34页包含我的标识符,所以我希望从该页读取所有表,直到遇到第二个标识符为止。我不想读取其余的表。标识符是一个仅对标识符唯一的字符串吗?是的,它是一个字符串,并且是唯一的(我可以将这些标识符读入列表)