我有一个word文档与文本段落以及表格。我想搜索一个文本以"This Act has been update to“开头的表。该表有一个单元格。第1行,第1列。如何使用代码查找此表。不熟悉使用表格和word interop。谢谢。
发布于 2013-07-05 17:08:24
我已经从我的一个项目中部分复制了这个示例(替换/删除了一些代码-因此它可能包含语法错误),但如果您已经在使用互操作和早期绑定-它可能会有所帮助
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-05 16:40:56
NetOffice有一些东西要提供给你。另一种解决方案是将word文件保存为HTML格式,然后使用HTML Agility Pack。
https://stackoverflow.com/questions/17484422
复制相似问题