当前位置: 移动技术网 > IT编程>开发语言>c# > C#获取Word文档中所有表格的实现代码分享

C#获取Word文档中所有表格的实现代码分享

2019年07月18日  | 移动技术网IT编程  | 我要评论

今天从数据库生成了一份数据字典,但是没有备注,所以需要程序把表格都读出来。用到了下面的代码,亲测可用~~

object ofilename = @"f:\数据库.docx";
object oreadonly = false ;
object omissing = system.reflection.missing.value;
 
microsoft.office.interop.word._application oword;
microsoft.office.interop.word._document odoc;
oword = new microsoft.office.interop.word.application();
oword.visible = false;
odoc = oword.documents.open(ref ofilename, ref omissing, ref oreadonly, ref omissing, ref omissing,
  ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing);
 
//messagebox.show(odoc.tables.count.tostring());
for (int tablepos = 1; tablepos <= odoc.tables.count; tablepos++)
{
  microsoft.office.interop.word.table nowtable = odoc.tables[tablepos];
  string tablemessage = string.format("第{0}/{1}个表:\n", tablepos, odoc.tables.count);
 
  for (int rowpos = 1; rowpos <= nowtable.rows.count; rowpos++)
  {
    for (int columpos = 1; columpos <= nowtable.columns.count; columpos++)
    {
      tablemessage += nowtable.cell(rowpos, columpos).range.text;
      tablemessage = tablemessage.remove(tablemessage.length - 2, 2);
      tablemessage += "\t";
    }
 
    tablemessage += "\n";
  }
 
  messagebox.show(tablemessage);
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网