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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解C#获取Word文档中所有表格的实现代码讲解的相关内容吗,在本文为您仔细讲解C#获取Word中表格的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,获取Word表格,下面大家一起来学习吧。

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

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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们