c#获取word文件页数、字数统计。(字数、字符数计空格、字符数不计空格)

1 篇文章 0 订阅

1、在程序中先添加dll文件引用:
在这里插入图片描述
2、页面中引用命名空间:
using Word = Microsoft.Office.Interop.Word;

3、 上代码:

                            //开始统计word文件中的字数    
                            object filePath = "/upload/123.docx"; //这里是Word文件的路径
                            filePath = Server.MapPath(filePath.ToString()); //word物理路径

                            //启动Word程序 
                            object oMissing = Missing.Value;
                            object Nothing = Missing.Value; 
                            Word.Application wordApp = new Word.ApplicationClass();         //Word应用程序变量
                            Word.Document wordDoc = wordApp.Documents.Open(                 //Word文档变量
                                          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 wordNum = wordDoc.Characters.Count;

                            Word.Range docRanger = wordDoc.Content;
                            //字数
                            int Words = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticWords);
                            //字符数(不计空格)
                            int Characters = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticCharacters);
                            //字符数(计空格)
                            int Characterswithspaces = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticCharactersWithSpaces);
                            //中文字符和朝鲜语单词
                            int Fareastcharacters = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticFarEastCharacters);
                            //段落数
                            int Paragraphs = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticParagraphs);
                            //行数
                            int Lines = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticLines);
                            //页数
                            int Pages = docRanger.ComputeStatistics(Word.WdStatistic.wdStatisticPages);

                            //输出

                            //关闭文件
                            wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                            //退出Word程序
                            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

4、上效果:
在这里插入图片描述
这是Word(123.docx)点击【审阅】-【字数统计】查看字数、字符数等。
在这里插入图片描述
这是在前台页面中输出,方便查看。
48 就那个及其 不准确统计,把空格也算进去 ;
10 、16、35、5 分别是 字数、字符数(不计空格)、字符数(计空格)、中文字符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值