110,587
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 打开Word文件读取表格
/// </summary>
private void Change()
{
object fileName = "D:\\242021_200363131015.doc";
object readOnly = true;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
//开启应用程序WINWORD.EXE
//Word.ApplicationClass oWordApp = new Word.ApplicationClass();
Word._Application oWordApp = new Word.Application();
Word._Document oDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
oDoc.Activate();
//循环取出表格
for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
{
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);//remove \r\a
tableMessage += "\t";
}
tableMessage += "\n";
}
this.lbltableMessage.Text = tableMessage;
}
//关闭
//object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
//oDoc.Close(ref doNotSaveChanges, ref missing, ref missing);
this.killAllProcess();
}