C#获取Word文档中所有表格的实现代码分享
时间:2020-10-31 19:22:00|栏目:.NET代码|点击: 次
今天从数据库生成了一份数据字典,但是没有备注,所以需要程序把表格都读出来。用到了下面的代码,亲测可用~~
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); }
上一篇:C# 遍历文件夹子目录下所有图片及遍历文件夹下的文件
栏 目:.NET代码
下一篇:WPF拖动DataGrid滚动条时内容混乱的解决方法
本文地址:http://www.codeinn.net/misctech/17787.html