2013-04-09 96 views
3

當我使用下面的線,它會讀取特定文檔中的所有表格:如何從文檔中的特定位置讀取表格?

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

不過我想讀例如到另一個標識特定內容從一個標識符的表。

標識符可以在[SRS oraganisation_123]形式另一個標識符[SRS Oraganisation_456]

我想僅在上述標識符之間以讀取表。

假設第34頁包含我的標識符,所以我想從該點讀取所有表,直到我遇到我的第二個標識符。我不想讀剩下的表格。

請問我對問題的任何澄清。

回答

0

說開始和結束標識符存儲在變量稱爲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 
    } 
0

通過下面的代碼,如果它可以幫助你。

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++; 
      } 

通過以下鏈接的第3個數字答案。

Replace bookmark text in Word file using Open XML SDK

+0

,我想比較不會在表中的內容,我要的是,從一個讀表書籤到另一個書籤..! – 2013-04-09 04:33:43

+0

@CharanGourishetty通過我提供的鏈接 – Freelancer 2013-04-09 04:40:23

0

不知道你的程序是如何構成的......但如果你可以訪問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 
} 
+0

我的標識符將不會在表格內容中:(...)( – 2013-04-09 04:35:29

+0

數據存儲在哪裏? – Chutes 2013-04-09 04:36:17

+0

假設第34頁包含我的標識符,所以我想從該點讀取所有表格直到我遇到我的第二個標識符我不想讀剩餘的表 – 2013-04-09 04:40:59

相關問題