引用命名空间:using Microsoft.Office.Interop.Word;
//启动Word程序 Application myWordApp = new ApplicationClass(); object oMissing = Missing.Value; object Nothing = Missing.Value; object filePath = "/upload/123.docx"; //这里是Word文件的路径 filePath = Server.MapPath(filePath.ToString()); //word物理路径 //打开文件 Document myWordDoc = myWordApp.Documents.Open( ref filePath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); //文件页数 int pageNum = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref Nothing); //文件字数 int wordNum = myWordDoc.Characters.Count; //关闭文件 myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing); //退出Word程序 myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing); Response.Write("文件页数:" + pageNum + "<br/>文件字数:" + wordNum);