欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#获取word文件页数、字数

程序员文章站 2022-03-26 09:32:31
引用命名空间:using Microsoft.Office.Interop.Word; ......

引用命名空间: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);