C#獲得Word文檔中一切表格的完成代碼分享。本站提示廣大學習愛好者:(C#獲得Word文檔中一切表格的完成代碼分享)文章只能為提供參考,不一定能成為您想要的結果。以下是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);
}