2012-06-13 47 views
1

我正在使用Microsoft.Office.Interop.Word在C#(3.5)中閱讀word文檔。逐行讀取,將行分割成數組[]並處理每行的單詞,並根據某些業務邏輯替換某些單詞,並在替換單詞後,用換行代替整行。如何逐行閱讀MS Word段落和表格內容

直到現在,每件事情都很好。

現在我有一些word文檔,那些有段和表。我想逐個讀取表格的每一列,並替換特定列中列的內容。

更新


使用辦公自動化

1. Opening word file. 
2. Moving cursor to top of the document 
3. Selecting first line using (`wordApp.Selection.endKey`) and processing all words 
4. After processing the words replacing the selected line with the processed line. 
5. Using wordApp.Selection.MoveDown(ref lineCount, ref countPage, ref MISSING);  
    moving next line processed further. 

問題: 1.當讀取表使用wordApp.Selection.endKey

時,我想處理所有列的讀取僅第一列數據。 有什麼方法可以確定內容是段落還是表格?

enter image description here

+2

http://stackoverflow.com/questions/10696591/retrieving-table-data-from-a-doc-file-using-c-sharp。請參閱此處以從表 – gout

+1

中檢索值除了由@gout鏈接的答案之外,Word互操作的Table接口還包含一個Columns屬性,該屬性與該鏈接中使用的Rows屬性非常相似 –

回答

2

我發現一個相同的解決方法。 方法如下。

1.打開Word中使用WordApp.Documents.Open()
2.使用Selection.MoveDown遍歷由線
3.跳過表的單元格
4.在最後的處理,僅文檔的表的內容的文檔線文獻

//Process all Paragraphs in the documents 
while (doc.ActiveWindow.Selection.Bookmarks.Exists(@"\EndOfDoc") == false) 
{ 
    doc.ActiveWindow.Selection.MoveDown(ref wdLine, ref wdCountOne, ref wdMove); 
    doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove); 

    //Skiping table content 
    if (doc.ActiveWindow.Selection.get_Information(WdInformation.wdEndOfRangeColumnNumber).ToString() != "-1") 
    { 
    while (doc.ActiveWindow.Selection.get_Information(WdInformation.wdEndOfRangeColumnNumber).ToString() != "-1") 
    { 
     if (doc.ActiveWindow.Selection.Bookmarks.Exists(@"\EndOfDoc")) 
     break; 

     doc.ActiveWindow.Selection.MoveDown(ref wdLine, ref wdCountOne, ref wdMove); 
     doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove); 
    } 
    doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove); 
    } 

    doc.ActiveWindow.Selection.EndKey(ref wdLine, ref wdExtend); 
    currLine = doc.ActiveWindow.Selection.Text; 
} 

//Processing all tables in the documents 
for (int iCounter = 1; iCounter <= doc.Tables.Count; iCounter++) 
{ 
    foreach (Row aRow in doc.Tables[iCounter].Rows) 
    { 
    foreach (Cell aCell in aRow.Cells) 
    { 
     currLine = aCell.Range.Text; 
     //Process Line 
    } 
    } 
} 
+0

「doc」指的是什麼?請用完整的代碼更新答案。 –

4

使用選擇來掃描文檔應該在性能上相當昂貴。 我建議以下代碼:

 List<Word.Range> TablesRanges = new List<Word.Range>(); 

     wordApp = new Microsoft.Office.Interop.Word.Application(); 
     doc = wordApp.Documents.OpenNoRepairDialog(FileName: @"c:\AAAAA.docx", ConfirmConversions: false, ReadOnly: true, AddToRecentFiles: false, NoEncodingDialog: true); 


     for (int iCounter = 1; iCounter <= doc.Tables.Count; iCounter++) 
     { 
      Word.Range TRange = doc.Tables[iCounter].Range; 
      TablesRanges.Add(TRange); 
     } 

     Boolean bInTable; 
     for (int par = 1; par <= doc.Paragraphs.Count; par++) 
     { 
      bInTable = false; 
      Word.Range r = doc.Paragraphs[par].Range; 
      foreach (Word.Range range in TablesRanges) 
      { 
       if (r.Start >= range.Start && r.Start <= range.End) 
       { 
        Console.WriteLine("In Table - Paragraph number " + par.ToString() + ":" + r.Text); 
        bInTable = true; 
        break; 
       } 

      } 

      if (!bInTable) 
       Console.WriteLine("!!!!!! Not In Table - Paragraph number " + par.ToString() + ":" + r.Text); 
     }