如何逐行阅读MS Word段落和表格内容

我正在使用Microsoft.Office.Interop.Word在C#(3.5)中阅读word文档。 逐行读取,将行拆分为数组[]并处理每一行的行,并根据一些业务逻辑替换一些单词并在替换单词后,用转换后的行替换整行。

直到现在每件事情都很好。

现在我有一些word文档,那些是段落和表格。 我想逐一阅读表中的每一列,并替换特定列中列的内容。

更新


使用Office Automation

 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时只读取第一列

我想处理所有列的数据。 有没有办法确定内容是段落还是表格?

在此处输入图像描述

使用选择来扫描文档应该是非常昂贵的性能。 我建议使用以下代码:

  List TablesRanges = new List(); 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); } 

我找到了同样的解决方法。 方法如下。

1.使用WordApp.Documents.Open()打开Word文档
2.使用Selection.MoveDown逐行遍历Document
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 } } }