0

我有一个带有文本段落和表格的 word 文档。我想搜索一个以“该法案已更新为”开头的表格。该表有一个单元格。第 1 行,第 1 列。如何使用代码找到此表。不熟悉使用表和单词互操作。谢谢。

4

2 回答 2

4

我已经从我的一个项目中部分复制了这个示例(替换/删除了一些代码 - 因此它可能包含语法错误),但如果您已经在使用互操作和早期绑定 - 它可能会有所帮助

using Word = Microsoft.Office.Interop.Word;

var wordApplication = new Word.Application();
var filename = "C:\test.doc";
Word.Application wordApp = null;

if (wordApplication != null)
    wordApp = wordApplication as Word.ApplicationClass;

Word.Document wordDoc = null;
if (File.Exists(fileName.ToString()) && wordApp != null)
        {
            object readOnly = isReadonly;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;
            wordDoc = wordApp.Documents.Open(ref fileName, ref missing,
                                             ref readOnly, ref missing, ref missing, ref missing,
                                             ref missing, ref missing, ref missing, ref missing,
                                             ref missing, ref isVisible, ref missing, ref missing, ref missing,
                                             ref missing);
        }

Word.Document wordDocument = wordDoc as Word.Document;
int tablecount = wordDocument.Tables.Count;
wordDocument.Activate();
for (int i = 1; i <= tablecount; i++)
{
Word.Table wTable = wordDocument.Tables[i];
Word.Cell pCell = wTable.Cell(1, 1);
if (pCell.Range.Text == "This Act has been update to") 
    {
        MessageBox.Show("Bingo !!!");
        break;
    }
}
于 2013-07-05T09:08:23.520 回答
0

NetOffice可以为您提供一些东西。另一种解决方案是将您的 word 文件保存为 HTML 格式,然后使用HTML Agility Pack

于 2013-07-05T08:40:55.580 回答