c#獲取word檔案頁數、字數

張偉大發表於2018-10-22

引用名稱空間: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);

 

相關文章