C#获取Word文档中所有表格的实现代码分享
今天从数据库生成了一份数据字典,但是没有备注,所以需要程序把表格都读出来。用到了下面的代码,亲测可用~~
objectoFileName=@"F:\数据库.docx";
objectoReadOnly=false;
objectoMissing=System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._ApplicationoWord;
Microsoft.Office.Interop.Word._DocumentoDoc;
oWord=newMicrosoft.Office.Interop.Word.Application();
oWord.Visible=false;
oDoc=oWord.Documents.Open(refoFileName,refoMissing,refoReadOnly,refoMissing,refoMissing,
refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing);
//MessageBox.Show(oDoc.Tables.Count.ToString());
for(inttablePos=1;tablePos<=oDoc.Tables.Count;tablePos++)
{
Microsoft.Office.Interop.Word.TablenowTable=oDoc.Tables[tablePos];
stringtableMessage=string.Format("第{0}/{1}个表:\n",tablePos,oDoc.Tables.Count);
for(introwPos=1;rowPos<=nowTable.Rows.Count;rowPos++)
{
for(intcolumPos=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);
}
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短