欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 21:32:04
今天从数据库生成了一份数据字典,但是没有备注,所以需要程序把表格都读出来。用到了下面的代码,亲测可用~~ object ofilename = @"f:\数据...

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

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);
}

上一篇:

下一篇: